chore(mdx-loader): migrate package to TypeScript (#5347)

* Polish code style

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Partly done migration

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Complete typing

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Fix tests

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* A-ha

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Cleanup

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Fix error

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Cleanup

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
Joshua Chen 2021-08-12 20:55:14 +08:00 committed by GitHub
parent ac4a253cdf
commit 3fc47938a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 345 additions and 287 deletions

View file

@ -13,6 +13,7 @@ import visit from 'unist-util-visit';
import remarkStringify from 'remark-stringify';
import htmlTags from 'html-tags';
import toText from 'hast-util-to-string';
import type {Code, InlineCode} from 'mdast';
const tags = htmlTags.reduce((acc: {[key: string]: boolean}, tag) => {
acc[tag] = true;
@ -21,10 +22,10 @@ const tags = htmlTags.reduce((acc: {[key: string]: boolean}, tag) => {
export default function sanitizeMD(code: string): string {
const markdownTree = unified().use(markdown).parse(code);
visit(markdownTree, 'code', (node) => {
visit(markdownTree, 'code', (node: Code) => {
node.value = `\n<!--${node.value}-->\n`;
});
visit(markdownTree, 'inlineCode', (node) => {
visit(markdownTree, 'inlineCode', (node: InlineCode) => {
node.value = `<!--${node.value}-->`;
});
@ -33,7 +34,7 @@ export default function sanitizeMD(code: string): string {
.stringify(markdownTree);
const htmlTree = unified().use(parse).parse(markdownString);
visit(htmlTree, 'element', (node) => {
visit(htmlTree, 'element', (node: any) => {
if (!tags[node.tagName as string]) {
node.type = 'text';
node.value = node.tagName + toText(node);