Repository navigation
fix(portal): keep the encoding a page slug carries #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1fe769f
7ef4844
b5c0694
299b824
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import fs from 'fs'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
| import { expect } from 'chai'; | ||
| import { loader } from 'fumadocs-core/source'; | ||
| import { openApiSection } from '../../portal-template/src/lib/openapi-section.server'; | ||
| import { decodeMarkdownUrl, slugsFromSplat } from '../../portal-template/src/lib/shared'; | ||
|
|
||
| const ok = { 200: { description: 'ok' } }; | ||
|
|
||
| describe('slugsFromSplat', () => { | ||
| it('drops the empty segments a leading or trailing slash leaves behind', () => { | ||
| expect(slugsFromSplat('guides/getting-started/')).to.deep.equal(['guides', 'getting-started']); | ||
| expect(slugsFromSplat('')).to.deep.equal([]); | ||
| expect(slugsFromSplat(undefined)).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('leaves an ordinary slug exactly as it was', () => { | ||
| expect(slugsFromSplat('api/openapi/pets/listPets')).to.deep.equal(['api', 'openapi', 'pets', 'listPets']); | ||
| }); | ||
|
|
||
| it('encodes each segment the way the page slugs were encoded, and no further', () => { | ||
| expect(slugsFromSplat('api/List All Pets')).to.deep.equal(['api', 'List%20All%20Pets']); | ||
| expect(slugsFromSplat('café')).to.deep.equal(['caf%C3%A9']); | ||
| expect(slugsFromSplat('pets&cats')).to.deep.equal(['pets&cats']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('a page whose operationId needs encoding', () => { | ||
| let directory: string; | ||
| let source: Awaited<ReturnType<typeof sectionSource>>; | ||
| let url: string; | ||
|
|
||
| const sectionSource = async () => | ||
| loader({ openapi: await openApiSection('openapi', path.join(directory, 'api.json'), null) }, { baseUrl: '/' }); | ||
|
|
||
| const decodedSplatOf = (pageUrl: string) => decodeURIComponent(pageUrl).replace(/^\//, ''); | ||
|
|
||
| before(async () => { | ||
| directory = fs.mkdtempSync(path.join(os.tmpdir(), 'page-slugs-')); | ||
| fs.writeFileSync( | ||
| path.join(directory, 'api.json'), | ||
| JSON.stringify({ | ||
| openapi: '3.1.0', | ||
| info: { title: 'Pets', version: '1' }, | ||
| tags: [{ name: 'pets' }], | ||
| paths: { '/pets': { get: { operationId: 'List All Pets', tags: ['pets'], responses: ok } } } | ||
| }) | ||
| ); | ||
|
|
||
| source = await sectionSource(); | ||
| const page = source.getPages().find((candidate) => candidate.url.includes('%20')); | ||
| expect(page, 'no page slug needed encoding').to.not.equal(undefined); | ||
|
Check warning on line 53 in test/portal-template/page-slugs.test.ts
|
||
| url = page!.url; | ||
| }); | ||
|
|
||
| after(() => { | ||
| fs.rmSync(directory, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('is found from the splat the router decoded', () => { | ||
| expect(source.getPage(slugsFromSplat(decodedSplatOf(url)))?.url).to.equal(url); | ||
| }); | ||
|
|
||
| it('is found at its Markdown address too', () => { | ||
| expect(source.getPage(decodeMarkdownUrl(slugsFromSplat(decodedSplatOf(url))))?.url).to.equal(url); | ||
| }); | ||
|
|
||
| it('is missed when the splat is looked up as it arrives', () => { | ||
| expect(source.getPage(decodedSplatOf(url).split('/'))).to.equal(undefined); | ||
|
Check warning on line 70 in test/portal-template/page-slugs.test.ts
|
||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.