Skip to content

Commit db32421

Browse files
committed
Consider basePath when generating slide URLs
1 parent fa85375 commit db32421

2 files changed

Lines changed: 61 additions & 8 deletions

File tree

packages/client/logic/slides.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export { slides }
88

99
export function getSlide(no: number | string) {
1010
return slides.value.find(
11-
s => (s.no === +no || s.meta.slide?.frontmatter.routeAlias === no),
11+
s => s.no === +no || s.meta.slide?.frontmatter.routeAlias === no,
1212
)
1313
}
1414

@@ -20,7 +20,13 @@ export function getSlidePath(
2020
if (typeof route === 'number' || typeof route === 'string')
2121
route = getSlide(route)!
2222
const no = route.meta.slide?.frontmatter.routeAlias ?? route.no
23-
return exporting ? `/export/${no}` : presenter ? `/presenter/${no}` : `/${no}`
23+
const basePath = import.meta.env.BASE_URL.replace(/\/$/, '') ?? ''
24+
const path = exporting
25+
? `/export/${no}`
26+
: presenter
27+
? `/presenter/${no}`
28+
: `/${no}`
29+
return `${basePath}${path}`
2430
}
2531

2632
export function useIsSlideActive() {

test/utils.test.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import type { ResolvedFontOptions, SlideInfo } from '@slidev/types'
1+
import type { ResolvedFontOptions, SlideInfo, SlideRoute } from '@slidev/types'
22
import { relative, resolve } from 'node:path'
33
import { slash } from '@antfu/utils'
4+
import { getSlidePath } from '@slidev/client/logic/slides'
45
import MarkdownIt from 'markdown-it'
56
import { describe, expect, it } from 'vitest'
67
import YAML from 'yaml'
78
import { parseAspectRatio, parseRangeString } from '../packages/parser/src'
89
import { getRoots } from '../packages/slidev/node/resolver'
9-
import { generateCoollabsFontsUrl, generateGoogleFontsUrl, stringifyMarkdownTokens, updateFrontmatterPatch } from '../packages/slidev/node/utils'
10+
import {
11+
generateCoollabsFontsUrl,
12+
generateGoogleFontsUrl,
13+
stringifyMarkdownTokens,
14+
updateFrontmatterPatch,
15+
} from '../packages/slidev/node/utils'
1016

1117
describe('utils', () => {
1218
it('page-range', () => {
@@ -33,12 +39,17 @@ describe('utils', () => {
3339

3440
it('stringify-markdown-tokens', () => {
3541
const md = MarkdownIt({ html: true })
36-
const stringify = (src: string) => stringifyMarkdownTokens(md.parseInline(src, {}))
42+
const stringify = (src: string) =>
43+
stringifyMarkdownTokens(md.parseInline(src, {}))
3744

38-
expect(stringify('<span style="color:red">Composable</span> Vue')).toBe('Composable Vue')
45+
expect(stringify('<span style="color:red">Composable</span> Vue')).toBe(
46+
'Composable Vue',
47+
)
3948
expect(stringify('<b>Whatever</b>')).toBe('Whatever')
4049
expect(stringify('Talk about `<details/>`')).toBe('Talk about <details/>')
41-
expect(stringify('What is Readonly\\<T\\> in TypeScript')).toBe('What is Readonly<T> in TypeScript')
50+
expect(stringify('What is Readonly\\<T\\> in TypeScript')).toBe(
51+
'What is Readonly<T> in TypeScript',
52+
)
4253
expect(stringify('Welcome to<br />*Slidev*')).toBe('Welcome to Slidev')
4354
})
4455

@@ -81,7 +92,9 @@ describe('utils', () => {
8192
})
8293

8394
it('roots', async () => {
84-
const { cliRoot, clientRoot, userRoot, userWorkspaceRoot } = await getRoots(resolve('slides.md'))
95+
const { cliRoot, clientRoot, userRoot, userWorkspaceRoot } = await getRoots(
96+
resolve('slides.md'),
97+
)
8598
const expectRelative = (v: string) => expect(slash(relative(__dirname, v)))
8699
expectRelative(cliRoot).toMatchInlineSnapshot(`"../packages/slidev"`)
87100
expectRelative(clientRoot).toMatchInlineSnapshot(`"../packages/client"`)
@@ -145,4 +158,38 @@ describe('utils', () => {
145158
"
146159
`)
147160
})
161+
162+
it('getSlidePath with base path', () => {
163+
// Mock import.meta.env.BASE_URL
164+
const originalBaseUrl = import.meta.env.BASE_URL
165+
166+
// Test with base path
167+
import.meta.env.BASE_URL = '/my_monorepo/my_prez/'
168+
169+
const mockRoute: SlideRoute = {
170+
no: 2,
171+
meta: {
172+
slide: {
173+
frontmatter: {},
174+
},
175+
},
176+
}
177+
178+
expect(getSlidePath(mockRoute, false, false)).toBe(
179+
'/my_monorepo/my_prez/2',
180+
)
181+
expect(getSlidePath(mockRoute, true, false)).toBe(
182+
'/my_monorepo/my_prez/presenter/2',
183+
)
184+
expect(getSlidePath(mockRoute, false, true)).toBe(
185+
'/my_monorepo/my_prez/export/2',
186+
)
187+
188+
// Test with root path
189+
import.meta.env.BASE_URL = '/'
190+
expect(getSlidePath(mockRoute, false, false)).toBe('/2')
191+
192+
// Restore original
193+
import.meta.env.BASE_URL = originalBaseUrl
194+
})
148195
})

0 commit comments

Comments
 (0)