mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-27 23:04:05 -06:00
Comment regex (fixes https://github.com/jackyzha0/quartz/issues/1114)
This commit is contained in:
parent
f5ac658376
commit
f64e277ca2
@ -59,7 +59,8 @@ const config: QuartzConfig = {
|
||||
},
|
||||
},
|
||||
plugins: {
|
||||
transformers: Presets.DefaultPreset(),
|
||||
//transformers: Presets.DefaultPreset(),
|
||||
transformers: Presets.ObsidianPreset(),
|
||||
filters: [Plugin.RemoveDrafts()],
|
||||
emitters: [
|
||||
Plugin.AliasRedirects(),
|
||||
|
||||
@ -7,7 +7,7 @@ import * as Resources from "../resources"
|
||||
export const DefaultPreset: QuartzTransformerPlugin = () => {
|
||||
return {
|
||||
textTransformers: [
|
||||
Text.ObsidianFlavoredMarkdownComments(),
|
||||
Text.HtmlComments(),
|
||||
Text.ObsidianFlavoredMarkdownCallouts(),
|
||||
Text.ObsidianFlavoredMarkdownWikilinks(),
|
||||
],
|
||||
|
||||
@ -7,6 +7,7 @@ import * as Resources from "../resources"
|
||||
export const ObsidianPreset: QuartzTransformerPlugin = () => {
|
||||
return {
|
||||
textTransformers: [
|
||||
Text.HtmlComments(),
|
||||
Text.ObsidianFlavoredMarkdownComments(),
|
||||
Text.ObsidianFlavoredMarkdownCallouts(),
|
||||
Text.ObsidianFlavoredMarkdownWikilinks(),
|
||||
|
||||
28
quartz/plugins/transformers/text/htmlComments.ts
Normal file
28
quartz/plugins/transformers/text/htmlComments.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { TextTransformerPlugin } from "../../types"
|
||||
|
||||
const commentRegex = new RegExp(/<!--.*?-->/gms)
|
||||
const codeBlockRegex = new RegExp(/(```.*?```)/gms)
|
||||
|
||||
export const HtmlComments: TextTransformerPlugin = () => {
|
||||
return {
|
||||
name: "ObsidianFlavoredMarkdownComments",
|
||||
transformation(_ctx, src) {
|
||||
// do comments at text level
|
||||
if (src instanceof Buffer) {
|
||||
src = src.toString()
|
||||
} // capture all codeblocks before parsing comments
|
||||
const codeBlocks = Array.from(src.matchAll(codeBlockRegex), (x) => x[1].toString())
|
||||
|
||||
src = src.replaceAll(codeBlockRegex, "###codeblockplaceholder###")
|
||||
|
||||
src = src.replaceAll(commentRegex, "")
|
||||
|
||||
// Restore codeblocks
|
||||
codeBlocks.forEach((codeblock) => {
|
||||
src = src.replace("###codeblockplaceholder###", codeblock)
|
||||
})
|
||||
|
||||
return src
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
export { HtmlComments } from "./htmlComments"
|
||||
export { ObsidianFlavoredMarkdownCallouts } from "./ofmCallouts"
|
||||
export { ObsidianFlavoredMarkdownComments } from "./ofmComments"
|
||||
export { ObsidianFlavoredMarkdownWikilinks } from "./ofmWikilinks"
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { TextTransformerPlugin } from "../../types"
|
||||
|
||||
const commentRegex = new RegExp(/%%[\s\S]*?%%/g)
|
||||
const commentRegex = new RegExp(/%%.*?%%/gms)
|
||||
const codeBlockRegex = new RegExp(/(```.*?```)/gms)
|
||||
|
||||
export const ObsidianFlavoredMarkdownComments: TextTransformerPlugin = () => {
|
||||
return {
|
||||
@ -11,7 +12,17 @@ export const ObsidianFlavoredMarkdownComments: TextTransformerPlugin = () => {
|
||||
src = src.toString()
|
||||
}
|
||||
|
||||
src = src.replace(commentRegex, "")
|
||||
// capture all codeblocks before parsing comments
|
||||
const codeBlocks = Array.from(src.matchAll(codeBlockRegex), (x) => x[1].toString())
|
||||
|
||||
src = src.replaceAll(codeBlockRegex, "###codeblockplaceholder###")
|
||||
|
||||
src = src.replaceAll(commentRegex, "")
|
||||
|
||||
// Restore codeblocks
|
||||
codeBlocks.forEach((codeblock) => {
|
||||
src = src.replace("###codeblockplaceholder###", codeblock)
|
||||
})
|
||||
|
||||
return src
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user