Skip to content

Commit

Permalink
Omit ...{:type} annotations from search index (#2922)
Browse files Browse the repository at this point in the history
* Omit `...{:type}` annotations from search index

* Prettier

* Lint

* Update packages/nextra/src/server/remark-plugins/remark-structurize.ts

Co-authored-by: Dimitri POSTOLOV <[email protected]>

* Lint

* polish

---------

Co-authored-by: Dimitri POSTOLOV <[email protected]>
Co-authored-by: Dimitri POSTOLOV <[email protected]>
  • Loading branch information
3 people authored Jul 20, 2024
1 parent a95e745 commit 3002b01
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
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

0 comments on commit 3002b01

Please sign in to comment.