-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (37 loc) · 1.09 KB
/
index.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
import moment from "moment";
import { ast, text, html, isHeading, isParagraph, isText, isDepth, hasChildren } from "remark-helpers";
const isHeader = (node) => {
return isHeading(node) && isDepth(node, 1);
}
const isTag = (node) => {
return isParagraph(node)
&& hasChildren(node)
&& isText(node.children[0])
&& node.children[0].value.startsWith("#")
&& node.children[0].value.endsWith(";");
}
const isDate = (node) => {
if (!node.value && !node.children) {
return false;
}
if (node.value) {
return moment(node.value).isValid();
} else if (node.children) {
return isDate(node.children[0]);
}
}
export default (input) => {
let mdast = ast(input).children;
let clonedMdast = { type: "root", children: [] };
for (let i = 0; i < mdast.length; i++) {
if (isHeader(mdast[i]) || isDate(mdast[i]) || isTag(mdast[i])) {
continue;
}
clonedMdast.children.push(mdast[i]);
}
return {
text: text(clonedMdast),
html: html(clonedMdast),
clonedMdast
};
};