forked from web-platform-dx/web-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.ts
More file actions
31 lines (26 loc) · 1.02 KB
/
text.ts
File metadata and controls
31 lines (26 loc) · 1.02 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
import { toString as hastTreeToString } from "hast-util-to-string";
import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
export function convertMarkdown(markdown: string) {
const mdTree = unified().use(remarkParse).parse(markdown);
const htmlTree = unified().use(remarkRehype).runSync(mdTree);
const text = hastTreeToString(htmlTree);
let html = unified().use(rehypeStringify).stringify(htmlTree);
// Remove leading <p> and trailing </p> if there is only one of each in the
// description. (If there are multiple paragraphs, let them be.)
if (
html.lastIndexOf("<p>") === 0 &&
html.indexOf("</p>") === html.length - 4
) {
html = html.substring(3, html.length - 4);
}
return { text, html };
}
export function convertHTML(html: string) {
const htmlTree = unified().use(rehypeParse).parse(html);
const text = hastTreeToString(htmlTree);
return { text };
}