Skip to content

Commit

Permalink
feat: rename pagesMap to routes, revert page meta back to route meta
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 2, 2024
1 parent 521139e commit 9cdca77
Show file tree
Hide file tree
Showing 41 changed files with 321 additions and 340 deletions.
6 changes: 3 additions & 3 deletions e2e/docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export default defineUserConfig({
theme: e2eTheme(),

extendsPage: (page) => {
if (page.path === '/page-data/meta.html') {
page.meta = {
if (page.path === '/page-data/route-meta.html') {
page.routeMeta = {
a: 1,
b: 2,
...page.meta,
...page.routeMeta,
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/docs/404.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
meta:
routeMeta:
notFound: true
---
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
meta:
routeMeta:
a: 0
c: 3
---
43 changes: 0 additions & 43 deletions e2e/docs/router/resolve-page.md

This file was deleted.

43 changes: 43 additions & 0 deletions e2e/docs/router/resolve-route.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## resolve

### Path

#### Index

- Clean URL: {{ JSON.stringify(resolveRoute('/')) }}
- HTML: {{ JSON.stringify(resolveRoute('/index.html')) }}
- Markdown: {{ JSON.stringify(resolveRoute('/README.md')) }}

#### Non-Index

- Clean URL: {{ JSON.stringify(resolveRoute('/router/resolve-route')) }}
- HTML: {{ JSON.stringify(resolveRoute('/router/resolve-route.html')) }}
- Markdown: {{ JSON.stringify(resolveRoute('/router/resolve-route.md')) }}

#### Non-ASCII

- Clean URL: {{ JSON.stringify(resolveRoute('/routes/non-ascii-paths/中文目录名/中文文件名')) }}
- HTML: {{ JSON.stringify(resolveRoute('/routes/non-ascii-paths/中文目录名/中文文件名.html')) }}
- Markdown: {{ JSON.stringify(resolveRoute('/routes/non-ascii-paths/中文目录名/中文文件名.md')) }}

#### Non-ASCII Encoded

- Clean URL: {{ JSON.stringify(resolveRoute(encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名'))) }}
- HTML: {{ JSON.stringify(resolveRoute(encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html'))) }}
- Markdown: {{ JSON.stringify(resolveRoute(encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.md'))) }}

#### Non-Existent

- Clean URL: {{ JSON.stringify(resolveRoute('/non-existent')) }}
- HTML: {{ JSON.stringify(resolveRoute('/non-existent.html')) }}
- Markdown: {{ JSON.stringify(resolveRoute('/non-existent.md')) }}

#### Route Meta

- Clean URL: {{ JSON.stringify(resolveRoute('/page-data/route-meta')) }}
- HTML: {{ JSON.stringify(resolveRoute('/page-data/route-meta.html')) }}
- Markdown: {{ JSON.stringify(resolveRoute('/page-data/route-meta.md')) }}

<script setup>
import { resolveRoute } from 'vuepress/client'
</script>
59 changes: 0 additions & 59 deletions e2e/tests/router/resolve-page.cy.ts

This file was deleted.

59 changes: 59 additions & 0 deletions e2e/tests/router/resolve-route.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const testCases = [
{
selector: '#index',
expected: {
path: '/',
meta: {},
},
},
{
selector: '#non-index',
expected: {
path: '/router/resolve-route.html',
meta: {},
},
},
{
selector: '#non-ascii',
expected: {
path: encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html'),
meta: {},
},
},
{
selector: '#non-ascii-encoded',
expected: {
path: encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html'),
meta: {},
},
},
{
selector: '#non-existent',
expected: {
path: '/non-existent.html',
meta: { notFound: true },
},
},
{
selector: '#route-meta',
expected: {
path: '/page-data/route-meta.html',
meta: { a: 0, b: 2, c: 3 },
},
},
]

const parseResolvedRouteFromElement = (el: Cypress.JQueryWithSelector) =>
JSON.parse(/: (\{.*\})\s*$/.exec(el.text())![1])

it('should resolve routes correctly', () => {
cy.visit('/router/resolve-route.html')

testCases.forEach(({ selector, expected }) => {
cy.get(`.e2e-theme-content ${selector} + ul > li`).each((el) => {
const resolvedRoute = parseResolvedRouteFromElement(el)
expect(resolvedRoute.path).to.equal(expected.path)
expect(resolvedRoute.meta).to.deep.equal(expected.meta)
})
})
})
6 changes: 3 additions & 3 deletions packages/cli/src/commands/dev/handlePageAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createPage,
preparePage,
preparePageComponent,
preparePagesMap,
prepareRoutes,
} from '@vuepress/core'
import type { App, Page } from '@vuepress/core'

Expand Down Expand Up @@ -33,8 +33,8 @@ export const handlePageAdd = async (
await preparePageComponent(app, page)
await preparePage(app, page)

// prepare pages entry
await preparePagesMap(app)
// prepare routes file
await prepareRoutes(app)

return page
}
12 changes: 6 additions & 6 deletions packages/cli/src/commands/dev/handlePageChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createPage,
preparePage,
preparePageComponent,
preparePagesMap,
prepareRoutes,
} from '@vuepress/core'
import type { App, Page } from '@vuepress/core'

Expand Down Expand Up @@ -37,12 +37,12 @@ export const handlePageChange = async (
await preparePage(app, pageNew)

const isPathChanged = pageOld.path !== pageNew.path
const isMetaChanged =
JSON.stringify(pageOld.meta) !== JSON.stringify(pageNew.meta)
const isRouteMetaChanged =
JSON.stringify(pageOld.routeMeta) !== JSON.stringify(pageNew.routeMeta)

// prepare pages map if the path or meta is changed
if (isPathChanged || isMetaChanged) {
await preparePagesMap(app)
// prepare routes file if the path or route meta is changed
if (isPathChanged || isRouteMetaChanged) {
await prepareRoutes(app)
}

return [pageOld, pageNew]
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/dev/handlePageUnlink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { preparePagesMap } from '@vuepress/core'
import { prepareRoutes } from '@vuepress/core'
import type { App, Page } from '@vuepress/core'

/**
Expand All @@ -21,8 +21,8 @@ export const handlePageUnlink = async (
// remove the old page
app.pages.splice(pageIndex, 1)

// re-prepare page files
await preparePagesMap(app)
// re-prepare routes file
await prepareRoutes(app)

return page
}
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"external": [
"@internal/clientConfigs",
"@internal/layoutComponents",
"@internal/pagesMap",
"@internal/routes",
"@internal/siteData"
],
"format": [
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/components/Content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { computed, defineAsyncComponent, defineComponent, h } from 'vue'
import { usePageData } from '../composables/index.js'
import { resolvePage } from '../router/index.js'
import { resolveRoute } from '../router/index.js'

/**
* Markdown rendered content
Expand All @@ -20,8 +20,8 @@ export const Content = defineComponent({
setup(props) {
const pageData = usePageData()
const pageComponent = computed(() => {
const page = resolvePage(props.path || pageData.value.path)
return defineAsyncComponent(() => page.loader().then(({ comp }) => comp))
const route = resolveRoute(props.path || pageData.value.path)
return defineAsyncComponent(() => route.loader().then(({ comp }) => comp))
})

return () => h(pageComponent.value)
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/components/VPLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { h } from 'vue'
import type { FunctionalComponent, VNode } from 'vue'
import { useRouter } from 'vue-router'
import { withBase } from '../helpers/index.js'
import { resolvePagePath } from '../router/index.js'
import { resolveRoutePath } from '../router/index.js'

/**
* Forked from https://github.com/vuejs/router/blob/941b2131e80550009e5221d4db9f366b1fea3fd5/packages/router/src/RouterLink.ts#L293
Expand Down Expand Up @@ -35,7 +35,7 @@ export const VPLink: FunctionalComponent<
}
> = ({ to = '' }, { slots }) => {
const router = useRouter()
const path = withBase(resolvePagePath(to))
const path = withBase(resolveRoutePath(to))

return h(
'a',
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export * from './pageHead.js'
export * from './pageHeadTitle.js'
export * from './pageLang.js'
export * from './pageLayout.js'
export * from './pagesMap.js'
export * from './routeLocale.js'
export * from './routes.js'
export * from './siteData.js'
export * from './siteLocaleData.js'
export * from './updateHead.js'
11 changes: 0 additions & 11 deletions packages/client/src/composables/pagesMap.ts

This file was deleted.

11 changes: 11 additions & 0 deletions packages/client/src/composables/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { redirects, routes } from '../router/index.js'

/**
* Returns the ref of pages map
*/
export const useRedirects = (): typeof redirects => redirects

/**
* Returns the ref of routes map
*/
export const useRoutes = (): typeof routes => routes
Loading

0 comments on commit 9cdca77

Please sign in to comment.