-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.ts
172 lines (162 loc) · 4.12 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { createDebug } from '@umijs/utils';
import type { Transformer } from 'unified';
import unified from 'unified';
import type { Node } from 'unist';
import frontmatter from 'remark-frontmatter';
import math from 'remark-math';
import mathjax from 'rehype-mathjax';
import headings from 'rehype-autolink-headings';
import comments from 'rehype-remove-comments';
import stringify from 'rehype-stringify';
import parse from 'remark-parse';
import gfm from 'remark-gfm';
import rehype from './rehype';
import slug from './slug';
import meta from './meta';
import codeBlock from './codeBlock';
import code from './code';
import embed from './embed';
import api from './api';
import mdComponent from './mdComponent';
import link from './link';
import img from './img';
import table from './table';
import previewer from './previewer';
import raw from './raw';
import jsxify from './jsxify';
import isolation from './isolation';
import domWarn from './domWarn';
import sourceCode from './sourceCode';
const log = createDebug('dumi:remark');
interface IDumiVFileData {
/**
* markdown file path base cwd
*/
filePath?: string;
/**
* markdown file updated time in git history, fallback to file updated time
*/
updatedTime?: number;
/**
* the related component name of markdown file
*/
componentName?: string;
/**
* page title
*/
title?: string;
/**
* component keywords
*/
keywords?: string[];
/**
* mark component deprecated
*/
deprecated?: true;
/**
* component uuid (for HiTu)
*/
uuid?: string;
/**
* slug list in markdown file
*/
slugs?: {
depth: number;
value: string;
heading: string;
}[];
}
function debug(name: string) {
return function debugPlugin() {
return () => {
if (this.data('fileAbsPath')) {
log(name, this.data('fileAbsPath'));
}
};
};
}
// reserve unknown property for Node, to avoid custom plugin throw type error after @types/[email protected]
declare module 'unist' {
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style, @typescript-eslint/no-shadow
export interface Node {
[key: string]: unknown;
}
}
export interface IDumiElmNode extends Node {
properties: {
id?: string;
href?: string;
[key: string]: any;
};
tagName: string;
children?: IDumiElmNode[];
}
export type IDumiUnifiedTransformer = (
node: Parameters<Transformer>[0],
vFile: Parameters<Transformer>[1] & { data: IDumiVFileData },
next?: Parameters<Transformer>[2],
) => ReturnType<Transformer>;
export default (source: string, fileAbsPath: string, type: 'jsx' | 'html', masterKey?: string) => {
const rehypeCompiler: any = {
jsx: [jsxify],
html: [stringify, { allowDangerousHtml: true, closeSelfClosing: true }],
}[type];
const processor = unified()
// parse to remark
.use(parse)
.use(debug('parse'))
.use(gfm)
.use(debug('gfm'))
// remark plugins
.use(frontmatter)
.use(debug('frontmatter'))
.use(math)
.use(debug('math'))
.use(meta)
.use(debug('meta'))
.use(codeBlock)
.use(debug('codeBlock'))
// remark to rehype
.use(rehype)
.use(debug('rehype'))
// rehype plugins
.use(mathjax)
.use(debug('mathjax'))
.use(sourceCode)
.use(debug('sourceCode'))
.use(raw)
.use(debug('raw'))
.use(domWarn)
.use(debug('domWarn'))
.use(comments, { removeConditional: true })
.use(debug('comments'))
.use(code)
.use(debug('code'))
.use(api)
.use(debug('api'))
.use(mdComponent)
.use(slug)
.use(debug('slug'))
.use(embed)
.use(debug('embed'))
.use(headings)
.use(debug('headings'))
.use(link)
.use(debug('link'))
.use(img)
.use(debug('img'))
.use(table)
.use(debug('table'))
.use(previewer)
.use(debug('previewer'))
.use(isolation)
.use(debug('isolation'))
.data('masterKey', masterKey)
.data('fileAbsPath', fileAbsPath)
.data('outputType', type);
// apply compiler via type
processor.use(rehypeCompiler[0], rehypeCompiler[1]);
const result = processor.processSync(source);
debug('compiler').call(processor);
return result;
};