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

Omit ...{:type} annotations from search index #2922

Merged
merged 7 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/pages/docs/guide/_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
ssg: 'Next.js SSG',
i18n: 'Next.js I18n',
'custom-css': '',
search: '',
advanced: '',
'built-ins': 'Built-ins'
}
12 changes: 12 additions & 0 deletions docs/pages/docs/guide/search.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Search

Nextra features full-page search, that includes list items, code blocks,
headings and table cells.

## Search in `<code attr="code1" />{:html}`

Inline `<code attr="code2" />{:html}` can be searched as well as code blocks.

```html
<code>I'm Searchable!</code>
```
19 changes: 14 additions & 5 deletions packages/nextra/src/server/remark-plugins/remark-structurize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { Root } from 'mdast'
import type { Plugin } from 'unified'
import type { Search, StructurizedData } from '../../types'
import type { HProperties } from './remark-custom-heading-id'

type RootContent = Root['children'][number]

const CODE_TABLE_QUOTE_LIST = new Set<string>([
'code',
Expand Down Expand Up @@ -37,7 +40,7 @@ export const remarkStructurize: Plugin<[Search], Root> = options => {
}
}

function walk(node: any): string {
function walk(node: RootContent | Root): string {
let result = ''
const { type } = node

Expand All @@ -56,10 +59,15 @@ export const remarkStructurize: Plugin<[Search], Root> = options => {
}
} else if (
(opts.codeblocks && type === 'code') ||
['text', 'inlineCode', 'tableCell'].includes(type)
type === 'text' ||
type === 'inlineCode' ||
type === 'tableCell'
) {
result += node.value
if (!skip) content += node.value
// Inline code may have an `{:language}` suffix, trim this off if it exists.
const value =
type === 'inlineCode' ? node.value.replace(/\{:\w+}$/, '') : node.value
result += value
if (!skip) content += value
}

if (
Expand All @@ -78,7 +86,8 @@ export const remarkStructurize: Plugin<[Search], Root> = options => {
if (node.depth > 1) {
save()
content = '' // reset content after h1 content
activeSlug = node.data.hProperties.id + '#' + result
const hProperties = node.data!.hProperties as HProperties
activeSlug = hProperties.id + '#' + result
}
}
return result
Expand Down
Loading