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

👷‍♂️ [WIP] add versioning support #840

Closed
wants to merge 1 commit into from
Closed
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/grumpy-jeans-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nextra-theme-docs': patch
---

add versioning support
1 change: 0 additions & 1 deletion examples/docs/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const withNextra = require('nextra')({
import nextra from 'nextra'

const withNextra = nextra({
theme: 'nextra-theme-docs',
themeConfig: './src/theme.config.js',
themeConfig: './theme.config.jsx',
unstable_staticImage: true,
unstable_flexsearch: {
codeblock: false
}
})

module.exports = withNextra({
export default withNextra({
reactStrictMode: true
})
27 changes: 22 additions & 5 deletions examples/docs/src/pages/_meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
{
"index": "Introduction",
"get-started": "Get Started",
"features": "Features",
"themes": "Themes",
"advanced": "Advanced"
"index": {
"type": "page",
"display": "hidden",
"theme": {
"timestamp": false
}
},
"docs": {
"type": "page",
"title": "v3.0.0",
"versioned": true
},
"v2.13.13": {
"type": "page",
"versioned": true
},
"nextra_link": {
"title": "Nextra ↗",
"href": "https://github.com/shuding/nextra",
"type": "page",
"newWindow": true
}
}
6 changes: 6 additions & 0 deletions examples/docs/src/pages/docs/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"index": "Get Started",
"features": "Features",
"themes": "Themes",
"advanced": "Advanced"
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const withNextra = require('nextra')({
themeConfig: './theme.config.js'
// optional: add `unstable_staticImage: true` to enable Nextra's auto image import
})

module.exports = withNextra()
```

Expand Down
1 change: 1 addition & 0 deletions examples/docs/src/pages/v2.13.13/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions examples/docs/src/pages/v2.13.13/hello.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# New doc
1 change: 1 addition & 0 deletions examples/docs/src/pages/v2.13.13/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# New doc
File renamed without changes.
1 change: 0 additions & 1 deletion packages/nextra-theme-docs/src/components/head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export function Head(): ReactElement {
:root {
--nextra-primary-hue: ${lightHue}deg;
--nextra-navbar-height: 4rem;
--nextra-menu-height: 3.75rem;
--nextra-banner-height: 2.5rem;
}

Expand Down
20 changes: 19 additions & 1 deletion packages/nextra-theme-docs/src/components/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useConfig, useMenu } from '../contexts'
import { MenuIcon } from 'nextra/icons'
import { Item, PageItem, MenuItem, renderComponent, getFSRoute } from '../utils'
import { Anchor } from './anchor'
import { VersionSwitch } from './version-switch'
import { DEFAULT_LOCALE } from '../constants'

export type NavBarProps = {
Expand Down Expand Up @@ -83,6 +84,21 @@ export function Navbar({ flatDirectories, items }: NavBarProps): ReactElement {
const activeRoute = getFSRoute(asPath, locale)
const { menu, setMenu } = useMenu()

const { versioned, filteredItems } = items.reduce<{
versioned: PageItem[]
filteredItems: (PageItem | MenuItem)[]
}>(
(acc, curr) => {
if ('versioned' in curr && curr.versioned) {
acc.versioned.push(curr)
} else {
acc.filteredItems.push(curr)
}
return acc
},
{ versioned: [], filteredItems: [] }
)

return (
<div className="nextra-nav-container nx-sticky nx-top-0 nx-z-20 nx-w-full nx-bg-transparent">
<div
Expand All @@ -106,7 +122,9 @@ export function Navbar({ flatDirectories, items }: NavBarProps): ReactElement {
{renderComponent(config.logo)}
</div>
)}
{items.map(pageOrMenu => {
{versioned.length > 0 && <VersionSwitch options={versioned} />}

{filteredItems.map(pageOrMenu => {
if (pageOrMenu.display === 'hidden') return null

if (pageOrMenu.type === 'menu') {
Expand Down
46 changes: 46 additions & 0 deletions packages/nextra-theme-docs/src/components/version-switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { ReactElement, useEffect, useState } from 'react'
import { ArrowRightIcon } from 'nextra/icons'
import { useRouter } from 'next/router'
import { Select } from './select'
import { PageItem } from '../utils'

export function VersionSwitch({
options
}: {
options: PageItem[]
}): ReactElement {
const router = useRouter()
const [route, setRoute] = useState('')
useEffect(() => {
const newRoute =
options.find(opt => router.route.startsWith(opt.route))?.route || ''
setRoute(newRoute)
}, [router.route])

const selected = route ? options.find(opt => opt.route === route) : options[0]
return (
<Select
className="nx-flex nx-gap-1 nx-items-center"
onChange={option => {
setRoute(option.key)
router.push(option.key)
}}
selected={{
key: route,
name: (
<>
{route ? selected?.title : 'Docs'}
<ArrowRightIcon
className="nx-shrink-0 nx-h-3.5 nx-w-3.5"
pathClassName="[[aria-expanded='true']>svg>&]:nx-rotate-[270deg] nx-origin-center nx-transition-transform nx-rotate-90"
/>
</>
)
}}
options={options.map(o => ({
key: o.route,
name: o.title
}))}
/>
)
}
1 change: 1 addition & 0 deletions packages/nextra-theme-docs/src/utils/normalize-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface PageItem extends MdxFile {
display?: Display
withIndexPage?: boolean
isUnderCurrentDocsTree?: boolean
versioned?: true
}

export interface MenuItem extends MdxFile {
Expand Down