Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/utils/docs-redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ export function resolveDocsPathRedirect({
return { type: 'not-found' }
}

// Manifest paths already have one trailing /index removed. Preserve the
// requested path for loading so nested index/index files stay distinct.
const canonicalRequestedPath = canonicalizeDocsPath(requestedPath)
const knownPaths = new Set(manifest.paths.map(normalizeManifestPath))

if (knownPaths.has(requestedPath)) {
if (knownPaths.has(canonicalRequestedPath)) {
return { type: 'render', docsPath: requestedPath }
}

const redirectFromTarget = getRedirectTarget({
knownPaths,
manifest,
requestedPath,
requestedPath: canonicalRequestedPath,
})

if (redirectFromTarget !== null) {
Expand All @@ -58,7 +61,7 @@ export function resolveDocsPathRedirect({
defaultDocs,
frameworks,
knownPaths,
requestedPath,
requestedPath: canonicalRequestedPath,
})

if (frameworkRedirectTarget !== null) {
Expand All @@ -67,7 +70,7 @@ export function resolveDocsPathRedirect({

const sectionIndexRedirectTarget = getSectionIndexRedirectTarget({
knownPaths,
requestedPath,
requestedPath: canonicalRequestedPath,
})

if (sectionIndexRedirectTarget !== null) {
Expand All @@ -87,8 +90,10 @@ export function docsManifestHasPath(
return false
}

const canonicalPath = canonicalizeDocsPath(normalizedPath)

return manifest.paths.some(
(path) => normalizeManifestPath(path) === normalizedPath,
(path) => normalizeManifestPath(path) === canonicalPath,
)
}

Expand Down Expand Up @@ -220,10 +225,17 @@ function getSectionIndexRedirectTarget(opts: {
}

function normalizeManifestPath(path: string) {
return removeLeadingSlash(path.trim())
.replace(/\.md$/, '')
.replace(/\/index$/, '')
.replace(/\/+$/g, '')
const normalizedPath = removeLeadingSlash(path.trim()).replace(/\/+$/g, '')

if (normalizedPath.endsWith('.md')) {
return normalizedPath.replace(/\.md$/, '').replace(/\/index$/, '')
}

return normalizedPath
}

function canonicalizeDocsPath(path: string) {
return path.replace(/\/index$/, '')
}

function normalizeDocsPath(path: string | null | undefined) {
Expand Down
69 changes: 69 additions & 0 deletions tests/docs-redirects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ function assertRedirectsTo(opts: {
)
}

function assertRenders(opts: {
defaultDocs: string
docsPath: string
frameworks: Array<string>
manifest: DocsRedirectManifest
}) {
assert.deepEqual(
resolveDocsPathRedirect({
defaultDocs: opts.defaultDocs,
docsPath: opts.docsPath,
frameworks: opts.frameworks,
manifest: opts.manifest,
}),
{ type: 'render', docsPath: opts.docsPath },
)
}

function assertNotFound(opts: {
defaultDocs: string
docsPath: string
Expand Down Expand Up @@ -160,6 +177,50 @@ assertNotFound({
manifest: manifestWithPaths(['overview', 'framework/react/overview']),
})

const typedocManifest = manifestWithPaths([
'overview',
'reference',
'reference/index',
'reference/index/type-aliases/DebugOptions',
'framework/react/reference',
'framework/react/reference/index',
])

assertRenders({
defaultDocs: 'overview',
docsPath: 'reference/index',
frameworks: ['react'],
manifest: typedocManifest,
})

assertRenders({
defaultDocs: 'overview',
docsPath: 'reference/index/index',
frameworks: ['react'],
manifest: typedocManifest,
})

assertRenders({
defaultDocs: 'overview',
docsPath: 'reference/index/type-aliases/DebugOptions',
frameworks: ['react'],
manifest: typedocManifest,
})

assertRenders({
defaultDocs: 'overview',
docsPath: 'framework/react/reference/index',
frameworks: ['react'],
manifest: typedocManifest,
})

assertRenders({
defaultDocs: 'overview',
docsPath: 'framework/react/reference/index/index',
frameworks: ['react'],
manifest: typedocManifest,
})

assertRedirectsTo({
defaultDocs: 'overview',
docsPath: 'react/overview',
Expand Down Expand Up @@ -225,6 +286,14 @@ assert.equal(
true,
)

assert.equal(
docsManifestHasPath(
manifestWithPaths(['reference/index']),
'reference/index/index',
),
true,
)

assert.equal(
docsManifestHasPath(
manifestWithPaths(['guides/queries.md']),
Expand Down
11 changes: 11 additions & 0 deletions tests/docs-route-smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ test(
})
}

if (library.id === 'table') {
paths.push({
path: '/table/latest/docs/reference/index/index',
expected: 'ok',
})
paths.push({
path: '/table/latest/docs/framework/react/reference/index/index',
expected: 'ok',
})
}

return paths
})

Expand Down
Loading