Skip to content

Commit

Permalink
use title package in nextra to determine sidebar title based on pag…
Browse files Browse the repository at this point in the history
…ename (#814)

* use `title` package in nextra to determine sidebar title based on pagename

* update snapshots

* fix types check
  • Loading branch information
dimaMachina committed Sep 6, 2022
1 parent d6d5ab8 commit 93d028b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-shrimps-cheat.md
@@ -0,0 +1,5 @@
---
'nextra': patch
---

use `title` package in nextra to determine sidebar title based on pagename
2 changes: 1 addition & 1 deletion packages/nextra-theme-docs/src/utils/normalize-pages.ts
@@ -1,7 +1,7 @@
import { PageMapItem } from 'nextra'
import { DEFAULT_PAGE_THEME } from '../constants'
import { PageTheme } from '../types'
import { Folder, MdxFile } from 'nextra/src/types'
import { Folder, MdxFile } from 'nextra'

function extendMeta(
meta: string | Record<string, any> = {},
Expand Down
10 changes: 5 additions & 5 deletions packages/nextra/__test__/__snapshots__/context.test.ts.snap
Expand Up @@ -1035,7 +1035,7 @@ exports[`context > getAllPages() > should work 1`] = `
"kind": "MdxPage",
"locale": "en-US",
"meta": {
"title": "loooooooooooooooooooong-title",
"title": "Loooooooooooooooooooong Title",
},
"name": "loooooooooooooooooooong-title",
"route": "/docs/advanced/more/loooooooooooooooooooong-title",
Expand Down Expand Up @@ -1279,7 +1279,7 @@ exports[`context > getAllPages() > should work 1`] = `
"kind": "MdxPage",
"locale": "en-US",
"meta": {
"title": "code-block-without-language",
"title": "Code Block without Language",
},
"name": "code-block-without-language",
"route": "/docs/code-block-without-language",
Expand Down Expand Up @@ -2749,7 +2749,7 @@ exports[`context > getCurrentLevelPages() > should work 1`] = `
"kind": "MdxPage",
"locale": "en-US",
"meta": {
"title": "loooooooooooooooooooong-title",
"title": "Loooooooooooooooooooong Title",
},
"name": "loooooooooooooooooooong-title",
"route": "/docs/advanced/more/loooooooooooooooooooong-title",
Expand Down Expand Up @@ -2993,7 +2993,7 @@ exports[`context > getCurrentLevelPages() > should work 1`] = `
"kind": "MdxPage",
"locale": "en-US",
"meta": {
"title": "code-block-without-language",
"title": "Code Block without Language",
},
"name": "code-block-without-language",
"route": "/docs/code-block-without-language",
Expand Down Expand Up @@ -3162,7 +3162,7 @@ exports[`context > getPagesUnderRoute() > should work 1`] = `
"kind": "MdxPage",
"locale": "en-US",
"meta": {
"title": "loooooooooooooooooooong-title",
"title": "Loooooooooooooooooooong Title",
},
"name": "loooooooooooooooooooong-title",
"route": "/docs/advanced/more/loooooooooooooooooooong-title",
Expand Down
6 changes: 3 additions & 3 deletions packages/nextra/__test__/__snapshots__/page-map.test.ts.snap
Expand Up @@ -196,7 +196,7 @@ exports[`Page Process > pageMap en-US 1`] = `
},
"title": "Change Log",
},
"code-block-without-language": "code-block-without-language",
"code-block-without-language": "Code Block without Language",
"conditional-fetching": "Conditional Data Fetching",
"data-fetching": "Data Fetching",
"error-handling": {
Expand Down Expand Up @@ -289,7 +289,7 @@ exports[`Page Process > pageMap en-US 1`] = `
},
{
"data": {
"loooooooooooooooooooong-title": "loooooooooooooooooooong-title",
"loooooooooooooooooooong-title": "Loooooooooooooooooooong Title",
},
"kind": "Meta",
"locale": "en-US",
Expand Down Expand Up @@ -695,7 +695,7 @@ exports[`Page Process > pageMap zh-CN 1`] = `
},
{
"data": {
"loooooooooooooooooooong-title": "loooooooooooooooooooong-title",
"loooooooooooooooooooong-title": "Loooooooooooooooooooong Title",
},
"kind": "Meta",
"locale": "en-US",
Expand Down
3 changes: 2 additions & 1 deletion packages/nextra/package.json
Expand Up @@ -86,7 +86,8 @@
"remark-gfm": "^3.0.1",
"remark-reading-time": "^2.0.1",
"shiki": "0.10.1",
"slash": "^3.0.0"
"slash": "^3.0.0",
"title": "^3.5.3"
},
"peerDependencies": {
"next": ">=9.5.3",
Expand Down
9 changes: 9 additions & 0 deletions packages/nextra/src/env.d.ts
Expand Up @@ -5,3 +5,12 @@ declare module globalThis {
route: string
}
}

declare module 'title' {
export default function title(
title: string,
special?: {
special: string[]
}
)
}
6 changes: 5 additions & 1 deletion packages/nextra/src/plugin.ts
Expand Up @@ -16,6 +16,7 @@ import slash from 'slash'
import grayMatter from 'gray-matter'
import { findPagesDir } from 'next/dist/lib/find-pages-dir.js'
import { Compiler } from 'webpack'
import title from 'title'

import { restoreCache } from './content-dump'
import { CWD, MARKDOWN_EXTENSION_REGEX, META_FILENAME } from './constants'
Expand Down Expand Up @@ -103,7 +104,10 @@ export async function collectFiles(
)
const defaultMeta: [string, string][] = mdxPages
.filter(item => item.locale === locale)
.map(item => [item.name, item.frontMatter?.title || item.name])
.map(item => [
item.name,
item.frontMatter?.title || title(item.name.replace(/[-_]/g, ' '))
])
const metaFilename = locale
? META_FILENAME.replace('.', `.${locale}.`)
: META_FILENAME
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93d028b

Please sign in to comment.