mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-05-19 06:54:18 +02:00
use regex
This commit is contained in:
parent
781352f01a
commit
bd83712be5
@ -7,7 +7,6 @@ import {
|
|||||||
DefinitionContent,
|
DefinitionContent,
|
||||||
Paragraph,
|
Paragraph,
|
||||||
Code,
|
Code,
|
||||||
FootnoteDefinition,
|
|
||||||
} from "mdast"
|
} from "mdast"
|
||||||
import { Element, Literal, Root as HtmlRoot } from "hast"
|
import { Element, Literal, Root as HtmlRoot } from "hast"
|
||||||
import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
|
import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
|
||||||
@ -146,21 +145,7 @@ const wikilinkImageEmbedRegex = new RegExp(
|
|||||||
/^(?<alt>(?!^\d*x?\d*$).*?)?(\|?\s*?(?<width>\d+)(x(?<height>\d+))?)?$/,
|
/^(?<alt>(?!^\d*x?\d*$).*?)?(\|?\s*?(?<width>\d+)(x(?<height>\d+))?)?$/,
|
||||||
)
|
)
|
||||||
|
|
||||||
const inlineFootnoteRegex = /\^\[([\s\S]*?)\]/g
|
const inlineFootnoteRegex = /\^\[((?:[^\[\]]|\[(?:[^\[\]]|\[[^\[\]]*\])*\])*)\]/g
|
||||||
|
|
||||||
// Helper function to match balanced brackets
|
|
||||||
function matchBalancedBrackets(text: string, startIndex: number): number {
|
|
||||||
let depth = 1;
|
|
||||||
let i = startIndex;
|
|
||||||
|
|
||||||
while (i < text.length && depth > 0) {
|
|
||||||
if (text[i] === '[') depth++;
|
|
||||||
if (text[i] === ']') depth--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return depth === 0 ? i - 1 : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>> = (userOpts) => {
|
export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>> = (userOpts) => {
|
||||||
const opts = { ...defaultOptions, ...userOpts }
|
const opts = { ...defaultOptions, ...userOpts }
|
||||||
@ -233,46 +218,21 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (opts.inlineFootnotes) {
|
if (opts.inlineFootnotes) {
|
||||||
let footnoteCounter = 1
|
// Replaces ^[inline] footnotes with regular footnotes [^1]:
|
||||||
const footnotes: Record<string, string> = {}
|
const footnotes: Record<string, string> = {}
|
||||||
let result = ""
|
|
||||||
let currentIndex = 0
|
// Replace inline footnotes with references and collect definitions
|
||||||
|
const result = src.replace(inlineFootnoteRegex, (_match, content) => {
|
||||||
|
const id = `inline-${Math.random().toString(36).substring(2, 8)}`
|
||||||
|
footnotes[id] = content.trim()
|
||||||
|
return `[^${id}]`
|
||||||
|
})
|
||||||
|
|
||||||
while (true) {
|
// Append footnote definitions if we found any
|
||||||
// Find next inline footnote start
|
|
||||||
const startMatch = src.indexOf("^[", currentIndex)
|
|
||||||
if (startMatch === -1) break
|
|
||||||
|
|
||||||
// Add text before the footnote
|
|
||||||
result += src.slice(currentIndex, startMatch)
|
|
||||||
|
|
||||||
// Find the matching closing bracket
|
|
||||||
const contentStart = startMatch + 2
|
|
||||||
const contentEnd = matchBalancedBrackets(src, contentStart)
|
|
||||||
|
|
||||||
if (contentEnd === -1) {
|
|
||||||
// No matching bracket found, treat as normal text
|
|
||||||
result += src.slice(startMatch, contentStart)
|
|
||||||
currentIndex = contentStart
|
|
||||||
} else {
|
|
||||||
// Extract footnote content
|
|
||||||
const content = src.slice(contentStart, contentEnd)
|
|
||||||
const id = `inline${Math.random().toString(36).substring(2, 8)}`
|
|
||||||
footnotes[id] = content.trim()
|
|
||||||
result += `[^${id}]`
|
|
||||||
currentIndex = contentEnd + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add remaining text
|
|
||||||
result += src.slice(currentIndex)
|
|
||||||
|
|
||||||
// Append footnote definitions
|
|
||||||
if (Object.keys(footnotes).length > 0) {
|
if (Object.keys(footnotes).length > 0) {
|
||||||
result += "\n\n"
|
return result + "\n\n" + Object.entries(footnotes)
|
||||||
Object.entries(footnotes).forEach(([id, content]) => {
|
.map(([id, content]) => `[^${id}]: ${content}`)
|
||||||
result += `[^${id}]: ${content}\n`
|
.join("\n") + "\n"
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
Loading…
x
Reference in New Issue
Block a user