Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Jeandcc opened this issue Aug 29, 2023 · 3 comments

Comments

@Jeandcc
Copy link
Contributor

Jeandcc commented Aug 29, 2023

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) + ' '
}
}

@Jeandcc
Copy link
Contributor Author

Jeandcc commented Aug 31, 2023

Hello @praveenn77 , any chance you could have a quick look here? I'd be willing to work on a PR if you're too busy, but it would be great to get some insights from you first.

Thanks

@praveenn77
Copy link
Owner

Hi @Jeandcc , thanks for reporting this issue,
I have made a change and publsihed a new version 2.4.2 now you can search your FrontMatter element by adding an attribute data-search-children to the wrapper elements.
image
image

Hope this helps,
Thanks.

@Jeandcc
Copy link
Contributor Author

Jeandcc commented Sep 7, 2023

Hi @praveenn77 , thanks a lot for the code you built for this functionality!

I took the freedom to further develop this feature, aiming at reducing the workload for developers in case they have headings on different levels of hierarchy.

With the previous approach, devs would have to add the data-search-children attribute to all divs that contained headings.

With the PR #117 that I opened, this would not be necessary, as the code would look through all direct and indirect children of the element with the data-search-children attribute.

Could you please take a look at it, and let me know in case of questions or requests for tweaks?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants