From 880ccba87400e298560041da6fa0b8a5f28e4370 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Wed, 24 Jan 2024 13:08:46 +0800 Subject: [PATCH] revert: "refactor: remove extra methods" This reverts commit a6fe43dfac3c3fa7e54fb99261d055fdb8e51f41. --- e2e/docs/routes/get-pages-path.md | 13 +++++++ e2e/docs/routes/is-page-exist.md | 18 ++++++++++ e2e/docs/routes/resolve.md | 9 +++++ e2e/tests/routes/get-pages-path.cy.ts | 45 ++++++++++++++++++++++++ e2e/tests/routes/is-page-exisit.cy.ts | 22 ++++++++++++ e2e/tests/routes/resolve.cy.ts | 10 ++++++ packages/client/src/components/VPLink.ts | 10 ++---- packages/client/src/helpers/index.ts | 1 + packages/client/src/helpers/router.ts | 38 ++++++++++++++++++++ 9 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 e2e/docs/routes/get-pages-path.md create mode 100644 e2e/docs/routes/is-page-exist.md create mode 100644 e2e/docs/routes/resolve.md create mode 100644 e2e/tests/routes/get-pages-path.cy.ts create mode 100644 e2e/tests/routes/is-page-exisit.cy.ts create mode 100644 e2e/tests/routes/resolve.cy.ts create mode 100644 packages/client/src/helpers/router.ts diff --git a/e2e/docs/routes/get-pages-path.md b/e2e/docs/routes/get-pages-path.md new file mode 100644 index 0000000000..80cba289f2 --- /dev/null +++ b/e2e/docs/routes/get-pages-path.md @@ -0,0 +1,13 @@ +## GetPagesPath + + + + diff --git a/e2e/docs/routes/is-page-exist.md b/e2e/docs/routes/is-page-exist.md new file mode 100644 index 0000000000..ea263787fa --- /dev/null +++ b/e2e/docs/routes/is-page-exist.md @@ -0,0 +1,18 @@ +# isPageExist + +- /: {{isPageExist('/')}} +- /README.md: {{isPageExist('/README.md')}} +- /index.html: {{isPageExist('/index.html')}} +- /not-exist: {{isPageExist('/not-exist')}} +- /not-exist.html: {{isPageExist('/not-exist.html')}} +- /not-exist.md: {{isPageExist('/not-exist.md')}} +- /routes/non-ascii-paths/中文目录名/中文文件名.md: {{isPageExist('/routes/non-ascii-paths/中文目录名/中文文件名.md')}} +- /routes/non-ascii-paths/中文目录名/中文文件名.html: {{isPageExist('/routes/non-ascii-paths/中文目录名/中文文件名.html')}} +- {{encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.md')}}: {{isPageExist(encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.md'))}} +- {{encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html')}}: {{isPageExist(encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html'))}} +- /zh/: {{isPageExist('/zh/')}} +- /zh: {{isPageExist('/zh')}} + + diff --git a/e2e/docs/routes/resolve.md b/e2e/docs/routes/resolve.md new file mode 100644 index 0000000000..0b49d02d5f --- /dev/null +++ b/e2e/docs/routes/resolve.md @@ -0,0 +1,9 @@ +## resolve + +- Clean url: {{JSON.stringify(resolve('/page-data/meta'))}} +- HTML: {{JSON.stringify(resolve('/page-data/meta.html'))}} +- Markdown: {{JSON.stringify(resolve('/page-data/meta.md'))}} + + diff --git a/e2e/tests/routes/get-pages-path.cy.ts b/e2e/tests/routes/get-pages-path.cy.ts new file mode 100644 index 0000000000..1021e1bbe2 --- /dev/null +++ b/e2e/tests/routes/get-pages-path.cy.ts @@ -0,0 +1,45 @@ +it('getPagesPath', () => { + cy.visit('/routes/get-pages-path.html') + + const paths = [ + '/', + // '/layouts/custom-layout.html', + // '/layouts/layout.html', + // '/markdown/anchors.html', + // '/markdown/code-blocks.html', + // '/markdown/emoji.html', + // '/markdown/import-code-blocks.html', + // '/markdown/links/bar.html', + // '/markdown/links/baz.html', + // '/markdown/links/foo.html', + // '/markdown/toc.html', + // '/markdown/vue-components.html', + // '/page-data/frontmatter.html', + // '/page-data/headers.html', + // '/page-data/lang.html', + // '/page-data/meta.html', + // '/page-data/permalink.html', + // '/page-data/title-from-frontmatter.html', + // '/page-data/title-from-h1.html', + // '/routes/non-ascii-paths/', + // '/routes/non-ascii-paths/%E4%B8%AD%E6%96%87%E7%9B%AE%E5%BD%95%E5%90%8D/%E4%B8%AD%E6%96%87%E6%96%87%E4%BB%B6%E5%90%8D.html', + // '/routes/get-pages-path.html', + // '/routes/resolve.html', + // '/routes/is-page-exist.html', + // '/zh/', + // '/404.html', + ] + + const pagePaths: string[] = [] + + // eslint-disable-next-line cypress/unsafe-to-chain-command + cy.get('.e2e-theme-content ul li') + .each((el) => { + pagePaths.push(el.text()) + }) + .then(() => { + paths.forEach((path) => { + expect(pagePaths.includes(path)).to.equal(true) + }) + }) +}) diff --git a/e2e/tests/routes/is-page-exisit.cy.ts b/e2e/tests/routes/is-page-exisit.cy.ts new file mode 100644 index 0000000000..42c5e5052d --- /dev/null +++ b/e2e/tests/routes/is-page-exisit.cy.ts @@ -0,0 +1,22 @@ +it('isPageExist', () => { + cy.visit('/routes/is-page-exist.html') + + const results = [ + '/: true', + '/README.md: true', + '/index.html: true', + '/not-exist: false', + '/not-exist.html: false', + '/not-exist.md: false', + '/routes/non-ascii-paths/中文目录名/中文文件名.md: true', + '/routes/non-ascii-paths/中文目录名/中文文件名.html: true', + `${encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.md')}: true`, + `${encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html')}: true`, + '/zh/: true', + '/zh: false', + ] + + cy.get('.e2e-theme-content ul li').each((el) => { + expect(results.includes(el.text())).to.equal(true) + }) +}) diff --git a/e2e/tests/routes/resolve.cy.ts b/e2e/tests/routes/resolve.cy.ts new file mode 100644 index 0000000000..9f5e6b1812 --- /dev/null +++ b/e2e/tests/routes/resolve.cy.ts @@ -0,0 +1,10 @@ +it('resolve', () => { + cy.visit('/routes/resolve.html') + const result = { path: '/page-data/meta.html', meta: { a: 0, b: 2, c: 3 } } + + cy.get('.e2e-theme-content ul li').each((el) => { + expect(JSON.parse(/: (\{.*\})\s*$/.exec(el.text())![1])).to.deep.include( + result, + ) + }) +}) diff --git a/packages/client/src/components/VPLink.ts b/packages/client/src/components/VPLink.ts index 83857f950b..9b83516062 100644 --- a/packages/client/src/components/VPLink.ts +++ b/packages/client/src/components/VPLink.ts @@ -1,9 +1,7 @@ import { h } from 'vue' import type { FunctionalComponent, VNode } from 'vue' import { useRouter } from 'vue-router' -import { pagesMap, redirectsMap } from '../composables/index.js' -import { withBase } from '../helpers/index.js' -import { resolvers } from '../resolvers.js' +import { resolve, withBase } from '../helpers/index.js' /** * Forked from https://github.com/vuejs/router/blob/941b2131e80550009e5221d4db9f366b1fea3fd5/packages/router/src/RouterLink.ts#L293 @@ -24,7 +22,7 @@ const guardEvent = (event: MouseEvent): boolean | void => { return true } -interface VPLinkProps { +export interface VPLinkProps { to: string } @@ -36,9 +34,7 @@ export const VPLink: FunctionalComponent< } > = ({ to = '' }, { slots }) => { const router = useRouter() - const path = withBase( - resolvers.resolvePagePath(pagesMap.value, redirectsMap.value, to), - ) + const path = withBase(resolve(to).path) return h( 'a', diff --git a/packages/client/src/helpers/index.ts b/packages/client/src/helpers/index.ts index e5726afa68..1be02a4358 100644 --- a/packages/client/src/helpers/index.ts +++ b/packages/client/src/helpers/index.ts @@ -1,2 +1,3 @@ export * from './defineClientConfig.js' +export * from './router.js' export * from './withBase.js' diff --git a/packages/client/src/helpers/router.ts b/packages/client/src/helpers/router.ts new file mode 100644 index 0000000000..25d86244e7 --- /dev/null +++ b/packages/client/src/helpers/router.ts @@ -0,0 +1,38 @@ +import type { PagesMap } from '@internal/pagesMap' +import { pagesMap, redirectsMap } from '../composables/index.js' +import { resolvers } from '../resolvers.js' + +/** + * Get all paths of pages + * + * @returns all paths of pages + */ +export const getPagesPath = (): string[] => Array.from(pagesMap.value.keys()) + +/** + * Check whether the page exists + * + * @param path path of the page + * @returns whether the page exists + */ +export const isPageExist = (path: string): boolean => + pagesMap.value.has( + resolvers.resolvePagePath(pagesMap.value, redirectsMap.value, path), + ) + +/** + * Resolve final path and meta with given path + * + * @param path path of the page + * @returns resolved path and meta + */ +export const resolve = ( + path: string, +): { path: string; meta: PageMeta | null } => { + path = resolvers.resolvePagePath(pagesMap.value, redirectsMap.value, path) + + return { + path, + meta: (pagesMap.value as PagesMap).get(path)?.meta ?? null, + } +}