-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (26 loc) · 878 Bytes
/
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
import moment from "moment";
import { ast, text, html, isParagraph } from "remark-helpers";
const isDate = (format, locale, node) => {
return moment(text(node), format, locale, true).isValid();
}
export default (format, locale, input) => {
if (!input)
return;
let mdast = ast(input).children;
let clonedMdast = { type: "root", children: [] };
for (let i = 0; i < mdast.length; i++) {
if (isParagraph(mdast[i]) && isDate(format, locale, mdast[i])) {
clonedMdast.children.push(mdast[i]);
}
};
if (clonedMdast.children.length === 0)
return;
let plainText = text(clonedMdast);
return {
text: plainText,
html: html(clonedMdast),
unix: moment.utc(plainText, format, locale, true).unix(),
moment: moment(plainText, format, locale, true),
clonedMdast
};
};