mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-26 13:26:22 +00:00
chore: transclude subsection without dynamic regex construction
This commit is contained in:
parent
a506cedd7a
commit
0ca8a2ac7c
1 changed files with 13 additions and 9 deletions
|
@ -19,6 +19,7 @@ interface RenderComponents {
|
||||||
footer: QuartzComponent
|
footer: QuartzComponent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const headerRegex = new RegExp(/h[1-6]/)
|
||||||
export function pageResources(
|
export function pageResources(
|
||||||
baseDir: FullSlug | RelativeURL,
|
baseDir: FullSlug | RelativeURL,
|
||||||
staticResources: StaticResources,
|
staticResources: StaticResources,
|
||||||
|
@ -105,20 +106,23 @@ export function renderPage(
|
||||||
// header transclude
|
// header transclude
|
||||||
blockRef = blockRef.slice(1)
|
blockRef = blockRef.slice(1)
|
||||||
let startIdx = undefined
|
let startIdx = undefined
|
||||||
|
let startDepth = undefined
|
||||||
let endIdx = undefined
|
let endIdx = undefined
|
||||||
let headerRegex = /h[1-6]/
|
|
||||||
for (const [i, el] of page.htmlAst.children.entries()) {
|
for (const [i, el] of page.htmlAst.children.entries()) {
|
||||||
if (el.type === "element" && el.tagName.match(headerRegex)) {
|
// skip non-headers
|
||||||
if (endIdx) {
|
if (!(el.type === "element" && el.tagName.match(headerRegex))) continue
|
||||||
break
|
const depth = Number(el.tagName.substring(1))
|
||||||
}
|
|
||||||
|
|
||||||
if (startIdx !== undefined) {
|
// lookin for our blockref
|
||||||
endIdx = i
|
if (startIdx === undefined || startDepth === undefined) {
|
||||||
} else if (el.properties?.id === blockRef) {
|
// skip until we find the blockref that matches
|
||||||
|
if (el.properties?.id === blockRef) {
|
||||||
startIdx = i
|
startIdx = i
|
||||||
headerRegex = new RegExp(`h[1-${el.tagName.slice(-1)}]`)
|
startDepth = Number(el.tagName.substring(1))
|
||||||
}
|
}
|
||||||
|
} else if (depth <= startDepth) {
|
||||||
|
// looking for new header that is same level or higher
|
||||||
|
endIdx = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue