add counter, regex comments, change inlineFootnotes to enableInlineFootnotes

This commit is contained in:
sss 2025-03-03 07:06:49 +01:00
parent 7e04bb4e6e
commit df3cdb18b8

View File

@ -38,7 +38,7 @@ export interface Options {
enableYouTubeEmbed: boolean enableYouTubeEmbed: boolean
enableVideoEmbed: boolean enableVideoEmbed: boolean
enableCheckbox: boolean enableCheckbox: boolean
inlineFootnotes: boolean enableInlineFootnotes: boolean
} }
const defaultOptions: Options = { const defaultOptions: Options = {
@ -54,7 +54,7 @@ const defaultOptions: Options = {
enableYouTubeEmbed: true, enableYouTubeEmbed: true,
enableVideoEmbed: true, enableVideoEmbed: true,
enableCheckbox: false, enableCheckbox: false,
inlineFootnotes: true, enableInlineFootnotes: true,
} }
const calloutMapping = { const calloutMapping = {
@ -145,7 +145,12 @@ 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 = /\^\[((?:[^\[\]]|\[(?:[^\[\]]|\[[^\[\]]*\])*\])*)\]/g const inlineFootnoteRegex = new RegExp(/\^\[((?:[^\[\]]|\[(?:[^\[\]]|\[[^\[\]]*\])*\])*)\]/g)
// match inline footnotes where content can contain any properly nested brackets
// \^\[...\] -> matches ^[inline footnote's] brackets
// (?:...) -> does not capture any of the following:
// [^\[\]] -> any character that is not a bracket
// \[(?:[^\[\]]|\[[^\[\]]*\])*\] -> a properly nested set of brackets
export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>> = (userOpts) => { export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>> = (userOpts) => {
const opts = { ...defaultOptions, ...userOpts } const opts = { ...defaultOptions, ...userOpts }
@ -217,15 +222,16 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>>
}) })
} }
if (opts.inlineFootnotes) { if (opts.enableInlineFootnotes) {
// Replaces ^[inline] footnotes with regular footnotes [^1]: // Replaces ^[inline] footnotes with regular footnotes [^1]:
const footnotes: Record<string, string> = {} const footnotes: Record<string, string> = {}
let counter = 1
// Replace inline footnotes with references and collect definitions // Replace inline footnotes with references and collect definitions
const result = (src as string).replace( const result = (src as string).replace(
inlineFootnoteRegex, inlineFootnoteRegex,
(_match: string, content: string) => { (_match: string, content: string) => {
const id = `inline-${Object.keys(footnotes).length + 1}` const id = `inline-${counter++}`
footnotes[id] = content.trim() footnotes[id] = content.trim()
return `[^${id}]` return `[^${id}]`
}, },