-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparse.js
More file actions
35 lines (35 loc) · 1.16 KB
/
parse.js
File metadata and controls
35 lines (35 loc) · 1.16 KB
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
import mit from "markdown-it";
export const parse = (content) => {
const parser = mit();
const tokens = parser.parse(content, {});
const parsedLinks = [];
const parsedImages = [];
const scan = (tokens) => {
tokens.forEach((token, index) => {
if (token.type === "link_open") {
const indexOfNextClose = tokens.findIndex((t2, i2) => i2 > index && t2.type === "link_close");
if (indexOfNextClose > index) {
parsedLinks.push({
target: token.attrGet("href"),
content: tokens
.slice(index + 1, indexOfNextClose)
.map((t) => t.content)
.join(""),
});
}
}
if (token.type === "image")
parsedImages.push({
src: token.attrGet("src"),
alt: token.content,
});
if (token.children)
scan(token.children);
});
};
scan(tokens);
return {
links: parsedLinks,
images: parsedImages,
};
};