From 5dcb7e83fc3c8383ebbc84aac4553df4ad3ef59a Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Fri, 15 Sep 2023 16:46:06 +0000
Subject: [PATCH] fix: use git dates by default, @napi/git is fast enough
---
quartz/plugins/transformers/ofm.ts | 104 ++++++++++++++++++++++++++++++++++-----------------
1 files changed, 69 insertions(+), 35 deletions(-)
diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index b2f1dba..8306f40 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -1,7 +1,7 @@
import { PluggableList } from "unified"
import { QuartzTransformerPlugin } from "../types"
import { Root, HTML, BlockContent, DefinitionContent, Code, Paragraph } from "mdast"
-import { Element, Literal } from 'hast'
+import { Element, Literal } from "hast"
import { Replace, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
import { slug as slugAnchor } from "github-slugger"
import rehypeRaw from "rehype-raw"
@@ -135,31 +135,32 @@
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) {
- visit(tree, "html", (node: HTML) => {
- if (typeof replace === "string") {
- node.value = node.value.replace(regex, replace)
- } else {
- node.value = node.value.replaceAll(regex, (substring: string, ...args) => {
- 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
- }
- })
- }
- })
- }
+ if (replace) {
+ visit(tree, "html", (node: HTML) => {
+ if (typeof replace === "string") {
+ node.value = node.value.replace(regex, replace)
+ } else {
+ node.value = node.value.replaceAll(regex, (substring: string, ...args) => {
+ 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, regex, replace)
- }
+ mdastFindReplace(tree, regex, replace)
+ }
: mdastFindReplace
return {
@@ -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
}
@@ -357,8 +366,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,
},
@@ -421,22 +431,47 @@
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)) {
+
+ 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) {
+ 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,
+ }
+ file.data.blocks![block] = node
+ }
+ }
+ }
+ }
+ } else if (inlineTagTypes.has(node.tagName)) {
const last = node.children.at(-1) as Literal
- if (last.value && typeof last.value === 'string') {
+ 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)
- node.properties = {
- ...node.properties,
- id: block
+
+ if (!Object.keys(file.data.blocks!).includes(block)) {
+ node.properties = {
+ ...node.properties,
+ id: block,
+ }
+ file.data.blocks![block] = node
}
- file.data.blocks![block] = node
}
}
}
@@ -490,4 +525,3 @@
blocks: Record<string, Element>
}
}
-
--
Gitblit v1.10.0