Skip to content

Commit

Permalink
chore: transclude subsection without dynamic regex construction
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Mar 6, 2024
1 parent a506ced commit 0ca8a2a
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions quartz/components/renderPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface RenderComponents {
footer: QuartzComponent
}

const headerRegex = new RegExp(/h[1-6]/)
export function pageResources(
baseDir: FullSlug | RelativeURL,
staticResources: StaticResources,
Expand Down Expand Up @@ -105,20 +106,23 @@ export function renderPage(
// header transclude
blockRef = blockRef.slice(1)
let startIdx = undefined
let startDepth = undefined
let endIdx = undefined
let headerRegex = /h[1-6]/
for (const [i, el] of page.htmlAst.children.entries()) {
if (el.type === "element" && el.tagName.match(headerRegex)) {
if (endIdx) {
break
}

if (startIdx !== undefined) {
endIdx = i
} else if (el.properties?.id === blockRef) {
// skip non-headers
if (!(el.type === "element" && el.tagName.match(headerRegex))) continue
const depth = Number(el.tagName.substring(1))

// lookin for our blockref
if (startIdx === undefined || startDepth === undefined) {
// skip until we find the blockref that matches
if (el.properties?.id === blockRef) {
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
}
}

Expand Down

0 comments on commit 0ca8a2a

Please sign in to comment.