Add comments
This commit is contained in:
parent
cccfccc299
commit
ea9a70b191
56
src/index.ts
56
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<string, keyof typeof callouts> = {
|
|||
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: `<span class="text-comment">${inner}</span>`,
|
||||
}
|
||||
const [inner] = capture;
|
||||
|
||||
const comment: Comment = {
|
||||
type: 'comment',
|
||||
children: [{ type: 'text', value: inner, }],
|
||||
data: {
|
||||
hName: 'small',
|
||||
hChildren: [
|
||||
{ type: 'text', value: inner },
|
||||
],
|
||||
},
|
||||
};
|
||||
return comment;
|
||||
},
|
||||
]);
|
||||
|
||||
|
|
@ -228,27 +237,6 @@ export default function ofm() {
|
|||
},
|
||||
]);
|
||||
|
||||
/*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);
|
||||
|
||||
visit(tree, "blockquote", (node, index, parent) => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue