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: support for searching code blocks #1797

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chenjiahan
Copy link
Member

Summary

This PR adds a new search.codeBlocks option.

If enabled, the search index will include code block content, which allows users to search code blocks.

import { defineConfig } from 'rspress/config';

export default defineConfig({
  search: {
    codeBlocks: true,
  },
});

Default value

This default value is false to align with other search engines like Docsearch, see: https://docsearch.algolia.com/docs/docsearch-program/#can-you-index-code-samples.

If it works well, we may consider enabling it by default in the future.

Related Issue

resolve #1543

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copy link

netlify bot commented Feb 2, 2025

Deploy Preview for aquamarine-blini-95325f ready!

Name Link
🔨 Latest commit f22e338
🔍 Latest deploy log https://app.netlify.com/sites/aquamarine-blini-95325f/deploys/679f7a25125b7d0008d70301
😎 Deploy Preview https://deploy-preview-1797--aquamarine-blini-95325f.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 86 (🔴 down 6 from production)
Accessibility: 97 (no change from production)
Best Practices: 92 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -108,7 +109,7 @@ export async function extractPageData(
{
// Skip code blocks
selector: 'pre > code',
format: 'skip',
format: searchCodeBlocks ? 'inlineTag' : 'skip',
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

@chenjiahan chenjiahan Feb 3, 2025

Choose a reason for hiding this comment

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

All the built-it formats of html-to-text will preserve the <code> prefix.

I guess this is not bad, since it shows that the search results are from code blocks.

If you have time, you can try writing a custom formatter to remove <code> 😄

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

Successfully merging this pull request may close these issues.

[Feature]: Code blocks are searchable.
2 participants