-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.ts
48 lines (43 loc) · 1.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import path from 'upath';
import getYamlConfig from 'umi-build-dev/lib/routes/getYamlConfig';
import remark from './remark';
const FRONT_COMMENT_EXP = /^\n*\/\*[^]+?\s*\*\/\n*/;
const MD_WRAPPER = `
import React from 'react';
import Alert from '${path.join(__dirname, '../themes/default/alert.js')}';
import FatherDocPreviewer from '${path.join(__dirname, '../themes/default/previewer.js')}';
export default function () {
return <>$CONTENT</>;
}`
export interface TransformResult {
content: string;
config: {
frontmatter: { [key: string]: any };
[key: string]: any;
};
}
export default {
markdown(raw: string, dir: string): TransformResult {
const result = remark(raw, dir);
const contents = (result.contents as string).replace(/class="/g, 'className="');
return {
content: MD_WRAPPER.replace('$CONTENT', contents),
config: {
frontmatter: {},
...result.data as TransformResult['config'],
},
};
},
jsx(raw: string): TransformResult {
return {
// discard frontmatter for source code display
content: raw.replace(FRONT_COMMENT_EXP, ''),
config: {
frontmatter: getYamlConfig(raw),
},
};
},
tsx(raw: string): TransformResult {
return this.jsx(raw);
},
}