fix wikilinks to anchors in the same document
This commit is contained in:
parent
85bb91d998
commit
5e7865d7e9
@ -4,7 +4,6 @@ draft: true
|
|||||||
|
|
||||||
## todo
|
## todo
|
||||||
|
|
||||||
- wikilink to anchors in the same document
|
|
||||||
- blockquote with link in first child broken
|
- blockquote with link in first child broken
|
||||||
- 404 using base url
|
- 404 using base url
|
||||||
- back button with anchors / popovers + spa is broken
|
- back button with anchors / popovers + spa is broken
|
||||||
|
@ -108,7 +108,7 @@ const capitalize = (s: string): string => {
|
|||||||
// ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name)
|
// ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name)
|
||||||
// (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link)
|
// (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link)
|
||||||
// (|[^\[\]\|\#]+)? -> | then one or more non-special characters (alias)
|
// (|[^\[\]\|\#]+)? -> | then one or more non-special characters (alias)
|
||||||
const wikilinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)(#[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g")
|
const wikilinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)?(#[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g")
|
||||||
const highlightRegex = new RegExp(/==(.+)==/, "g")
|
const highlightRegex = new RegExp(/==(.+)==/, "g")
|
||||||
const commentRegex = new RegExp(/%%(.+)%%/, "g")
|
const commentRegex = new RegExp(/%%(.+)%%/, "g")
|
||||||
// from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
|
// from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
|
||||||
@ -161,10 +161,11 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
|
|||||||
if (opts.wikilinks) {
|
if (opts.wikilinks) {
|
||||||
src = src.toString()
|
src = src.toString()
|
||||||
return src.replaceAll(wikilinkRegex, (value, ...capture) => {
|
return src.replaceAll(wikilinkRegex, (value, ...capture) => {
|
||||||
const [fp, rawHeader, rawAlias] = capture
|
const [rawFp, rawHeader, rawAlias] = capture
|
||||||
|
const fp = rawFp ?? ""
|
||||||
const anchor = rawHeader?.trim().slice(1)
|
const anchor = rawHeader?.trim().slice(1)
|
||||||
const displayAnchor = anchor ? `#${slugAnchor(anchor)}` : ""
|
const displayAnchor = anchor ? `#${slugAnchor(anchor)}` : ""
|
||||||
const displayAlias = rawAlias ?? ""
|
const displayAlias = rawAlias ?? rawHeader?.replace("#", "|") ?? ""
|
||||||
const embedDisplay = value.startsWith("!") ? "!" : ""
|
const embedDisplay = value.startsWith("!") ? "!" : ""
|
||||||
return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]`
|
return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]`
|
||||||
})
|
})
|
||||||
@ -177,8 +178,8 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
|
|||||||
plugins.push(() => {
|
plugins.push(() => {
|
||||||
return (tree: Root, _file) => {
|
return (tree: Root, _file) => {
|
||||||
findAndReplace(tree, wikilinkRegex, (value: string, ...capture: string[]) => {
|
findAndReplace(tree, wikilinkRegex, (value: string, ...capture: string[]) => {
|
||||||
let [fp, rawHeader, rawAlias] = capture
|
let [rawFp, rawHeader, rawAlias] = capture
|
||||||
fp = fp.trim()
|
const fp = rawFp?.trim() ?? ""
|
||||||
const anchor = rawHeader?.trim() ?? ""
|
const anchor = rawHeader?.trim() ?? ""
|
||||||
const alias = rawAlias?.slice(1).trim()
|
const alias = rawAlias?.slice(1).trim()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user