Skip to content

Commit

Permalink
revert: "refactor: remove extra methods"
Browse files Browse the repository at this point in the history
This reverts commit a6fe43d.
  • Loading branch information
Mister-Hope committed Jan 24, 2024
1 parent 46bba07 commit 880ccba
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 7 deletions.
13 changes: 13 additions & 0 deletions e2e/docs/routes/get-pages-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## GetPagesPath

<ul>
<li v-for="path in pagesPath">
{{path}}
</li>
</ul>

<script setup>
import { getPagesPath } from 'vuepress/client'

const pagesPath = getPagesPath();
</script>
18 changes: 18 additions & 0 deletions e2e/docs/routes/is-page-exist.md
Original file line number Diff line number Diff line change
@@ -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')}}

<script setup>
import { isPageExist } from 'vuepress/client'
</script>
9 changes: 9 additions & 0 deletions e2e/docs/routes/resolve.md
Original file line number Diff line number Diff line change
@@ -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'))}}

<script setup>
import { resolve } from 'vuepress/client'
</script>
45 changes: 45 additions & 0 deletions e2e/tests/routes/get-pages-path.cy.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
22 changes: 22 additions & 0 deletions e2e/tests/routes/is-page-exisit.cy.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
10 changes: 10 additions & 0 deletions e2e/tests/routes/resolve.cy.ts
Original file line number Diff line number Diff line change
@@ -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,
)
})
})
10 changes: 3 additions & 7 deletions packages/client/src/components/VPLink.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,7 +22,7 @@ const guardEvent = (event: MouseEvent): boolean | void => {
return true
}

interface VPLinkProps {
export interface VPLinkProps {
to: string
}

Expand All @@ -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',
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './defineClientConfig.js'
export * from './router.js'
export * from './withBase.js'
38 changes: 38 additions & 0 deletions packages/client/src/helpers/router.ts
Original file line number Diff line number Diff line change
@@ -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 = <PageMeta>(
path: string,
): { path: string; meta: PageMeta | null } => {
path = resolvers.resolvePagePath(pagesMap.value, redirectsMap.value, path)

return {
path,
meta: (pagesMap.value as PagesMap<PageMeta>).get(path)?.meta ?? null,
}
}

0 comments on commit 880ccba

Please sign in to comment.