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

[v3] allow items: Map for type: 'menu' since object can't guaranty the insertion order #2759

Merged
merged 6 commits into from
Mar 4, 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
5 changes: 5 additions & 0 deletions .changeset/tall-cobras-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nextra-theme-docs': minor
---

allow `items: Map` for `type: 'menu'` since object can't guaranty the insertion order
1 change: 0 additions & 1 deletion docs/pages/api/og.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export default async function (req) {
letterSpacing: -4,
backgroundImage: 'linear-gradient(90deg, #fff 40%, #aaa)',
backgroundClip: 'text',
'-webkit-background-clip': 'text',
color: 'transparent'
}}
>
Expand Down
41 changes: 22 additions & 19 deletions examples/swr-site/pages/en/_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@ export default {
about: {
type: 'menu',
title: 'About',
items: {
contributors: {
title: 'Contributors',
href: 'https://github.com/vercel/swr/graphs/contributors',
newWindow: true
},
team: {
title: 'Team'
},
acknowledgement: {
title: 'Acknowledgement'
},
'a-page': {
title: 'A Page'
},
changelog: {
title: 'Changelog'
}
}
items: new Map([
[
'contributors',
{
title: 'Contributors',
href: 'https://github.com/vercel/swr/graphs/contributors',
newWindow: true
}
],
['team', { title: 'Team' }],
['acknowledgement', { title: 'Acknowledgement' }],
['a-page', { title: 'A Page' }],
['changelog', { title: 'Changelog' }],
[
'123',
{
title: 'Last, key is number',
href: 'https://google.com',
newWindow: true
}
]
])
},
examples: {
type: 'page',
Expand Down
6 changes: 6 additions & 0 deletions packages/nextra-theme-blog/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@
- Updated dependencies [576cb6f1]
- [email protected]

## 2.13.4

### Patch Changes

- [email protected]

## 2.13.3

### Patch Changes
Expand Down
8 changes: 8 additions & 0 deletions packages/nextra-theme-docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@
- Updated dependencies [576cb6f1]
- [email protected]

## 2.13.4

### Patch Changes

- f7fc10b4: fix for the memory leak issue in the `highlight-matches.tsx`
component when search query contain multiple whitespaces
- [email protected]

## 2.13.3

### Patch Changes
Expand Down
33 changes: 17 additions & 16 deletions packages/nextra-theme-docs/src/components/highlight-matches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@ export const HighlightMatches = memo<MatchArgs>(function HighlightMatches({
}
const splitText = value.split('')
const escapedSearch = escapeStringRegexp(match.trim())
const regexp = new RegExp(escapedSearch.replaceAll(' ', '|'), 'ig')
const regexp = new RegExp(escapedSearch.replaceAll(/\s+/g, '|'), 'ig')
let result
let index = 0
const content: (string | ReactNode)[] = []

while (
(result = regexp.exec(value)) &&
// case `> ` replaced previously to `>||` + some character provoke memory leak because
// `RegExp#exec` will always return a match
regexp.lastIndex !== 0
) {
const before = splitText.splice(0, result.index - index).join('')
const after = splitText.splice(0, regexp.lastIndex - result.index).join('')
content.push(
before,
<span key={result.index} className="_text-primary-600">
{after}
</span>
)
index = regexp.lastIndex
while ((result = regexp.exec(value))) {
if (result.index === regexp.lastIndex) {
regexp.lastIndex++
} else {
const before = splitText.splice(0, result.index - index).join('')
const after = splitText
.splice(0, regexp.lastIndex - result.index)
.join('')
content.push(
before,
<span key={result.index} className="_text-primary-600">
{after}
</span>
)
index = regexp.lastIndex
}
}

return (
Expand Down
6 changes: 5 additions & 1 deletion packages/nextra-theme-docs/src/components/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function NavbarMenu({
const routes = Object.fromEntries(
(menu.children || []).map(route => [route.name, route])
)
const entries =
items instanceof Map
? Array.from(items.entries())
: Object.entries(items || {})

return (
<Menu as="div" className="_relative">
Expand All @@ -54,7 +58,7 @@ function NavbarMenu({
as={Menu.Items}
className="_absolute _right-0 _z-20 _mt-1 _max-h-64 _min-w-full _overflow-auto _rounded-md _ring-1 _ring-black/5 _bg-white _py-1 _text-sm _shadow-lg dark:_ring-white/20 dark:_bg-neutral-800"
>
{Object.entries(items || {}).map(([key, item]) => (
{entries.map(([key, item]) => (
<Menu.Item key={key}>
{({ active }) => (
<Anchor
Expand Down
2 changes: 2 additions & 0 deletions packages/nextra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@

- d8a406b4: add `"sideEffects": false` for better tree-shaking

## 2.13.4

## 2.13.3

### Patch Changes
Expand Down
54 changes: 32 additions & 22 deletions packages/nextra/__test__/__snapshots__/page-map.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ exports[`Page Process > should match i18n site page maps 1`] = `
{
"data": {
"about": {
"items": {
"a-page": {
"title": "A Page",
"items": Map {
"contributors" => {
"href": "https://github.com/vercel/swr/graphs/contributors",
"newWindow": true,
"title": "Contributors",
},
"team" => {
"title": "Team",
},
"acknowledgement": {
"acknowledgement" => {
"title": "Acknowledgement",
},
"changelog": {
"a-page" => {
"title": "A Page",
},
"changelog" => {
"title": "Changelog",
},
"contributors": {
"href": "https://github.com/vercel/swr/graphs/contributors",
"123" => {
"href": "https://google.com",
"newWindow": true,
"title": "Contributors",
},
"team": {
"title": "Team",
"title": "Last, key is number",
},
},
"title": "About",
Expand Down Expand Up @@ -637,23 +642,28 @@ exports[`Page Process > should match i18n site page maps 1`] = `
{
"data": {
"about": {
"items": {
"a-page": {
"title": "A Page",
"items": Map {
"contributors" => {
"href": "https://github.com/vercel/swr/graphs/contributors",
"newWindow": true,
"title": "Contributors",
},
"team" => {
"title": "Team",
},
"acknowledgement": {
"acknowledgement" => {
"title": "Acknowledgement",
},
"changelog": {
"a-page" => {
"title": "A Page",
},
"changelog" => {
"title": "Changelog",
},
"contributors": {
"href": "https://github.com/vercel/swr/graphs/contributors",
"123" => {
"href": "https://google.com",
"newWindow": true,
"title": "Contributors",
},
"team": {
"title": "Team",
"title": "Last, key is number",
},
},
"title": "About",
Expand Down
Loading