diff --git a/src/index.ts b/src/index.ts index 3b7a211..a7aca15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,8 @@ import capitalize from "lodash.capitalize" import { visit } from "unist-util-visit" -import { toHast } from "mdast-util-to-hast" -import { toHtml } from "hast-util-to-html" import { toString } from 'mdast-util-to-string'; import { FindAndReplaceList, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace" -import type { Callout, Root, Tag } from "mdast"; +import type { Comment, Root, Tag } from "mdast"; declare module 'mdast' { @@ -25,14 +23,22 @@ declare module 'mdast' title?: string; fold?: boolean; } + interface Comment extends Parent + { + type: "comment"; + data?: CommentData | undefined; + } + interface CommentData extends Data {} interface PhrasingContentMap { tag: Tag; + comment: Comment; } interface RootContentMap { tag: Tag; callout: Callout; + comment: Comment; } } @@ -112,11 +118,6 @@ const calloutMapping: Record = { cite: "quote", } -function mdastToHtml(ast: any) { - const hast = toHast(ast, { allowDangerousHtml: true })!; - return toHtml(hast, { allowDangerousHtml: true }); -} - function canonicalizeCallout(calloutName: string): keyof typeof callouts { let callout = calloutName.toLowerCase() as keyof typeof calloutMapping return calloutMapping[callout] ?? "note" @@ -194,11 +195,19 @@ export default function ofm() { replacements.push([ commentRegex, (_value: string, ...capture: string[]) => { - const [inner] = capture - return { - type: "html", - value: `${inner}`, - } + const [inner] = capture; + + const comment: Comment = { + type: 'comment', + children: [{ type: 'text', value: inner, }], + data: { + hName: 'small', + hChildren: [ + { type: 'text', value: inner }, + ], + }, + }; + return comment; }, ]); @@ -227,27 +236,6 @@ export default function ofm() { } as Tag; }, ]); - - /*visit(tree, "html", (node) => { - for (const [regex, replace] of replacements) { - if (typeof replace === "string") { - node.value = node.value.replace(regex, replace) - } else { - node.value = node.value.replaceAll(regex, (substring: string, ...args: any) => { - const replaceValue = replace!(substring, ...args) - if (typeof replaceValue === "string") { - return replaceValue - } else if (Array.isArray(replaceValue)) { - return replaceValue.map(mdastToHtml).join("") - } else if (typeof replaceValue === "object" && replaceValue !== null) { - return mdastToHtml(replaceValue) - } else { - return substring - } - }) - } - } - })*/ mdastFindReplace(tree, replacements);