From daa8796554dea41d6fbf81f4eccea58153a4e850 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Mon, 18 Mar 2024 01:15:42 +0000
Subject: [PATCH] fix: format
---
quartz/plugins/transformers/ofm.ts | 92 ++++++++++++++++++++++++++++++++++------------
1 files changed, 68 insertions(+), 24 deletions(-)
diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index 89b297b..50371c8 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -1,5 +1,5 @@
import { QuartzTransformerPlugin } from "../types"
-import { Blockquote, Root, Html, BlockContent, DefinitionContent, Paragraph, Code } from "mdast"
+import { Root, Html, BlockContent, DefinitionContent, Paragraph, Code } from "mdast"
import { Element, Literal, Root as HtmlRoot } from "hast"
import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
import { slug as slugAnchor } from "github-slugger"
@@ -17,7 +17,6 @@
import { PhrasingContent } from "mdast-util-find-and-replace/lib"
import { capitalize } from "../../util/lang"
import { PluggableList } from "unified"
-import { ValidCallout, i18n } from "../../i18n"
export interface Options {
comments: boolean
@@ -100,13 +99,15 @@
export const arrowRegex = new RegExp(/(-{1,2}>|={1,2}>|<-{1,2}|<={1,2})/, "g")
+// (\|[^\|\[\n]*)? -> optional check if wikilink is inside a table cell
// !? -> optional embedding
// \[\[ -> open brace
// ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name)
// (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link)
-// (\|[^\[\]\#]+)? -> | then one or more non-special characters (alias)
+// (\|[^\[\]\#]+)? -> \| then one or more non-special characters (alias)
+// ([^\|\n]*\|)? -> optional check if wikilink is inside a table cell
export const wikilinkRegex = new RegExp(
- /!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\#]+)?\]\]/,
+ /(\|[^\|\[\n]*)?!?\[\[([^\[\]\|\#\\]+)?(#+[^\[\]\|\#\\]+)?(\\?\|[^\[\]\#]+)?\]\]([^\|\n]*\|)?/,
"g",
)
const highlightRegex = new RegExp(/==([^=]+)==/, "g")
@@ -118,9 +119,13 @@
// #(...) -> 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}\d])+(?:\/[-_\p{L}\p{Emoji}\d]+)*)/, "gu")
+const tagRegex = new RegExp(
+ /(?:^| )#((?:[-_\p{L}\p{Emoji}\p{M}\d])+(?:\/[-_\p{L}\p{Emoji}\p{M}\d]+)*)/,
+ "gu",
+)
const blockReferenceRegex = new RegExp(/\^([-_A-Za-z0-9]+)$/, "g")
const ytLinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
+const ytPlaylistLinkRegex = /[?&]list=([^#?&]*)/
const videoExtensionRegex = new RegExp(/\.(mp4|webm|ogg|avi|mov|flv|wmv|mkv|mpg|mpeg|3gp|m4v)$/)
const wikilinkImageEmbedRegex = new RegExp(
/^(?<alt>(?!^\d*x?\d*$).*?)?(\|?\s*?(?<width>\d+)(x(?<height>\d+))?)?$/,
@@ -167,28 +172,34 @@
}
src = src.replace(wikilinkRegex, (value, ...capture) => {
- const [rawFp, rawHeader, rawAlias]: (string | undefined)[] = capture
+ const [rawTablePre, rawFp, rawHeader, rawAlias, rawTablePost]: (string | undefined)[] =
+ capture
const fp = rawFp ?? ""
const anchor = rawHeader?.trim().replace(/^#+/, "")
const blockRef = Boolean(anchor?.startsWith("^")) ? "^" : ""
const displayAnchor = anchor ? `#${blockRef}${slugAnchor(anchor)}` : ""
- const displayAlias = rawAlias ?? rawHeader?.replace("#", "|") ?? ""
+ let displayAlias = rawAlias ?? rawHeader?.replace("#", "|") ?? ""
const embedDisplay = value.startsWith("!") ? "!" : ""
if (rawFp?.match(externalLinkRegex)) {
return `${embedDisplay}[${displayAlias.replace(/^\|/, "")}](${rawFp})`
}
+ // transform `[[note#^block_ref|^block_ref]]` to `[[note#^block_ref\|^block_ref]]`,
+ // when the wikilink with alias is inside a table.
+ if (displayAlias && displayAlias.startsWith("|") && rawTablePre && rawTablePost) {
+ displayAlias = `\\${displayAlias}`
+ }
+
return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]`
})
}
return src
},
- markdownPlugins(ctx) {
+ markdownPlugins(_ctx) {
const plugins: PluggableList = []
- const cfg = ctx.cfg.configuration
// regex replacements
plugins.push(() => {
@@ -200,7 +211,7 @@
replacements.push([
wikilinkRegex,
(value: string, ...capture: string[]) => {
- let [rawFp, rawHeader, rawAlias] = capture
+ let [_rawTablePre, rawFp, rawHeader, rawAlias, _rawTablePost] = capture
const fp = rawFp?.trim() ?? ""
const anchor = rawHeader?.trim() ?? ""
const alias = rawAlias?.slice(1).trim()
@@ -325,7 +336,7 @@
children: [
{
type: "text",
- value: `#${tag}`,
+ value: tag,
},
],
}
@@ -409,12 +420,7 @@
children: [
{
type: "text",
- value: useDefaultTitle
- ? capitalize(
- i18n(cfg.locale).components.callout[calloutType as ValidCallout] ??
- calloutType,
- )
- : titleContent + " ",
+ value: useDefaultTitle ? capitalize(typeString) : titleContent + " ",
},
...restOfTitle,
],
@@ -530,12 +536,35 @@
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,
+ if (last.value === "") {
+ // this is an inline block ref but the actual block
+ // is the previous element above it
+ let idx = (index ?? 1) - 1
+ while (idx >= 0) {
+ const element = parent?.children.at(idx)
+ if (!element) break
+ if (element.type !== "element") {
+ idx -= 1
+ } else {
+ if (!Object.keys(file.data.blocks!).includes(block)) {
+ element.properties = {
+ ...element.properties,
+ id: block,
+ }
+ file.data.blocks![block] = element
+ }
+ return
+ }
}
- file.data.blocks![block] = node
+ } else {
+ // normal paragraph transclude
+ if (!Object.keys(file.data.blocks!).includes(block)) {
+ node.properties = {
+ ...node.properties,
+ id: block,
+ }
+ file.data.blocks![block] = node
+ }
}
}
}
@@ -554,7 +583,9 @@
if (node.tagName === "img" && typeof node.properties.src === "string") {
const match = node.properties.src.match(ytLinkRegex)
const videoId = match && match[2].length == 11 ? match[2] : null
+ const playlistId = node.properties.src.match(ytPlaylistLinkRegex)?.[1]
if (videoId) {
+ // YouTube video (with optional playlist)
node.tagName = "iframe"
node.properties = {
class: "external-embed",
@@ -562,7 +593,20 @@
frameborder: 0,
width: "600px",
height: "350px",
- src: `https://www.youtube.com/embed/${videoId}`,
+ src: playlistId
+ ? `https://www.youtube.com/embed/${videoId}?list=${playlistId}`
+ : `https://www.youtube.com/embed/${videoId}`,
+ }
+ } else if (playlistId) {
+ // YouTube playlist only.
+ node.tagName = "iframe"
+ node.properties = {
+ class: "external-embed",
+ allow: "fullscreen",
+ frameborder: 0,
+ width: "600px",
+ height: "350px",
+ src: `https://www.youtube.com/embed/videoseries?list=${playlistId}`,
}
}
}
@@ -616,7 +660,7 @@
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
- mermaidImport ||= await import('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs')
+ mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
--
Gitblit v1.10.0