Skip to content

fix(queryCollectionSearchSections): fix options types#3705

Open
hendrikheil wants to merge 2 commits intonuxt:mainfrom
hendrikheil:fix/search-sections/options-types
Open

fix(queryCollectionSearchSections): fix options types#3705
hendrikheil wants to merge 2 commits intonuxt:mainfrom
hendrikheil:fix/search-sections/options-types

Conversation

@hendrikheil
Copy link
Contributor

@hendrikheil hendrikheil commented Jan 28, 2026

🔗 Linked issue


❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

This PR updates the server and client types for queryCollectionSearchSections. It adds missing options like minHeading, maxHeading and extraFields and removes an unused options separators, which had no effect on the actual query.

I've also added a test for the extraFields behavior to ensure it actually works as expected.

It might be useful to export the options of generateSearchSections as a type so that they don't have to be duplicated two additional times?

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@vercel
Copy link

vercel bot commented Jan 28, 2026

@hendrikheil is attempting to deploy a commit to the Nuxt Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Jan 28, 2026

📝 Walkthrough

Walkthrough

The PR makes queryCollectionSearchSections generic over T extends keyof PageCollections in client and server runtimes, and adds options extraFields and minHeading/maxHeading to its signature. generateSearchSections accepts extraFields typed as (string | symbol | number)[] and performs casts where needed. Documentation was updated to show extraFields, and a unit test was added to verify extra fields (e.g., author) are included in produced sections.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(queryCollectionSearchSections): fix options types' accurately reflects the main change: correcting TypeScript type definitions for function options.
Description check ✅ Passed The description directly relates to the changeset, explaining the type fixes, added options (minHeading, maxHeading, extraFields), removed unused option (separators), and test additions.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
docs/content/docs/4.utils/4.query-collection-search-sections.md (1)

26-26: Type signature in documentation is incomplete.

The function signature in the Type section doesn't include extraFields in the options, but the API section below documents it. Update the type signature for consistency:

📝 Suggested fix
-function queryCollectionSearchSections(collection: keyof Collections, opts?: { ignoredTags?: string[], minHeading?: string, maxHeading?: string }): ChainablePromise<T, Section[]>
+function queryCollectionSearchSections(collection: keyof Collections, opts?: { ignoredTags?: string[], minHeading?: string, maxHeading?: string, extraFields?: string[] }): ChainablePromise<T, Section[]>
src/runtime/internal/search.ts (1)

30-40: Pipeline failure: Type error when spreading extraFields into select().

The widened type (string | symbol | number)[] for extraFields causes a TypeScript error at line 37 because select() expects keyof T. Since you're already casting extraFields as string[] at line 40, apply a similar cast when passing to select():

🔧 Proposed fix
   const documents = await queryBuilder
     .where('extension', '=', 'md')
-    .select('path', 'body', 'description', 'title', ...(extraFields || []))
+    .select('path', 'body', 'description', 'title', ...((extraFields || []) as string[]))
     .all()

@hendrikheil hendrikheil force-pushed the fix/search-sections/options-types branch from 379e0ec to 44050d8 Compare January 28, 2026 17:12
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 28, 2026

npm i https://pkg.pr.new/@nuxt/content@3705

commit: 7f9174c

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/content/docs/4.utils/4.query-collection-search-sections.md (1)

48-55: Clarify that extraFields extends the section object properties.

The documentation for the return value (lines 49-55) lists the standard properties (id, title, titles, content, level) but doesn't mention that when extraFields is provided, those additional fields will also be present in each section object.

Consider adding a note like:

  - `extraFields`: An array of additional fields from the collection items to include in the section objects.
- Returns: A Promise that resolves to an array of searchable sections. Each section is an object with the following properties:
  - `id`: A unique identifier for the section.
  - `title`: The title of the section (usually the heading text).
  - `titles`: An array of parent section titles, representing the hierarchy.
  - `content`: The textual content of the section.
  - `level`: The heading level (1-6) of the section, where 1 is the highest level.
  - Any additional fields specified in the `extraFields` option will also be included in each section.

This helps users understand that the section objects are dynamically extended based on the extraFields option.

🧹 Nitpick comments (1)
docs/content/docs/4.utils/4.query-collection-search-sections.md (1)

26-26: Consider more specific types for heading level options.

The minHeading and maxHeading options are currently typed as string, which allows any string value. For better type safety and developer experience, consider using a union type:

minHeading?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
maxHeading?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'

This would provide autocomplete suggestions and catch invalid values at compile time.

@farnabaz
Copy link
Member

farnabaz commented Feb 5, 2026

It might be useful to export the options of generateSearchSections as a type so that they don't have to be duplicated two additional times?

Thanks for the PR @hendrikheil
Could you create this type and update PR? :)

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

Successfully merging this pull request may close these issues.

2 participants