-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
clean.js
75 lines (64 loc) · 1.61 KB
/
clean.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
71
72
73
74
75
import { isFrontMatterNode } from "../common/util.js";
import { startWithPragma } from "./pragma.js";
const ignoredProperties = new Set([
"position",
"raw", // front-matter
]);
function clean(ast, newObj, parent) {
// for codeblock
if (
ast.type === "front-matter" ||
ast.type === "code" ||
ast.type === "yaml" ||
ast.type === "import" ||
ast.type === "export" ||
ast.type === "jsx"
) {
delete newObj.value;
}
if (ast.type === "list") {
delete newObj.isAligned;
}
if (ast.type === "list" || ast.type === "listItem") {
delete newObj.spread;
delete newObj.loose;
}
// texts can be splitted or merged
if (ast.type === "text") {
return null;
}
if (ast.type === "inlineCode") {
newObj.value = ast.value.replace(/[\t\n ]+/g, " ");
}
if (ast.type === "wikiLink") {
newObj.value = ast.value.trim().replace(/[\t\n]+/g, " ");
}
if (ast.type === "definition" || ast.type === "linkReference") {
newObj.label = ast.label
.trim()
.replace(/[\t\n ]+/g, " ")
.toLowerCase();
}
if (
(ast.type === "definition" ||
ast.type === "link" ||
ast.type === "image") &&
ast.title
) {
newObj.title = ast.title.replace(/\\(["')])/g, "$1");
}
// for insert pragma
if (
parent &&
parent.type === "root" &&
parent.children.length > 0 &&
(parent.children[0] === ast ||
(isFrontMatterNode(parent.children[0]) && parent.children[1] === ast)) &&
ast.type === "html" &&
startWithPragma(ast.value)
) {
return null;
}
}
clean.ignoredProperties = ignoredProperties;
export default clean;