mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-30 16:24:06 -06:00
Obsidian Parser (Tags)
This commit is contained in:
parent
318a13c4a3
commit
7790db45c4
@ -4,4 +4,5 @@ export { ObsidianCheckboxes } from "./checkboxes"
|
|||||||
export { ObsidianComments } from "./comments"
|
export { ObsidianComments } from "./comments"
|
||||||
export { ObsidianHighlights } from "./highlights"
|
export { ObsidianHighlights } from "./highlights"
|
||||||
export { ObsidianMermaid } from "./mermaid"
|
export { ObsidianMermaid } from "./mermaid"
|
||||||
|
export { ObsidianTags } from "./tags"
|
||||||
export { ObsidianWikilinks } from "./wikilinks"
|
export { ObsidianWikilinks } from "./wikilinks"
|
||||||
|
|||||||
@ -0,0 +1,82 @@
|
|||||||
|
import { QuartzParser } from "../../types"
|
||||||
|
import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
|
||||||
|
import { FilePath, pathToRoot, slugTag, slugifyFilePath } from "../../../util/path"
|
||||||
|
import { JSResource } from "../../../util/resources"
|
||||||
|
import { Root } from "mdast"
|
||||||
|
import { Pluggable } from "unified"
|
||||||
|
|
||||||
|
interface Options {
|
||||||
|
enabled: Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultOptions: Options = {
|
||||||
|
enabled: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// (?:^| ) -> non-capturing group, tag should start be separated by a space or be the start of the line
|
||||||
|
// #(...) -> capturing group, tag itself must start with #
|
||||||
|
// (?:[-_\p{L}\d\p{Z}])+ -> non-capturing group, non-empty string of (Unicode-aware) alpha-numeric characters and symbols, hyphens and/or underscores
|
||||||
|
// (?:\/[-_\p{L}\d\p{Z}]+)*) -> non-capturing group, matches an arbitrary number of tag strings separated by "/"
|
||||||
|
const tagRegex = new RegExp(
|
||||||
|
/(?:^| )#((?:[-_\p{L}\p{Emoji}\p{M}\d])+(?:\/[-_\p{L}\p{Emoji}\p{M}\d]+)*)/gu,
|
||||||
|
)
|
||||||
|
|
||||||
|
export const ObsidianTags: QuartzParser<Partial<Options>> = (userOpts) => {
|
||||||
|
const opts: Options = { ...defaultOptions, ...userOpts }
|
||||||
|
return {
|
||||||
|
name: "ObsidianTags",
|
||||||
|
textTransform(_ctx, src: string | Buffer) {
|
||||||
|
if (src instanceof Buffer) {
|
||||||
|
src = src.toString()
|
||||||
|
}
|
||||||
|
return src
|
||||||
|
},
|
||||||
|
markdownPlugins(_ctx) {
|
||||||
|
const plug: Pluggable = (tree: Root, file) => {
|
||||||
|
const base = pathToRoot(file.data.slug!)
|
||||||
|
const replacements: [RegExp, string | ReplaceFunction][] = []
|
||||||
|
replacements.push([
|
||||||
|
tagRegex,
|
||||||
|
(_value: string, tag: string) => {
|
||||||
|
// Check if the tag only includes numbers and slashes
|
||||||
|
if (/^[\/\d]+$/.test(tag)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
tag = slugTag(tag)
|
||||||
|
if (file.data.frontmatter) {
|
||||||
|
const noteTags = file.data.frontmatter.tags ?? []
|
||||||
|
file.data.frontmatter.tags = [...new Set([...noteTags, tag])]
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: "link",
|
||||||
|
url: base + `/tags/${tag}`,
|
||||||
|
data: {
|
||||||
|
hProperties: {
|
||||||
|
className: ["tag-link"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
value: tag,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
])
|
||||||
|
mdastFindReplace(tree, replacements)
|
||||||
|
}
|
||||||
|
return plug
|
||||||
|
},
|
||||||
|
htmlPlugins() {
|
||||||
|
const plug: Pluggable = () => {}
|
||||||
|
return plug
|
||||||
|
},
|
||||||
|
externalResources() {
|
||||||
|
const js = {} as JSResource
|
||||||
|
return js
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -44,6 +44,7 @@ import {
|
|||||||
ObsidianComments,
|
ObsidianComments,
|
||||||
ObsidianHighlights,
|
ObsidianHighlights,
|
||||||
ObsidianMermaid,
|
ObsidianMermaid,
|
||||||
|
ObsidianTags,
|
||||||
ObsidianWikilinks,
|
ObsidianWikilinks,
|
||||||
} from "../parsers/obsidian"
|
} from "../parsers/obsidian"
|
||||||
|
|
||||||
@ -267,6 +268,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<ObsidianO
|
|||||||
plugins.push(ObsidianWikilinks({ enabled: opts.wikilinks }).markdownPlugins(ctx))
|
plugins.push(ObsidianWikilinks({ enabled: opts.wikilinks }).markdownPlugins(ctx))
|
||||||
plugins.push(ObsidianHighlights({ enabled: opts.highlight }).markdownPlugins(ctx))
|
plugins.push(ObsidianHighlights({ enabled: opts.highlight }).markdownPlugins(ctx))
|
||||||
plugins.push(ObsidianArrow({ enabled: opts.parseArrows }).markdownPlugins(ctx))
|
plugins.push(ObsidianArrow({ enabled: opts.parseArrows }).markdownPlugins(ctx))
|
||||||
|
plugins.push(ObsidianTags({ enabled: opts.parseTags }).markdownPlugins(ctx))
|
||||||
plugins.push(ObsidianCallouts({ enabled: opts.callouts }).markdownPlugins(ctx))
|
plugins.push(ObsidianCallouts({ enabled: opts.callouts }).markdownPlugins(ctx))
|
||||||
plugins.push(ObsidianMermaid({ enabled: opts.mermaid }).markdownPlugins(ctx))
|
plugins.push(ObsidianMermaid({ enabled: opts.mermaid }).markdownPlugins(ctx))
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user