Skip to content

[issue]: lunr-search doesn't pickup on heading elements that are non-direct children of '.markdown' #115

@Jeandcc

Description

@Jeandcc

Here's a demo codebase used to replicate the issue: https://github.com/Jeandcc/wrapped-heading-issue-w-docusaurus-lunr-search

As you can see on this file, we swizzled the DocItem 'component' so we can render some content on the documentation page based on the Front Matter data as well, and not only on the markdown of the page.

// /src/theme/DocItem/Content/index.js
export default function DocItemContent({ children }) {
  const { frontMatter } = useDoc();
  // (...)

  const headingFromFM = frontMatter?.heading || "";
  const descriptionFromFM = frontMatter?.description || "";
  const wrappedHeadingFromFM = frontMatter?.wrappedHeading || "";
  const wrappedDescriptionFromFM = frontMatter?.wrappedDescription || "";

  return (
    <div className={clsx(ThemeClassNames.docs.docMarkdown, "markdown")}>
      { /* (...) */ }

      <MDXContent>
        {headingFromFM && <h2>{headingFromFM}</h2>}
        {descriptionFromFM && <p>{descriptionFromFM}</p>}

        <div>
          {wrappedHeadingFromFM && <h2>{wrappedHeadingFromFM}</h2>}
          {wrappedDescriptionFromFM && <p>{wrappedDescriptionFromFM}</p>}
        </div>

        {children}
      </MDXContent>
    </div>
  );
}

Here's the example doc used to render Front Matter content as well as MD content.

The problem that we are currently facing is that docusaurus-lunr-search only pickups the heading elements that are direct children of the .markdown element. Headings that are encapsulated by other elements, such as divs, will not get picked up for indexing by lunr-search.

Here's "visual proof":

Visual preview of the documentation page

image

Searching for top-level heading generated with FrontMatter and Swizzling:

image

Searching for non-top-level heading generated with FrontMatter and Swizzling:

image


After browsing the docusaurus-lunr-search codebase, I believe the fix for this issue will touch on these lines of the codebase:

for (const node of markdown.children) {
if (is(node, sectionHeaderTest)) {
if (currentSection) {
emitCurrent()
}
currentSection = node
} else if (currentSection) {
contentsAcc += getContent(node) + ' '
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions