Allow passing user options to pagefind.createIndex#3674
Conversation
🦋 Changeset detectedLatest commit: 697254d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Hello! Thank you for opening your first PR to Starlight! ✨ Here’s what will happen next:
|
Lunaria Status Overview🌕 This pull request will trigger status changes. Learn moreBy default, every PR changing files present in the Lunaria configuration's You can change this by adding one of the keywords present in the Tracked Files
Warnings reference
|
❌ Deploy Preview for astro-starlight failed. Why did it fail? →
|
✅ Deploy Preview for astro-starlight ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Thanks for the PR @controversial! We normally ask people first open a discussion for features like this to give us a bit of time to chat through the expectations, motivations, use cases etc. and solicit user feedback, which helps us prioritise and understand the value of things. In this case, I do have some concerns around the usefulness of everything included here. I can see an argument for We can chat about these here if you like, but given these doubts, it might make more sense to move to a discussion to outline all the use cases etc. |
|
I’d be happy to open a discussion! The primary motivation here was that I was trying to accomplish #3675 in user-land, and was surprised to find that i didn’t have the ability to configure I think that’s useful functionality even when paired with I’d be happy to drop the other two parts of this PR ( |
I guess to me the question is whether there are use cases that wouldn’t be “Fix a Starlight bug”, because if the API mainly allows user-land fixes of bugs on our end, it would be better to fix those bugs upstream rather than requiring individual users to patch things up if they notice. Have you hit other cases where control over |
|
I think the other cases would be anywhere that useres are rendering markup that they don’t directly control—this could be markup that Starlight generates, or it could be markup from 3rd-party remark plugins, or from 3rd-party components that are imported and rendered within mdx content. IMO it’s not uncommon to have markup in a page to which it would be difficult to directly attach |
delucis
left a comment
There was a problem hiding this comment.
Thanks for chatting that through! Yeah, I agree there’s a reasonable argument for excludeSelectors here. Would you be able to clean up the PR to only include that? I’ve also left a couple of early review notes.
Re: testing — did you try excludeSelectors in a demo project? I think knowing it works correctly with a manual test (including e.g. for multiple different selectors) would be a good start. When switching to Pagefind’s Node API we decided for our CI testing, we should trust Pagefind behaves like it’s supposed to, so only test we pass the right values in from user config. That‘s probably sufficient here too.
|
|
||
| <script> | ||
| import { pagefindUserConfig } from 'virtual:starlight/pagefind-config'; | ||
| const { index: _, ...pagefindClientConfig } = pagefindUserConfig; |
There was a problem hiding this comment.
I think this would still end up leaking the index configuration into the client bundle. It might be better to do this here when creating the virtual module so it isn’t included at all:
And updating the virtual module type needs to be done manually too here:
starlight/packages/starlight/virtual-internal.d.ts
Lines 25 to 29 in 49f153d
|
|
||
| - See [“Customize Pagefind's result ranking”](https://pagefind.app/docs/ranking/) in the Pagefind documentation for more details about using the `pagefind.ranking` option to control how search result ranking is calculated | ||
| - See [“Searching multiple sites”](https://pagefind.app/docs/multisite/) in the Pagefind documentation for more details about using the `pagefind.mergeIndex` option to control how to search across multiple sites | ||
| - See [“pagefind.createIndex”](https://pagefind.app/docs/node-api/#pagefindcreateindex) in the Pagefind documentation for more details about using the `pagefind.index` option to customize what content is indexed. |
There was a problem hiding this comment.
Given we’ll only support excludeSelectors, we can probably describe that here in a tiny bit of detail.
| index?: Pick< | ||
| pagefind.PagefindServiceConfig, | ||
| 'rootSelector' | 'excludeSelectors' | 'forceLanguage' | ||
| >; |
There was a problem hiding this comment.
I’m not sure it’s useful to document this using a type from Pagefind which people can’t look up. But with the simplification, it’s probably not too bad to show the actual type? (I’m assuming it’s something like this.)
| index?: Pick< | |
| pagefind.PagefindServiceConfig, | |
| 'rootSelector' | 'excludeSelectors' | 'forceLanguage' | |
| >; | |
| index?: { | |
| excludeSelectors?: string[]; | |
| }; |
|
Hi @controversial are you still interested in landing these changes? |
|
Yes, I am, sorry that I haven’t been able to work on this for a few weeks! Will try to make these changes today or tomorrow. |
|
No worries! Just wanted to make sure if we should close or keep the PR. Take your time 🙌 |

Description
This PR allows the user to specify options in
pagefind.indexthat will be passed through topagefind.createIndex, notably:excludeSelectors, which is useful for excluding content from pagefind where the user does not control the markup to adddata-pagefind-ignore. Configuring this option is the main motivation for this PR.rootSelector, which could be useful if anyone wants to change the default placement ofdata-pagefind-bodyfrom its default placement on<main>.forceLanguagewhich seemed like it wouldn’t hurt to include but I don’t have a specific motivation for it, and can remove it if it seems it would work against Starlight’s locale handling.I excluded the ability to configure logging options here, since they don’t affect the output index, and I excluded the config option for including
/index.htmlpaths since it doesn’t seem useful within Starlight.These options weren’t practical to expose in Starlight before switching to the JS API in #3534 but now it seems reasonable to make them configurable.
This is a more generic solution to allow users to address problems like the one described in #3676
I’m eager for suggestions on how to include unit tests for this functionality, at a glance it seems like the existing unit tests mock most of the relevant functionality for pagefind, so I haven’t added tests here yet as it didn’t seem straightforward