Skip to content

Commit

Permalink
[v3] allow items: Map for type: 'menu' since object can't guarant…
Browse files Browse the repository at this point in the history
…y the insertion order (#2759)

* fix: Invalid webkit style (#2750)

* fix: Invalid webkit style

"-webkit-background-clip" is an invalid style for JSX, this commit switches this to the "WebkitBackgroundClip" style.

* refactor: Remove webkit styling

* Comprehensive fix for memory leak in highlight-matches component (#2746)

* fix: resolve memory leak issue in highlight-matches component

A critical update has been made to prevent potential infinite loops that could occur in certain scenarios, such as when a match is followed by two spaces and a character.

* refactor: apply simplified regex approach to resolve memory leak

* add changeset

---------

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

* Version Packages (#2752)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* add changeset

* update snapshots

---------

Co-authored-by: Jun Xiang <[email protected]>
Co-authored-by: 이재민 <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
5 people authored Mar 4, 2024
1 parent 9e9e3dc commit 962cea6
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 59 deletions.
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

0 comments on commit 962cea6

Please sign in to comment.