mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-24 05:14:06 -06:00
Block multi-line code blocks from being caught
This prevents multi-line code blocks containing a badge definition to be replaced with badge HTML which is certainly unintended.
This commit is contained in:
parent
1d38be71ac
commit
d45b010ce2
@ -94,8 +94,11 @@ export const BADGE_TYPES: any[] = [
|
|||||||
|
|
||||||
var allBadges: any[] = BADGE_TYPES //Append custom badges to the end of this array.
|
var allBadges: any[] = BADGE_TYPES //Append custom badges to the end of this array.
|
||||||
|
|
||||||
const REGEXP = /\[!!([^\]]+)\]/gm
|
// Catches all badge blocks with syntax `[!!...]` but not `[!!]
|
||||||
const CODEREGEX = /`([^`\n]+)`/g
|
const REGEXP = /\`\[!!([^\]]+)\]\`/gm
|
||||||
|
|
||||||
|
// Catches all multiline code blocks
|
||||||
|
const FENCED_CODE_REGEX = /(```[\s\S]*?```|~~~[\s\S]*?~~~)/gm
|
||||||
|
|
||||||
export interface Options {
|
export interface Options {
|
||||||
customBadges: Array<{
|
customBadges: Array<{
|
||||||
@ -121,12 +124,31 @@ export const InlineBadges: QuartzTransformerPlugin<Partial<Options>> = (userOpts
|
|||||||
allBadges.push([badge.name, badge.name, badge.icon]) // Pushes it to the array in the format.
|
allBadges.push([badge.name, badge.name, badge.icon]) // Pushes it to the array in the format.
|
||||||
}
|
}
|
||||||
|
|
||||||
var srcReplacement: string = src //Start by assuming there are no badges.
|
// Required later to ensure we don't change badges that are within multiline code blocks
|
||||||
for (const match of src.matchAll(CODEREGEX)) {
|
const fencedCodeBlocks: Array<[number, number]> = []
|
||||||
for (const badgeMatch of match[0].matchAll(REGEXP)) {
|
for (const match of src.matchAll(FENCED_CODE_REGEX)) {
|
||||||
srcReplacement = srcReplacement.replace(match[0], buildBadge(badgeMatch[0]))
|
fencedCodeBlocks.push([match.index!, match.index! + match[0].length])
|
||||||
|
}
|
||||||
|
// Helper to check if a given index is inside any code block
|
||||||
|
function isInFencedBlock(idx: number) {
|
||||||
|
return fencedCodeBlocks.some(([start, end]) => idx >= start && idx < end)
|
||||||
|
}
|
||||||
|
|
||||||
|
let lastIndex = 0 //Last code block that was checked
|
||||||
|
let srcReplacement = "" //Build the source text from this.
|
||||||
|
|
||||||
|
for (const match of src.matchAll(REGEXP)) {
|
||||||
|
const matchIndex = match.index!
|
||||||
|
const matchEnd = matchIndex + match[0].length
|
||||||
|
|
||||||
|
if (!isInFencedBlock(matchIndex)) {
|
||||||
|
srcReplacement += src.slice(lastIndex, matchIndex)
|
||||||
|
srcReplacement += buildBadge(match[0].replaceAll("`", "")) //Get rid of trailing code block syntax.
|
||||||
|
lastIndex = matchEnd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
srcReplacement += src.slice(lastIndex)
|
||||||
return srcReplacement
|
return srcReplacement
|
||||||
},
|
},
|
||||||
externalResources() {
|
externalResources() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user