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

feat!: allow customizing markdown renderer used for local search indexing #2770

Merged
merged 5 commits into from Aug 20, 2023
Merged

feat!: allow customizing markdown renderer used for local search indexing #2770

merged 5 commits into from Aug 20, 2023

Conversation

damieng
Copy link
Contributor

@damieng damieng commented Aug 10, 2023

Localsearch indexes content directly rendered from the file system without opportunity to change the HTML before indexing.

This can be a problem if your layout normally adds important elements such as a <h1> based on a Frontmatter title (as the VueJS blog does).

This PR introduces a hook where, for localsearch, you can provide a function that takes the HTML, environmental data such as the page path and Frontmatter, and a render function. You can then use these elements to either re-process the markdown or to manipulate it in some way.,

In the VueJS blog scenario you can now do this to ensure titles are headings on your search results:

import { defineConfig } from 'vitepress'

export default defineConfig({
  themeConfig: {
    search: {
      options: {
        preIndexRender: (html, env, render) =>
          env.frontmatter?.title
            ? render('# ' + env.frontmatter?.title) + html
            : html
      }
    }
  }
}

Thanks for your consideration!

@damieng
Copy link
Contributor Author

damieng commented Aug 11, 2023

Removed "fileContents" as the raw markdown is available as env.content.

@brc-dd
Copy link
Member

brc-dd commented Aug 15, 2023

Hi, sorry for the delay. But this might need some more consideration as preIndexRender will be a node only API and having it serialized in each page won't make sense. We'll need some way to mark it so that it doesn't appear in the client-side code. I'll take a look. Thanks for the PR.

@brc-dd brc-dd self-assigned this Aug 15, 2023
@damieng
Copy link
Contributor Author

damieng commented Aug 15, 2023

No problem, thanks for the update. Let me know if there is anything I can help with.

@brc-dd
Copy link
Member

brc-dd commented Aug 17, 2023

I think we probably can decide on some name conventions like node_<fnName> and strip them away in serializer. These functions need not be pure.

The other alternative I see is using some property to indicate node only: name: { node: true, fn: () => {...} } But that's more verbose.

Other thing I tried was using some magic comments, but turns out esbuild removes them while loading the config, so those won't work.

First should be good IMO, wdyt?

@damieng
Copy link
Contributor Author

damieng commented Aug 19, 2023

The node_ prefix works for me. I've updated the PR and re-tested it on my project and it works fine.

image

image

@brc-dd
Copy link
Member

brc-dd commented Aug 19, 2023

Thanks for the update. I'll update the serializer part.

@brc-dd brc-dd marked this pull request as draft August 20, 2023 13:13
@brc-dd brc-dd changed the title feat(theme): add preIndexRender hook for localsearch feat!: allow customizing markdown renderer used for local search indexing Aug 20, 2023
@brc-dd brc-dd marked this pull request as ready for review August 20, 2023 15:01
@brc-dd
Copy link
Member

brc-dd commented Aug 20, 2023

Can you verify if the example mentioned in https://deploy-preview-2770--vitepress-docs.netlify.app/reference/default-theme-search#example-transforming-content-adding-anchors works for you with the latest commit?

@damieng
Copy link
Contributor Author

damieng commented Aug 20, 2023

Confirmed working, my H1-adding scenario is now:

  themeConfig: {
    search: {
      provider: "local",
      options: {
        detailedView: true,
        _render: (src, env, md) => {
          const html = md.render(src, env)
          if (env.frontmatter?.search === false) return ""
          return env.frontmatter?.title ? md.render("# " + env.frontmatter?.title) + html : html
        },
      },
    },

Thanks!

@brc-dd brc-dd merged commit 00dc1e6 into vuejs:main Aug 20, 2023
7 checks passed
@damieng damieng deleted the feat/add-preindex-hook-to-theme branch August 20, 2023 21:46
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants