Adam Brangenberg
2023-07-31 cbae88fc4e9b98764cfccca2e85f265c4b894573
quartz/plugins/transformers/ofm.ts
@@ -9,7 +9,7 @@
import { JSResource } from "../../resources"
// @ts-ignore
import calloutScript from "../../components/scripts/callout.inline.ts"
import { FilePath, canonicalizeServer, pathToRoot, slugifyFilePath } from "../../path"
import { FilePath, canonicalizeServer, pathToRoot, slugTag, slugifyFilePath } from "../../path"
export interface Options {
  comments: boolean
@@ -110,7 +110,7 @@
// from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
const calloutRegex = new RegExp(/^\[\!(\w+)\]([+-]?)/)
// (?:^| )   -> non-capturing group, tag should start be separated by a space or be the start of the line
// #(\w+)    -> tag itself is # followed by a string of alpha-numeric characters
// #(\w+)    -> tag itself is # followed by a string of alpha-numeric characters
const tagRegex = new RegExp(/(?:^| )#(\w+)/, "g")
export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options> | undefined> = (
@@ -119,7 +119,7 @@
  const opts = { ...defaultOptions, ...userOpts }
  return {
    name: "ObsidianFlavoredMarkdown",
    textTransform(src) {
    textTransform(_ctx, src) {
      // pre-transform wikilinks (fix anchors to things that may contain illegal syntax e.g. codeblocks, latex)
      if (opts.wikilinks) {
        src = src.toString()
@@ -225,7 +225,7 @@
            findAndReplace(tree, commentRegex, (_value: string, ..._capture: string[]) => {
              return {
                type: "text",
                value: ""
                value: "",
              }
            })
          }
@@ -296,8 +296,9 @@
                node.data = {
                  hProperties: {
                    ...(node.data?.hProperties ?? {}),
                    className: `callout ${collapse ? "is-collapsible" : ""} ${defaultState === "collapsed" ? "is-collapsed" : ""
                      }`,
                    className: `callout ${collapse ? "is-collapsible" : ""} ${
                      defaultState === "collapsed" ? "is-collapsed" : ""
                    }`,
                    "data-callout": calloutType,
                    "data-callout-fold": collapse,
                  },
@@ -330,9 +331,13 @@
            const slug = canonicalizeServer(file.data.slug!)
            const base = pathToRoot(slug)
            findAndReplace(tree, tagRegex, (value: string, tag: string) => {
              if (file.data.frontmatter) {
                file.data.frontmatter.tags.push(tag)
              }
              return {
                type: "link",
                url: base + `/tags/${slugAnchor(tag)}`,
                url: base + `/tags/${slugTag(tag)}`,
                data: {
                  hProperties: {
                    className: ["tag-link"],