Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 1bd92a9

Browse files
committed
feat(docs): add edit page url and prev and next links
1 parent 6b0a52f commit 1bd92a9

File tree

7 files changed

+189
-11
lines changed

7 files changed

+189
-11
lines changed

website/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"vite-plugin-components": "^0.8.3",
4141
"vite-plugin-icons": "^0.3.3",
4242
"vite-plugin-md": "^0.6.0",
43-
"vite-plugin-mdx-vue": "^1.3.0",
43+
"vite-plugin-mdx-vue": "^1.6.0",
4444
"vite-plugin-pages": "^0.9.2",
4545
"vite-ssg": "^0.9.2"
4646
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<script lang="tsx">
2+
import { defineComponent, renderSlot, SetupContext } from 'vue'
3+
4+
type PaginationLinkProps = {
5+
href?: string
6+
label?: string
7+
} & {
8+
[key: string]: any
9+
}
10+
11+
const PaginationLink = (
12+
{ href, label, ...rest }: PaginationLinkProps,
13+
{ slots }: SetupContext
14+
) => (
15+
<router-link to={href} custom>
16+
{
17+
// converted router-link v-slot to jsx
18+
// https://router.vuejs.org/api/#v-slot-api-3-1-0
19+
}
20+
{({ href, navigate }: any) => (
21+
<c-link
22+
href={href}
23+
onClick={navigate}
24+
_hover={{
25+
textDecor: 'none',
26+
}}
27+
flex="1"
28+
borderRadius="md"
29+
{...rest}
30+
>
31+
<c-text fontSize="sm" px="2">
32+
{label}
33+
</c-text>
34+
<c-text mt="1" fontSize="lg" fontWeight="bold" color="teal.400">
35+
{renderSlot(slots, 'default')}
36+
</c-text>
37+
</c-link>
38+
)}
39+
</router-link>
40+
)
41+
42+
export default defineComponent({
43+
props: {
44+
previous: Object,
45+
next: Object,
46+
},
47+
setup(props) {
48+
return () => {
49+
return (
50+
<c-simple-grid
51+
as="nav"
52+
aria-label="Pagination"
53+
spacing="40px"
54+
my="64px"
55+
columns={2}
56+
>
57+
{props.previous ? (
58+
<PaginationLink
59+
textAlign="left"
60+
label="Previous"
61+
href={props.previous.path}
62+
rel="prev"
63+
>
64+
<c-icon
65+
name="chevron-left"
66+
mr="1"
67+
fontSize="1.2em"
68+
lineHeight="1em"
69+
d="inline-block"
70+
verticalAlign="middle"
71+
></c-icon>
72+
{props.previous.title}
73+
</PaginationLink>
74+
) : (
75+
<div />
76+
)}
77+
{props.next ? (
78+
<PaginationLink
79+
textAlign="right"
80+
label="Next"
81+
href={props.next.path}
82+
rel="next"
83+
>
84+
{props.next.title}
85+
<c-icon
86+
name="chevron-right"
87+
ml="1"
88+
fontSize="1.2em"
89+
lineHeight="1em"
90+
d="inline-block"
91+
verticalAlign="middle"
92+
></c-icon>
93+
</PaginationLink>
94+
) : (
95+
<div />
96+
)}
97+
</c-simple-grid>
98+
)
99+
}
100+
},
101+
})
102+
</script>

website/src/docs-theme/layout/LayoutMdx.vue

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<template #sidebar>
55
<app-sidebar :routes="routes" />
66
</template>
7+
<template #pagination>
8+
<app-pagination
9+
:previous="routeContext.prevRoute"
10+
:next="routeContext.nextRoute"
11+
></app-pagination>
12+
</template>
713
</page-container>
814
</template>
915

@@ -12,20 +18,35 @@ import { defineComponent, PropType } from 'vue'
1218
import { getHeadings } from '@/docs-theme/utils/get-headings'
1319
import { getRoutes } from '@/docs-theme/utils/get-routes'
1420
import { useRoute } from 'vue-router'
21+
import { getRouteContext } from '@/docs-theme/utils/get-route-context'
22+
import {
23+
findRouteByPath,
24+
removeFromLast,
25+
} from '@/docs-theme/utils/find-route-by-path'
1526
1627
export default defineComponent({
1728
props: {
1829
frontmatter: {
19-
type: Object as PropType<{ title: string }>,
30+
// prettier-ignore
31+
type: Object as PropType<{ title: string, slug: string }>,
32+
default: () => ({}),
2033
},
2134
},
2235
setup(props, { slots }) {
2336
const { path } = useRoute()
2437
const routes = getRoutes(path)
2538
const headings = getHeadings(slots)
39+
40+
const route = findRouteByPath(
41+
removeFromLast(props.frontmatter.slug, '#'),
42+
routes
43+
)
44+
const routeContext = getRouteContext(route, routes)
45+
2646
return {
2747
headings,
2848
routes,
49+
routeContext,
2950
}
3051
},
3152
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { RouteItem } from './get-route-context'
2+
3+
export function removeFromLast(path: string, key: string) {
4+
const index = path.lastIndexOf(key)
5+
return index === -1 ? path : path.substring(0, index)
6+
}
7+
8+
// @ts-ignore
9+
export function findRouteByPath(path: string, routes: RouteItem[]): RouteItem {
10+
for (const route of routes) {
11+
if (route.path && removeFromLast(route.path, '.') === path) {
12+
return route
13+
}
14+
15+
const childPath = route.routes && findRouteByPath(path, route.routes)
16+
17+
if (childPath) {
18+
return childPath
19+
}
20+
}
21+
}

website/src/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ export const createApp = ViteSSG(App, { routes }, ({ app, isClient }) => {
9090
path: `<path fill="currentColor" d="M904 160H120c-4.4 0-8 3.6-8 8v64c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-64c0-4.4-3.6-8-8-8zm0 624H120c-4.4 0-8 3.6-8 8v64c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-64c0-4.4-3.6-8-8-8zm0-312H120c-4.4 0-8 3.6-8 8v64c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-64c0-4.4-3.6-8-8-8z"></path>`,
9191
viewBox: '0 0 1024 1024',
9292
},
93+
chevronLeft: {
94+
path: `<path fill="currentColor" d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"></path>`,
95+
viewBox: '0 0 24 24',
96+
},
97+
chevronRight: {
98+
path: `<path fill="currentColor" d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"></path>`,
99+
viewBox: '0 0 24 24',
100+
},
93101
},
94102
},
95103
})

website/vite.config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ import remarkAutolinkHeadings from 'remark-autolink-headings'
1515
// @ts-ignore
1616
import remarkSlug from 'remark-slug'
1717

18+
const getEditPageUrl = (resourcePath: string) => {
19+
const EDIT_PAGE_PATH =
20+
'https://github.com/chakra-ui/chakra-ui-vue-next/edit/develop/website/'
21+
const editUrl = EDIT_PAGE_PATH + resourcePath
22+
return editUrl
23+
}
24+
25+
const getPageSlug = (resourcePath: string) => {
26+
const { dir, name } = path.parse(resourcePath)
27+
const dirPath = dir.replace('src/pages', '')
28+
const fullPath = path.join(...[dirPath, name])
29+
return fullPath
30+
}
31+
1832
/**
1933
*
2034
* @param html Page HTML
@@ -58,6 +72,18 @@ const config: UserConfig = {
5872
)
5973
return options
6074
},
75+
extendFrontmatter: {
76+
process: (_mdxContent, frontmatter) => {
77+
const editUrl = getEditPageUrl(frontmatter.__resourcePath)
78+
const slug = getPageSlug(frontmatter.__resourcePath)
79+
80+
return {
81+
...frontmatter,
82+
editUrl,
83+
slug,
84+
}
85+
},
86+
},
6187
}),
6288
Pages({
6389
extensions: ['vue', 'mdx'],

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13978,13 +13978,13 @@ vite-plugin-md@^0.6.0:
1397813978
gray-matter "^4.0.2"
1397913979
markdown-it "^12.0.4"
1398013980

13981-
vite-plugin-mdx-vue@^1.3.0:
13982-
version "1.3.0"
13983-
resolved "https://registry.yarnpkg.com/vite-plugin-mdx-vue/-/vite-plugin-mdx-vue-1.3.0.tgz#dd75cc0375c47797679110b26d0666877bf118c7"
13984-
integrity sha512-cHdi/UH+xIqZaoLmYMtNBrKuEHUMXKa7qtLuEZ8XD4ExBoJ79ZGGovJuWwjBr5Babm/CYHr4RRFgMnL5YZxaww==
13981+
vite-plugin-mdx-vue@^1.6.0:
13982+
version "1.6.0"
13983+
resolved "https://registry.yarnpkg.com/vite-plugin-mdx-vue/-/vite-plugin-mdx-vue-1.6.0.tgz#e2e580d2c86b5dd172281e73ba4b6c326a14afac"
13984+
integrity sha512-blUHqDQVe4gAf3YL0H07qCvFyMcstQM4v2XBJILQ8Nh7xrLlJi/cpjWJZPI1K5/EZ9xD7G181ubGMQr9WeGc5A==
1398513985
dependencies:
1398613986
"@vue/server-renderer" "^3.0.9"
13987-
vue-mdx-bundler "1.3.0"
13987+
vue-mdx-bundler "1.6.0"
1398813988
xdm "^1.5.1"
1398913989

1399013990
vite-plugin-pages@^0.9.2:
@@ -14109,10 +14109,10 @@ vue-jest@^5.0.0-alpha.7:
1410914109
extract-from-css "^0.4.4"
1411014110
tsconfig "^7.0.0"
1411114111

14112-
vue-mdx-bundler@1.3.0:
14113-
version "1.3.0"
14114-
resolved "https://registry.yarnpkg.com/vue-mdx-bundler/-/vue-mdx-bundler-1.3.0.tgz#c08e0cbe3fc21a314d3d1b72d7bfb2377540dcba"
14115-
integrity sha512-pIteIYX5ez2FmI0dol5p1XgiEzHacAPSFyAOdRmj+HNI/ZeyEKbN50OXCNqgCal/ACA/ZCaRBX0lZ7+arCn9Aw==
14112+
vue-mdx-bundler@1.6.0:
14113+
version "1.6.0"
14114+
resolved "https://registry.yarnpkg.com/vue-mdx-bundler/-/vue-mdx-bundler-1.6.0.tgz#1851b17dbb30eea39538c35d0c793aec0e06ddc4"
14115+
integrity sha512-PIjzktZgylZGBbJbr5BkDpJdK3XYT4Cz5kglB7lxi0qrb5+qqhcS8gvihZLApProtMiTaPeh8C8yK6au+somEw==
1411614116
dependencies:
1411714117
"@babel/core" "^7.13.16"
1411814118
"@babel/runtime" "^7.13.17"

0 commit comments

Comments
 (0)