Jacky Zhao
2023-09-13 cce389c81d262d1d2a2bd8140c879efd68e3c6dd
quartz/plugins/transformers/ofm.ts
@@ -135,6 +135,7 @@
    const hast = toHast(ast, { allowDangerousHtml: true })!
    return toHtml(hast, { allowDangerousHtml: true })
  }
  const findAndReplace = opts.enableInHtmlEmbed
    ? (tree: Root, regex: RegExp, replace?: Replace | null | undefined) => {
        if (replace) {
@@ -238,8 +239,16 @@
                    value: `<iframe src="${url}"></iframe>`,
                  }
                } else if (ext === "") {
                  // TODO: note embed
                  const block = anchor.slice(1)
                  return {
                    type: "html",
                    data: { hProperties: { transclude: true } },
                    value: `<blockquote class="transclude" data-url="${url}" data-block="${block}"><a href="${
                      url + anchor
                    }" class="transclude-inner">Transclude of block ${block}</a></blockquote>`,
                }
                }
                // otherwise, fall through to regular link
              }
@@ -422,17 +431,23 @@
      if (opts.parseBlockReferences) {
        plugins.push(() => {
          const inlineTagTypes = new Set(["p", "li"])
          const blockTagTypes = new Set(["blockquote"])
          return (tree, file) => {
            file.data.blocks = {}
            const validTagTypes = new Set(["blockquote", "p", "li"])
            visit(tree, "element", (node, _index, _parent) => {
              if (validTagTypes.has(node.tagName)) {
                const last = node.children.at(-1) as Literal
                if (last.value && typeof last.value === "string") {
                  const matches = last.value.match(blockReferenceRegex)
            visit(tree, "element", (node, index, parent) => {
              if (blockTagTypes.has(node.tagName)) {
                const nextChild = parent?.children.at(index! + 2) as Element
                if (nextChild && nextChild.tagName === "p") {
                  const text = nextChild.children.at(0) as Literal
                  if (text && text.value && text.type === "text") {
                    const matches = text.value.match(blockReferenceRegex)
                  if (matches && matches.length >= 1) {
                    last.value = last.value.slice(0, -matches[0].length)
                      parent!.children.splice(index! + 2, 1)
                    const block = matches[0].slice(1)
                      if (!Object.keys(file.data.blocks!).includes(block)) {
                    node.properties = {
                      ...node.properties,
                      id: block,
@@ -441,6 +456,25 @@
                  }
                }
              }
                }
              } else if (inlineTagTypes.has(node.tagName)) {
                const last = node.children.at(-1) as Literal
                if (last && last.value && typeof last.value === "string") {
                  const matches = last.value.match(blockReferenceRegex)
                  if (matches && matches.length >= 1) {
                    last.value = last.value.slice(0, -matches[0].length)
                    const block = matches[0].slice(1)
                    if (!Object.keys(file.data.blocks!).includes(block)) {
                      node.properties = {
                        ...node.properties,
                        id: block,
                      }
                      file.data.blocks![block] = node
                    }
                  }
                }
              }
            })
          }
        })