Compare commits
No commits in common. "02d116c89322cf08813a1a4736b130f249f250e3" and "721f362de35c1e1e91da97b420bb3c0290487742" have entirely different histories.
02d116c893
...
721f362de3
56
src/index.ts
56
src/index.ts
|
|
@ -1,8 +1,10 @@
|
||||||
import capitalize from "lodash.capitalize"
|
import capitalize from "lodash.capitalize"
|
||||||
import { visit } from "unist-util-visit"
|
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 { toString } from 'mdast-util-to-string';
|
||||||
import { FindAndReplaceList, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
|
import { FindAndReplaceList, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
|
||||||
import type { Comment, Root, Tag } from "mdast";
|
import type { Callout, Root, Tag } from "mdast";
|
||||||
|
|
||||||
declare module 'mdast'
|
declare module 'mdast'
|
||||||
{
|
{
|
||||||
|
|
@ -23,22 +25,14 @@ declare module 'mdast'
|
||||||
title?: string;
|
title?: string;
|
||||||
fold?: boolean;
|
fold?: boolean;
|
||||||
}
|
}
|
||||||
interface Comment extends Parent
|
|
||||||
{
|
|
||||||
type: "comment";
|
|
||||||
data?: CommentData | undefined;
|
|
||||||
}
|
|
||||||
interface CommentData extends Data {}
|
|
||||||
interface PhrasingContentMap
|
interface PhrasingContentMap
|
||||||
{
|
{
|
||||||
tag: Tag;
|
tag: Tag;
|
||||||
comment: Comment;
|
|
||||||
}
|
}
|
||||||
interface RootContentMap
|
interface RootContentMap
|
||||||
{
|
{
|
||||||
tag: Tag;
|
tag: Tag;
|
||||||
callout: Callout;
|
callout: Callout;
|
||||||
comment: Comment;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,6 +112,11 @@ const calloutMapping: Record<string, keyof typeof callouts> = {
|
||||||
cite: "quote",
|
cite: "quote",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mdastToHtml(ast: any) {
|
||||||
|
const hast = toHast(ast, { allowDangerousHtml: true })!;
|
||||||
|
return toHtml(hast, { allowDangerousHtml: true });
|
||||||
|
}
|
||||||
|
|
||||||
function canonicalizeCallout(calloutName: string): keyof typeof callouts {
|
function canonicalizeCallout(calloutName: string): keyof typeof callouts {
|
||||||
let callout = calloutName.toLowerCase() as keyof typeof calloutMapping
|
let callout = calloutName.toLowerCase() as keyof typeof calloutMapping
|
||||||
return calloutMapping[callout] ?? "note"
|
return calloutMapping[callout] ?? "note"
|
||||||
|
|
@ -195,19 +194,11 @@ export default function ofm() {
|
||||||
replacements.push([
|
replacements.push([
|
||||||
commentRegex,
|
commentRegex,
|
||||||
(_value: string, ...capture: string[]) => {
|
(_value: string, ...capture: string[]) => {
|
||||||
const [inner] = capture;
|
const [inner] = capture
|
||||||
|
return {
|
||||||
const comment: Comment = {
|
type: "html",
|
||||||
type: 'comment',
|
value: `<span class="text-comment">${inner}</span>`,
|
||||||
children: [{ type: 'text', value: inner, }],
|
}
|
||||||
data: {
|
|
||||||
hName: 'small',
|
|
||||||
hChildren: [
|
|
||||||
{ type: 'text', value: inner },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
return comment;
|
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
@ -236,6 +227,27 @@ export default function ofm() {
|
||||||
} as Tag;
|
} 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);
|
mdastFindReplace(tree, replacements);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue