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

@ -0,0 +1,39 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import escapeHtml from 'escape-html';
import toString from 'mdast-util-to-string';
import type {Parent} from 'unist';
import type {StaticPhrasingContent, Heading} from 'mdast';
export function stringifyContent(node: Parent): string {
return ((node.children || []) as StaticPhrasingContent[])
.map(toValue)
.join('');
}
export function toValue(node: StaticPhrasingContent | Heading): string {
if (node && node.type) {
switch (node.type) {
case 'text':
return escapeHtml(node.value);
case 'heading':
return stringifyContent(node);
case 'inlineCode':
return `<code>${escapeHtml(node.value)}</code>`;
case 'emphasis':
return `<em>${stringifyContent(node)}</em>`;
case 'strong':
return `<strong>${stringifyContent(node)}</strong>`;
case 'delete':
return `<del>${stringifyContent(node)}</del>`;
default:
}
}
return toString(node);
}