Add "Tag" as a new mdast type.

This commit is contained in:
Peaceultime 2024-09-04 13:25:53 +02:00
parent f87f742547
commit f91c62fa06
2 changed files with 22 additions and 26 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -5,6 +5,23 @@ 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"
declare module 'mdast'
{
interface Tag extends Literal {
type: "tag";
data?: TagData | undefined;
}
interface TagData extends Data {}
interface PhrasingContentMap
{
tag: Tag;
}
interface RootContentMap
{
tag: Tag;
}
}
// !? -> optional embedding
// \[\[ -> open brace
// ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name)
@ -179,35 +196,14 @@ export default function ofm() {
return false
}
tag = slugTag(tag)
if (!file.data)
file.data = {};
if (!file.data.frontmatter)
file.data.frontmatter = {};
if (!file.data.frontmatter.tags)
file.data.frontmatter.tags = [];
if (file.data.frontmatter && !file.data.frontmatter.tags.includes(tag)) {
file.data.frontmatter.tags.push(tag)
}
tag = slugTag(tag);
return {
type: "link",
url: `/tags#${tag}`,
type: "tag",
value: tag,
data: {
hProperties: {
className: ["tag"],
},
},
children: [
{
type: "text",
value: `#${tag}`,
},
],
hName: 'tag',
}
}
},
])