mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-08 20:37:53 +02:00
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:
parent
ac4a253cdf
commit
3fc47938a5
27 changed files with 345 additions and 287 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue