You've already forked remark-ofm
Compare commits
3 Commits
721f362de3
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e7ad4fd09 | ||
|
|
02d116c893 | ||
|
|
ea9a70b191 |
@@ -18,18 +18,18 @@
|
|||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"lodash.capitalize": "^4.2.1",
|
"lodash.capitalize": "^4.2.1",
|
||||||
"mdast-util-find-and-replace": "^3.0.1",
|
"mdast-util-find-and-replace": "^3.0.2",
|
||||||
"unist-util-visit": "^4.1.2"
|
"unist-util-visit": "^4.1.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/lodash.capitalize": "^4.2.9",
|
"@types/lodash.capitalize": "^4.2.9",
|
||||||
"@types/unist": "^2.0.10",
|
"@types/unist": "^2.0.11",
|
||||||
"mdast-util-to-string": "^4.0.0",
|
"mdast-util-to-string": "^4.0.0",
|
||||||
"rehype-raw": "^6.1.1",
|
"rehype-raw": "^6.1.1",
|
||||||
"rehype-stringify": "^9.0.4",
|
"rehype-stringify": "^9.0.4",
|
||||||
"remark-parse": "^10.0.2",
|
"remark-parse": "^10.0.2",
|
||||||
"remark-rehype": "^10.1.0",
|
"remark-rehype": "^10.1.0",
|
||||||
"typescript": "^5.5.4",
|
"typescript": "^5.8.3",
|
||||||
"unified": "^10.1.2"
|
"unified": "^10.1.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
61
src/index.ts
61
src/index.ts
@@ -1,10 +1,8 @@
|
|||||||
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 { Callout, Root, Tag } from "mdast";
|
import type { Comment, Root, Tag } from "mdast";
|
||||||
|
|
||||||
declare module 'mdast'
|
declare module 'mdast'
|
||||||
{
|
{
|
||||||
@@ -25,14 +23,22 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,11 +118,6 @@ 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"
|
||||||
@@ -170,6 +171,11 @@ export default function ofm() {
|
|||||||
return {
|
return {
|
||||||
type: "link",
|
type: "link",
|
||||||
url,
|
url,
|
||||||
|
data: {
|
||||||
|
hProperties: {
|
||||||
|
label: alias ?? fp,
|
||||||
|
}
|
||||||
|
},
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
type: "text",
|
type: "text",
|
||||||
@@ -194,11 +200,19 @@ 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 {
|
|
||||||
type: "html",
|
const comment: Comment = {
|
||||||
value: `<span class="text-comment">${inner}</span>`,
|
type: 'comment',
|
||||||
}
|
children: [{ type: 'text', value: inner, }],
|
||||||
|
data: {
|
||||||
|
hName: 'small',
|
||||||
|
hChildren: [
|
||||||
|
{ type: 'text', value: inner },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return comment;
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -228,27 +242,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);
|
mdastFindReplace(tree, replacements);
|
||||||
|
|
||||||
visit(tree, "blockquote", (node, index, parent) => {
|
visit(tree, "blockquote", (node, index, parent) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user