-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(umi-plugin): support to transform normal md to page component (#2)
- Loading branch information
1 parent
f91e295
commit 1227906
Showing
7 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
import './layout.less'; | ||
|
||
export default ({ children }) => children; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import 'https://cdnjs.cloudflare.com/ajax/libs/prism/1.17.1/themes/prism.css'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import remark from './remark'; | ||
|
||
export interface TransformResult { | ||
content: string; | ||
config: { [key: string]: any }; | ||
} | ||
|
||
export default { | ||
markdown(raw: string): TransformResult { | ||
return { | ||
content: `export default function () { | ||
return ( | ||
<div>${remark(raw)}</div> | ||
) | ||
}`, | ||
config: {}, | ||
}; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/umi-plugin-father-doc/src/transformer/remark/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import unified from 'unified'; | ||
import parse from 'remark-parse'; | ||
import rehype from 'remark-rehype'; | ||
import stringify from 'rehype-stringify'; | ||
import prism from '@mapbox/rehype-prism'; | ||
import jsx from './jsx'; | ||
|
||
const processor = unified() | ||
.use(parse) | ||
.use(rehype) | ||
.use(stringify, { allowDangerousHTML: true }) | ||
.use(prism) | ||
.use(jsx); | ||
|
||
export default (raw: string) => processor.processSync(raw).contents as string; |
38 changes: 38 additions & 0 deletions
38
packages/umi-plugin-father-doc/src/transformer/remark/jsx.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Node } from 'unist'; | ||
import visit from 'unist-util-visit'; | ||
|
||
function hasSubClassName(className: string[], subCls: string) { | ||
return (className || []).find(cls => cls.indexOf(subCls) > -1); | ||
} | ||
|
||
const visitor = (node, i, parent) => { | ||
// escape { & } for JSX | ||
node.value = node.value.replace(/([{}])/g, '{\'$1\'}'); | ||
|
||
// convert \n to <br> in code block for JSX | ||
if ( | ||
parent | ||
&& parent.tagName === 'code' | ||
&& hasSubClassName(parent.properties.className, 'language-') | ||
&& node.type === 'text' | ||
) { | ||
const replace = node.value.split('\n').reduce((result, str, isNotFirst) => { | ||
if (isNotFirst) { | ||
result.push({ type: 'raw', value: '<br />' }); | ||
} | ||
|
||
if (str) { | ||
result.push({ type: 'text', value: str }); | ||
} | ||
|
||
return result; | ||
}, []); | ||
|
||
// replace original children | ||
parent.children.splice(i, 1, ...replace); | ||
} | ||
} | ||
|
||
export default () => (tree: Node) => { | ||
visit(tree, 'text', visitor); | ||
} |