/
clean.js
79 lines (67 loc) · 1.69 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
76
77
78
79
"use strict";
const collapseWhiteSpace = require("collapse-white-space");
const { isFrontMatterNode } = require("../common/util.js");
const { startWithPragma } = require("./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" ||
ast.type === "imageReference"
) {
newObj.label = collapseWhiteSpace(ast.label);
}
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;
module.exports = clean;