-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentProcessors.ts
92 lines (85 loc) · 2.49 KB
/
contentProcessors.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
import rehypePrism from 'rehype-prism';
import { unified } from 'unified';
import remarkRehype from 'remark-rehype';
import rehypeStringify from 'rehype-stringify';
import rehypeParse from 'rehype-parse';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import remarkParse from 'remark-parse';
import remarkGfm from 'remark-gfm';
import externalLinks from './utils/plugins/external-links';
import htmlSlugify from './utils/plugins/html-slugify copy';
import checkLinksToMissingTranslations from './utils/plugins/missing-translations';
import addTableScroll from './utils/plugins/table-scroll';
import cleanupCodeSamples from './utils/plugins/cleanup-code-samples';
import { list } from './mdast/list';
import { code } from './mdast/code';
import rewriteRedirects from './utils/plugins/rewrite-redirects';
export function createHtmlParser() {
return unified().use(rehypeParse, { fragment: true });
}
export const htmlParseAndProcess = createHtmlParser()
.use(rehypeAutolinkHeadings) // Wrap headings in links, so they became inteactive
.use(externalLinks, {
target: '_blank',
rel: ['noopener', 'noreferrer'],
className: 'wd-external',
});
interface HtmlPostProcessorOptions {
existingLinks: string[];
redirectMap: Record<string, string>;
}
export const createHtmlPostProcessor = (options: HtmlPostProcessorOptions) => {
return createHtmlParser()
.use([[rewriteRedirects, options]])
.use([[checkLinksToMissingTranslations, options]])
.use(rehypeStringify, { allowDangerousHtml: true });
};
export const mdParseAndProcess = unified()
.use(remarkParse)
.use([remarkGfm, cleanupCodeSamples])
.use(remarkRehype, {
handlers: {
list,
code,
},
allowDangerousHtml: true,
})
.use([
htmlSlugify,
[
rehypeAutolinkHeadings,
{
behavior: 'append',
linkProperties: {
'aria-hidden': 'true',
},
},
],
[
externalLinks,
{
target: '_blank',
rel: ['noopener', 'noreferrer'],
className: 'wd-external',
},
],
]);
export const htmlProcess = unified()
.use([rehypePrism, addTableScroll])
// TODO:
// [
// remarkAutolinkHeadings,
// {
// content: {
// type: 'element',
// tagName: 'span',
// properties: {
// className: 'icon icon-link',
// },
// },
// linkProperties: {
// 'aria-hidden': 'true',
// },
// },
// ],
.use(rehypeStringify, { allowDangerousHtml: true });