Skip to content

Commit c5ac332

Browse files
committed
renderer with defaultRenderer
1 parent 68c77b2 commit c5ac332

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

README.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,42 @@ rules: [
3535
Usage in your markdown:
3636
--
3737

38-
Loader enhances the markdown syntax. Like `code` lang extended property.
38+
Loader enhances the markdown syntax. Like `code` lang extended property. Each time you use any of the next code chunks, you got render of the contained code.
3939

40-
- Use code block with language
41-
` ```js{eval} `
42-
to eval some script at the beginning of the document.
40+
- `js{eval}` To eval some script at the beginning of the document.
4341

44-
- Use code block with language
45-
` ```js{render} `
46-
to inline render React component from the code.
42+
- `js{render}` To inline render React component from the code.
4743

48-
- Use code block with language
49-
` ```js{+render}`
50-
to display the code and render the code both
44+
- `js{+render}` To display the code and render the code both
5145

52-
- Use code block with language
53-
` ```js{render+}`
54-
to render the code, and then display the code
46+
- `js{render+}` to render the code, and then display the code
47+
48+
## Import markdown
49+
50+
As the result, you will get high-grade React Component.
51+
52+
```js
53+
import React from 'react';
54+
import ReactDom from 'react-dom';
55+
import Readme from './Readme.md';
56+
57+
ReactDom.render(
58+
<Readme />,
59+
document.getElementById('root')
60+
)
61+
```
62+
63+
As a normal React component, it can accept props.
64+
65+
```js
66+
<Readme version="v1.0.0" />
67+
```
68+
69+
And markdown inline-components can use this props.
70+
71+
```js
72+
<Version>{props.version}</Version>
73+
```
5574

5675
## Advanced configuration
5776

@@ -104,7 +123,7 @@ module.exports = {
104123

105124
### AST renderer
106125

107-
In super-advanced way you can add your custom logic to render AST to javascript.
126+
In super-advanced way you can add your custom logic to render AST to javascript. Also you can add some initial code to the evalChunks.
108127

109128
```js
110129
{
@@ -113,19 +132,22 @@ In super-advanced way you can add your custom logic to render AST to javascript.
113132
use: {
114133
loader: 'markdown-feat-react-loader',
115134
options: {
116-
renderer: function(ast, evalChunks) {
135+
renderer: function(ast, evalChunks, defaultRenderer) {
117136
// ast - Markdown ast
137+
118138
// evalChunks - chunks of code, which will be injected to the top of javascript document
119-
// ...
139+
evalChunks.push(`const lodash = require('lodash');`)
120140

121-
return `...here you javascript...`;
141+
return defaultRenderer(ast);
122142
},
123143
}
124144
}
125145
}
126146

127147
```
128148

149+
You got the function `defaultRenderer` as the third argument, it provides you to render AST with the native logic, but you can manually convert AST to javascript code.
150+
129151
License
130152
--
131153

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-feat-react-loader",
3-
"version": "1.0.0-beta.1",
3+
"version": "1.0.0",
44
"description": "Use js inside markdown",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function defaultRenderer(ast) {
6262
); }`
6363
}
6464

65-
module.exports = function markdownReactStory(content) {
65+
module.exports = function markdownFeatReact(content) {
6666
const evalChunks = []
6767
const codechunks = []
6868

@@ -137,7 +137,7 @@ module.exports = function markdownReactStory(content) {
137137
ast.children = ast.children.map(extractCodeChunk).filter(Boolean)
138138

139139
/* Render js code */
140-
const code = renderer(ast, evalChunks)
140+
const code = renderer(ast, evalChunks, defaultRenderer)
141141

142142
const header = `
143143
"use strict";

0 commit comments

Comments
 (0)