This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
/
remark-plugins.js
70 lines (66 loc) · 2.21 KB
/
remark-plugins.js
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
// https://github.com/wooorm/remark
import remark from "remark";
// https://github.com/wooorm/remark-toc
import toc from "remark-toc";
// https://github.com/wooorm/remark-slug
import slug from "remark-slug";
// https://github.com/ben-eb/remark-autolink-headings
import autoLinkHeadings from "remark-autolink-headings";
// https://github.com/wooorm/remark-html
import html from "remark-html";
// https://github.com/mapbox/remark-react
import reactRenderer from "remark-react";
// for code highlight
import deepmerge from "deepmerge";
import sanitizeGhSchema from "hast-util-sanitize/lib/github.json";
export default (config?: PhenomicConfig, body: string) => {
const remarkInstance = remark().use(toc).use(slug).use(autoLinkHeadings, {
// @todo find how to make this options works with remark-react
content: {
type: "text",
value: "#"
},
linkProperties: {
className: "phenomic-HeadingAnchor"
}
});
const useReact =
config &&
config.plugins &&
config.plugins.find(p => p.name === "@phenomic/plugin-renderer-react");
if (!useReact) {
remarkInstance.use(html);
} else {
remarkInstance.use(reactRenderer, {
sanitize: deepmerge(sanitizeGhSchema, {
// remove user-content from github.json to remark-slug work as expected
clobberPrefix: "",
// allow code to have className
attributes: { "*": ["className"] }
}),
// we cannot rely on components from here as we are serializing this as
// json
// remarkReactComponents: {
// code: RemarkLowlight(languages),
// },
createElement: (component, props, children) => {
// here we optimize structure just a little to have to smallest json
// possible
return {
...(!children
? {}
: Array.isArray(children) && children.length === 0
? {}
: Array.isArray(children) && children.length === 1
? { c: children[0] }
: { c: children }),
...(!props
? {}
: Object.keys(props).length === 0 ? {} : { p: props }),
t: component
};
}
});
}
return remarkInstance.processSync(body, { commonmark: true });
};