-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
176 lines (151 loc) · 4.04 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
173
174
175
176
import { visit, SKIP } from 'unist-util-visit';
import type { Plugin } from 'unified';
import type { Root, Content } from 'mdast';
type Node = Content & { children?: Content };
/**
* generated from: https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements
* `Array.from(document.querySelectorAll('section[aria-labelledby="list_of_inline_elements"] > div > ul > li a code')).map((v) => v.innerText.replace(/<(.*)>/gi, '$1'))`
* */
export const inlineElements = [
'a',
'abbr',
'acronym',
'audio',
'b',
'bdi',
'bdo',
'big',
'br',
'button',
'canvas',
'cite',
'code',
'data',
'datalist',
'del',
'dfn',
'em',
'embed',
'i',
'iframe',
'img',
'input',
'ins',
'kbd',
'label',
'map',
'mark',
'meter',
'noscript',
'object',
'output',
'picture',
'progress',
'q',
'ruby',
's',
'samp',
'script',
'select',
'slot',
'small',
'span',
'strong',
'sub',
'sup',
'svg',
'template',
'textarea',
'time',
'u',
'tt',
'var',
'video',
'wbr',
];
/** **some** common block elements */
export const blockElements = ['div', 'aside', 'header', 'image', 'main', 'figure', 'p'];
/**
*
* @param noInlineHTML due to: https://github.com/syntax-tree/mdast-util-mdx-jsx mdxElement may contains some plain html element, we may exclude them
*/
export const isJsxElement = (node: Content & { name?: string }, noInlineHTML = false) => {
const jsx = node.type?.startsWith('mdxJsx');
if (!noInlineHTML || !node.name) return jsx;
return !inlineElements.includes(node.name) && jsx;
};
const splice = ([] as Content[]).splice;
const paragraph = 'paragraph';
export const DefaultOption = {
unwrapTags: (node: Content) =>
blockElements.includes(node.type || '') || isJsxElement(node, true),
noIncludeTags: (node: Content) =>
['mdxBlockElement'].includes(node.type || '') || isJsxElement(node, false),
};
export type RemarkDropParagraphOption = Partial<typeof DefaultOption>;
// https://github.com/mdx-js/mdx/issues/1170#issuecomment-725622285
const remarkDropParagraph: Plugin<[RemarkDropParagraphOption?], Root> = (options) => {
const { unwrapTags = () => false, noIncludeTags = () => false } = Object.assign(
{},
DefaultOption,
options,
);
const isNeedCleanInnerParagraph = (node: Content) => {
return noIncludeTags(node);
};
const isNeedCleanOuterParagraph = (node: Content) => {
return unwrapTags(node);
};
const travelChildren = (nodes: Content[]) => {
let paragraphChildren: Content[] = [];
const wrap = () =>
({
type: 'paragraph',
children: paragraphChildren,
position: paragraphChildren[0]?.position,
} as Content);
let needClean = false;
return {
children: nodes.reduce((acc, cur, idx) => {
if (isNeedCleanOuterParagraph(cur)) {
needClean = true;
const ret = [...acc, wrap(), cur];
paragraphChildren = [];
return ret;
} else {
paragraphChildren.push(cur);
if (idx >= nodes.length - 1 && paragraphChildren.length > 0) {
return [...acc, wrap()];
} else {
return acc;
}
}
}, [] as Content[]),
needClean,
};
};
function visitor(node: Node, index: number | null, parent: Node | undefined) {
// if there are children available keep diving into them
if (Array.isArray(node.children)) {
node.children.forEach(function (child) {
visit(child, visitor as any);
});
}
const isParagraph = node.type === paragraph;
if (isParagraph && parent && typeof index === 'number') {
if (isNeedCleanInnerParagraph(parent)) {
splice.apply(parent.children, [index, 1, ...(node.children || [])]);
return [SKIP, index];
}
const { needClean, children } = travelChildren(node.children || []);
if (needClean) {
splice.apply(parent.children, [index, 1, ...children]);
return [SKIP, index];
}
}
}
return (root: Root) => {
visit(root, visitor as any);
};
};
export default remarkDropParagraph;