Jacky Zhao
2023-11-12 43d638a6de5a8a11c1a719c596b07ea957c7329a
perf: compute mapping of folder name to file data for faster breadcrumbs
2 files modified
53 ■■■■ changed files
docs/features/breadcrumbs.md 6 ●●●● patch | view | raw | blame | history
quartz/components/Breadcrumbs.tsx 47 ●●●● patch | view | raw | blame | history
docs/features/breadcrumbs.md
@@ -16,10 +16,10 @@
```typescript title="quartz.layout.ts"
Component.Breadcrumbs({
  spacerSymbol: ">", // symbol between crumbs
  spacerSymbol: "❯", // symbol between crumbs
  rootName: "Home", // name of first/root element
  resolveFrontmatterTitle: false, // wether to resolve folder names through frontmatter titles (more computationally expensive)
  hideOnRoot: true, // wether to hide breadcrumbs on root `index.md` page
  resolveFrontmatterTitle: true, // whether to resolve folder names through frontmatter titles
  hideOnRoot: true, // whether to hide breadcrumbs on root `index.md` page
})
```
quartz/components/Breadcrumbs.tsx
@@ -41,25 +41,13 @@
  }
}
// given a folderName (e.g. "features"), search for the corresponding `index.md` file
function findCurrentFile(allFiles: QuartzPluginData[], folderName: string) {
  return allFiles.find((file) => {
    if (file.slug?.endsWith("index")) {
      const folderParts = file.filePath?.split("/")
      if (folderParts) {
        const name = folderParts[folderParts?.length - 2]
        if (name === folderName) {
          return true
        }
      }
    }
  })
}
export default ((opts?: Partial<BreadcrumbOptions>) => {
  // Merge options with defaults
  const options: BreadcrumbOptions = { ...defaultOptions, ...opts }
  // computed index of folder name to its associated file data
  let folderIndex: Map<string, QuartzPluginData> | undefined
  function Breadcrumbs({ fileData, allFiles, displayClass }: QuartzComponentProps) {
    // Hide crumbs on root if enabled
    if (options.hideOnRoot && fileData.slug === "index") {
@@ -70,28 +58,39 @@
    const firstEntry = formatCrumb(options.rootName, fileData.slug!, "/" as SimpleSlug)
    const crumbs: CrumbData[] = [firstEntry]
    if (!folderIndex && options.resolveFrontmatterTitle) {
      folderIndex = new Map()
      // construct the index for the first time
      for (const file of allFiles) {
        if (file.slug?.endsWith("index")) {
          const folderParts = file.filePath?.split("/")
          if (folderParts) {
            const folderName = folderParts[folderParts?.length - 2]
            folderIndex.set(folderName, file)
          }
        }
      }
    }
    // Split slug into hierarchy/parts
    const slugParts = fileData.slug?.split("/")
    if (slugParts) {
      // full path until current part
      let currentPath = ""
      for (let i = 0; i < slugParts.length - 1; i++) {
        let currentTitle = slugParts[i]
        let curPathSegment = slugParts[i]
        // TODO: performance optimizations/memoizing
        // Try to resolve frontmatter folder title
        if (options?.resolveFrontmatterTitle) {
          // try to find file for current path
          const currentFile = findCurrentFile(allFiles, currentTitle)
          if (currentFile) {
            currentTitle = currentFile.frontmatter!.title
          }
        const currentFile = folderIndex?.get(curPathSegment)
        if (currentFile) {
          curPathSegment = currentFile.frontmatter!.title
        }
        // Add current slug to full path
        currentPath += slugParts[i] + "/"
        // Format and add current crumb
        const crumb = formatCrumb(currentTitle, fileData.slug!, currentPath as SimpleSlug)
        const crumb = formatCrumb(curPathSegment, fileData.slug!, currentPath as SimpleSlug)
        crumbs.push(crumb)
      }