From 233d4b2f2c7dda62355382fb02760822c01e9ca2 Mon Sep 17 00:00:00 2001
From: Hydrophobefireman <bhavesh.pareek36@gmail.com>
Date: Thu, 28 Dec 2023 10:20:07 +0000
Subject: [PATCH] fix: fix invalid html output (#642)
---
quartz/plugins/transformers/ofm.ts | 41 ++++++++++++++++++++++-------------------
1 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index 9adeff4..f8a8393 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -1,5 +1,5 @@
import { QuartzTransformerPlugin } from "../types"
-import { Root, Html, BlockContent, DefinitionContent, Paragraph } 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"
@@ -105,6 +105,8 @@
return calloutMapping[callout] ?? "note"
}
+export const externalLinkRegex = /^https?:\/\//i
+
// !? -> optional embedding
// \[\[ -> open brace
// ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name)
@@ -158,13 +160,19 @@
}
src = src.replaceAll(wikilinkRegex, (value, ...capture) => {
- const [rawFp, rawHeader, rawAlias] = capture
+ const [rawFp, rawHeader, rawAlias]: (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("#", "|") ?? ""
const embedDisplay = value.startsWith("!") ? "!" : ""
+
+ if (rawFp?.match(externalLinkRegex)) {
+ return `${embedDisplay}[${displayAlias.replace(/^\|/, "")}](${rawFp})`
+ }
+
return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]`
})
}
@@ -423,25 +431,15 @@
})
}
- return plugins
- },
- htmlPlugins() {
- const plugins: PluggableList = [rehypeRaw]
-
if (opts.mermaid) {
plugins.push(() => {
- return (tree: HtmlRoot, _file) => {
- visit(tree, "element", (node) => {
- if (node.tagName === "pre") {
- const firstChild = node.children[0]
- if (firstChild && firstChild.type === "element" && firstChild.tagName === "code") {
- const code = firstChild
- const isMermaidBlock =
- (code.properties["className"] as Array<string>)?.[0] === "language-mermaid"
- if (isMermaidBlock) {
- node.children = code.children
- node.properties.className = ["mermaid"]
- }
+ return (tree: Root, _file) => {
+ visit(tree, "code", (node: Code) => {
+ if (node.lang === "mermaid") {
+ node.data = {
+ hProperties: {
+ className: ["mermaid"],
+ },
}
}
})
@@ -449,6 +447,11 @@
})
}
+ return plugins
+ },
+ htmlPlugins() {
+ const plugins: PluggableList = [rehypeRaw]
+
if (opts.parseBlockReferences) {
plugins.push(() => {
const inlineTagTypes = new Set(["p", "li"])
--
Gitblit v1.10.0