Compare commits

..

3 Commits

Author SHA1 Message Date
Clément Pons
8e7ad4fd09 Add label to links 2025-10-07 11:46:01 +02:00
Clément Pons
02d116c893 Merge branch 'master' of https://git.peaceultime.com/peaceultime/remark-ofm 2025-04-22 11:33:33 +02:00
Clément Pons
ea9a70b191 Add comments 2025-04-22 11:33:32 +02:00
3 changed files with 30 additions and 37 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -18,18 +18,18 @@
"license": "GPL-3.0",
"dependencies": {
"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"
},
"devDependencies": {
"@types/lodash.capitalize": "^4.2.9",
"@types/unist": "^2.0.10",
"@types/unist": "^2.0.11",
"mdast-util-to-string": "^4.0.0",
"rehype-raw": "^6.1.1",
"rehype-stringify": "^9.0.4",
"remark-parse": "^10.0.2",
"remark-rehype": "^10.1.0",
"typescript": "^5.5.4",
"typescript": "^5.8.3",
"unified": "^10.1.2"
}
}

View File

@@ -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"
@@ -170,6 +171,11 @@ export default function ofm() {
return {
type: "link",
url,
data: {
hProperties: {
label: alias ?? fp,
}
},
children: [
{
type: "text",
@@ -194,11 +200,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;
},
]);
@@ -227,27 +241,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);