Skip to content

Commit 576cb6f

Browse files
author
Dimitri POSTOLOV
authoredSep 5, 2023
[v3] rename nextraConfig.flexsearch to nextraConfig.search (#2256)

File tree

16 files changed

+55
-60
lines changed

16 files changed

+55
-60
lines changed
 
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nextra': major
3+
---
4+
5+
- rename `nextraConfig.flexsearch` to `nextraConfig.search`

‎docs/next.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const withNextra = nextra({
44
theme: 'nextra-theme-docs',
55
themeConfig: './theme.config.tsx',
66
latex: true,
7-
flexsearch: {
7+
search: {
88
codeblocks: false
99
},
1010
defaultShowCopyCode: true

‎docs/pages/docs/docs-theme/api/use-config.mdx

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ context.
5252
[Table of Contents Sidebar](/docs/docs-theme/theme-configuration#toc-sidebar)
5353
- `useNextSeoOptions`: a function that passes data to Next.js SEO component.
5454
(see [SEO Options](/docs/docs-theme/theme-configuration#seo-options))
55-
- `flexsearch`: data about search configuration
56-
- `title`: Title about current document
55+
- `title`: title about current page
5756
- `frontMatter`: parsed [front matter](https://jekyllrb.com/docs/front-matter)
5857
data
59-
- `flexsearch`: data for search configuration

‎examples/docs/next.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const withNextra = nextra({
44
theme: 'nextra-theme-docs',
55
themeConfig: './src/theme.config.js',
66
latex: true,
7-
flexsearch: {
7+
search: {
88
codeblock: false
99
}
1010
})

‎examples/docs/src/pages/themes/docs/configuration.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ export default {
572572

573573
<Unstable />
574574

575-
## `flexsearch`
575+
## `search`
576576

577577
Enable Nextra built-in search
578578

@@ -586,7 +586,7 @@ import nextra from 'nextra'
586586
const withNextra = nextra({
587587
theme: 'nextra-theme-blog',
588588
themeConfig: './theme.config.js',
589-
flexsearch: {
589+
search: {
590590
codeblock: false
591591
}
592592
})

‎packages/nextra-theme-docs/src/components/navbar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export function Navbar({ items }: NavBarProps): ReactElement {
164164
)
165165
})}
166166

167-
{config.flexsearch &&
167+
{process.env.NEXTRA_SEARCH &&
168168
renderComponent(config.search.component, {
169169
className: 'nx-hidden md:nx-inline-block mx-min-w-[200px]'
170170
})}

‎packages/nextra-theme-docs/src/components/sidebar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export function Sidebar({
415415
)}
416416
ref={containerRef}
417417
>
418-
{config.flexsearch && (
418+
{process.env.NEXTRA_SEARCH && (
419419
<div className="nx-px-4 nx-pt-4 md:nx-hidden">
420420
{renderComponent(config.search.component)}
421421
</div>

‎packages/nextra-theme-docs/src/contexts/config.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { Context } from '../types'
1010
import { MenuProvider } from './menu'
1111

1212
type Config<FrontMatterType = FrontMatter> = DocsThemeConfig &
13-
Pick<PageOpts<FrontMatterType>, 'flexsearch' | 'title' | 'frontMatter'>
13+
Pick<PageOpts<FrontMatterType>, 'title' | 'frontMatter'>
1414

1515
const ConfigContext = createContext<Config>({
1616
title: '',
@@ -98,7 +98,6 @@ export const ConfigProvider = ({
9898
}
9999
const extendedConfig: Config = {
100100
...theme,
101-
flexsearch: pageOpts.flexsearch,
102101
title: pageOpts.title,
103102
frontMatter: pageOpts.frontMatter
104103
}

‎packages/nextra/__test__/loader.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ describe('tree shaking', async () => {
3131
it('should not include default `pageOpts.frontMatter: {}`', () => {
3232
expect(testPage.includes('frontMatter:')).toBe(true)
3333
})
34-
it('should not include `pageOpts.flexsearch`', () => {
35-
expect(testPage.includes('flexsearch:')).toBe(false)
34+
it('should not include `pageOpts.search`', () => {
35+
expect(testPage.includes('search:')).toBe(false)
3636
})
3737
it('should not include `themeConfig`', () => {
3838
expect(testPage.includes('themeConfig:')).toBe(false)

‎packages/nextra/src/compile.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const clonedRemarkLinkRewrite = remarkLinkRewrite.bind(null)
7979
type CompileMdxOptions = Pick<
8080
LoaderOptions,
8181
| 'staticImage'
82-
| 'flexsearch'
82+
| 'search'
8383
| 'defaultShowCopyCode'
8484
| 'readingTime'
8585
| 'latex'
@@ -97,7 +97,7 @@ export async function compileMdx(
9797
source: string,
9898
{
9999
staticImage,
100-
flexsearch,
100+
search,
101101
readingTime,
102102
latex,
103103
codeHighlight,
@@ -116,16 +116,16 @@ export async function compileMdx(
116116
let searchIndexKey: string | null = null
117117
if (ERROR_ROUTES.has(route)) {
118118
/* skip */
119-
} else if (typeof flexsearch === 'object') {
120-
if (flexsearch.indexKey) {
121-
searchIndexKey = flexsearch.indexKey(filePath, route, locale)
119+
} else if (typeof search === 'object') {
120+
if (search.indexKey) {
121+
searchIndexKey = search.indexKey(filePath, route, locale)
122122
if (searchIndexKey === '') {
123123
searchIndexKey = locale || DEFAULT_LOCALE
124124
}
125125
} else {
126126
searchIndexKey = locale || DEFAULT_LOCALE
127127
}
128-
} else if (flexsearch) {
128+
} else if (search) {
129129
searchIndexKey = locale || DEFAULT_LOCALE
130130
}
131131

@@ -218,7 +218,7 @@ export async function compileMdx(
218218
remarkCustomHeadingId,
219219
[remarkHeadings, { isRemoteContent }] satisfies Pluggable,
220220
// structurize should be before remarkHeadings because we attach #id attribute to heading node
221-
flexsearch && ([remarkStructurize, flexsearch] satisfies Pluggable),
221+
search && ([remarkStructurize, search] satisfies Pluggable),
222222
staticImage && remarkStaticImage,
223223
readingTime && remarkReadingTime,
224224
latex && remarkMath,

‎packages/nextra/src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const DEFAULT_LOCALE = 'en-US'
1111

1212
export const DEFAULT_CONFIG = {
1313
staticImage: true,
14-
flexsearch: {
14+
search: {
1515
codeblocks: true
1616
},
1717
codeHighlight: true

‎packages/nextra/src/index.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const nextra: Nextra = nextraConfig =>
4747
return rules
4848
}
4949

50-
const nextraLoaderOptions = {
50+
const loaderOptions = {
5151
...DEFAULT_CONFIG,
5252
...nextraConfig,
5353
locales
@@ -61,14 +61,15 @@ const nextra: Nextra = nextraConfig =>
6161
return {
6262
...nextConfig,
6363
...(nextConfig.output !== 'export' && { rewrites }),
64-
...(hasI18n && {
65-
env: {
64+
env: {
65+
...nextConfig.env,
66+
...(hasI18n && {
6667
NEXTRA_DEFAULT_LOCALE:
67-
nextConfig.i18n?.defaultLocale || DEFAULT_LOCALE,
68-
...nextConfig.env
69-
},
70-
i18n: undefined
71-
}),
68+
nextConfig.i18n?.defaultLocale || DEFAULT_LOCALE
69+
}),
70+
NEXTRA_SEARCH: String(!!loaderOptions.search)
71+
},
72+
...(hasI18n && { i18n: undefined }),
7273
pageExtensions: [
7374
...(nextConfig.pageExtensions || DEFAULT_EXTENSIONS),
7475
...MARKDOWN_EXTENSIONS
@@ -78,7 +79,7 @@ const nextra: Nextra = nextraConfig =>
7879
config.plugins ||= []
7980
config.plugins.push(new NextraPlugin({ locales }))
8081

81-
if (nextraLoaderOptions.flexsearch) {
82+
if (loaderOptions.search) {
8283
config.plugins.push(new NextraSearchPlugin())
8384
}
8485
}
@@ -103,7 +104,7 @@ const nextra: Nextra = nextraConfig =>
103104
options.defaultLoaders.babel,
104105
{
105106
loader: 'nextra/loader',
106-
options: nextraLoaderOptions
107+
options: loaderOptions
107108
}
108109
]
109110
},
@@ -116,7 +117,7 @@ const nextra: Nextra = nextraConfig =>
116117
{
117118
loader: 'nextra/loader',
118119
options: {
119-
...nextraLoaderOptions,
120+
...loaderOptions,
120121
isPageImport: true
121122
}
122123
}
@@ -135,7 +136,7 @@ const nextra: Nextra = nextraConfig =>
135136
options.defaultLoaders.babel,
136137
{
137138
loader: 'nextra/loader',
138-
options: nextraLoaderOptions
139+
options: loaderOptions
139140
}
140141
]
141142
}

‎packages/nextra/src/loader.ts

+15-20
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ async function loader(
5858
isPageImport = false,
5959
theme,
6060
themeConfig,
61-
locales,
6261
defaultShowCopyCode,
63-
flexsearch,
64-
latex,
62+
search,
6563
staticImage,
6664
readingTime: _readingTime,
67-
mdxOptions,
65+
latex,
66+
codeHighlight,
6867
transform,
6968
transformPageOpts,
70-
codeHighlight
69+
mdxOptions,
70+
locales
7171
} = context.getOptions()
7272
context.cacheable(true)
7373

@@ -114,26 +114,21 @@ ${OFFICIAL_THEMES.includes(theme) ? `import '${theme}/style.css'` : ''}`
114114
isAppFileFromNodeModules = mdxPath.includes('/node_modules/')
115115
// Relative path instead of a package name
116116
const themeConfigImport = themeConfig
117-
? `import __nextra_themeConfig from '${slash(path.resolve(themeConfig))}'`
117+
? `import __themeConfig from '${slash(path.resolve(themeConfig))}'`
118118
: ''
119119

120-
return `import __nextra_layout from '${layoutPath}'
120+
const content = isAppFileFromNodeModules
121+
? 'export default function App({ Component, pageProps }) { return <Component {...pageProps} />}'
122+
: [cssImports, source].join('\n')
123+
124+
return `import __layout from '${layoutPath}'
121125
${themeConfigImport}
122-
${
123-
isAppFileFromNodeModules
124-
? 'export default function App({ Component, pageProps }) { return <Component {...pageProps} />}'
125-
: [cssImports, source].join('\n')
126-
}
126+
${content}
127127
128128
const __nextra_internal__ = globalThis[Symbol.for('__nextra_internal__')] ||= Object.create(null)
129129
__nextra_internal__.context ||= Object.create(null)
130-
__nextra_internal__.Layout = __nextra_layout
131-
__nextra_internal__.flexsearch = ${JSON.stringify(flexsearch)}
132-
${
133-
themeConfigImport
134-
? '__nextra_internal__.themeConfig = __nextra_themeConfig'
135-
: ''
136-
}`
130+
__nextra_internal__.Layout = __layout
131+
${themeConfigImport && '__nextra_internal__.themeConfig = __themeConfig'}`
137132
}
138133

139134
const { fileMap } = (await import(
@@ -196,7 +191,7 @@ ${
196191
readingTime: _readingTime,
197192
defaultShowCopyCode,
198193
staticImage,
199-
flexsearch,
194+
search,
200195
latex,
201196
codeHighlight,
202197
route,

‎packages/nextra/src/mdx-plugins/remark-structurize.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Root } from 'mdast'
22
import type { Plugin } from 'unified'
3-
import type { Flexsearch, StructurizedData } from '../types'
3+
import type { Search, StructurizedData } from '../types'
44

55
const CODE_TABLE_QUOTE_LIST = new Set<string>([
66
'code',
@@ -10,7 +10,7 @@ const CODE_TABLE_QUOTE_LIST = new Set<string>([
1010
'mdxJsxFlowElement'
1111
])
1212

13-
export const remarkStructurize: Plugin<[Flexsearch], Root> = options => {
13+
export const remarkStructurize: Plugin<[Search], Root> = options => {
1414
const opts = { codeblocks: true, ...(options as any) }
1515
const structurizedData: StructurizedData = Object.create(null)
1616
let activeSlug = ''

‎packages/nextra/src/setup-page.ts

-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ export function setupNextraPage({
140140
__nextra_internal__.pageMap = pageOpts.pageMap
141141

142142
pageOpts.frontMatter ||= {}
143-
pageOpts.flexsearch = __nextra_internal__.flexsearch
144143
__nextra_internal__.context[route] = {
145144
Content: MDXContent,
146145
pageOpts

‎packages/nextra/src/types.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ export type PageOpts<FrontMatterType = FrontMatter> = {
9191
headings: Heading[]
9292
hasJsxInH1?: boolean
9393
timestamp?: number
94-
flexsearch?: Flexsearch
9594
readingTime?: ReadingTime
9695
}
9796

@@ -103,7 +102,7 @@ export type ReadingTime = {
103102
}
104103

105104
type Theme = string
106-
export type Flexsearch =
105+
export type Search =
107106
| boolean
108107
| {
109108
/**
@@ -134,7 +133,7 @@ export type NextraConfig = {
134133
theme: Theme
135134
themeConfig?: string
136135
defaultShowCopyCode?: boolean
137-
flexsearch?: Flexsearch
136+
search?: Search
138137
staticImage?: boolean
139138
readingTime?: boolean
140139
latex?: boolean
@@ -176,7 +175,6 @@ export type NextraInternalGlobal = typeof globalThis & {
176175
refreshListeners: Record<string, (() => void)[]>
177176
Layout: FC<any>
178177
themeConfig?: ThemeConfig
179-
flexsearch?: Flexsearch
180178
}
181179
}
182180

0 commit comments

Comments
 (0)
Please sign in to comment.