ReviewOS

also looking at this

stacks/bunpress

fix: name the document a refused Markdown link was authored in

#94
Closed glennmichael123 wants to merge fix/name-the-document-a-refused-link-is-in into main
2 files +49 -2
packages/bunpress/src/markdown-pages.tsmodified+18-2
Changes to packages/bunpress/src/markdown-pages.ts
@@ -40,6 +40,22 @@ function routeFor(path: string): string {
4040 return `/${encodePath(route)}`
4141}
4242
43/**
44 * The document a link was authored in, for a message somebody has to act on.
45 *
46 * Both link failures below name the destination, which is not enough to find
47 * the link: the destination is what is missing, and the file holding it is
48 * what has to be edited. Relative to the documentation root, because an
49 * absolute temporary path is noise in a build log.
50 *
51 * Under an include, `resolveLink` is handed the innermost origin rather than
52 * the host page, so this names the file the link was actually written in.
53 */
54function authoredIn(page: MarkdownPage, root: string): string {
55 const within = relative(root, page.sourceFile)
56 return within && !within.startsWith('..') && !isAbsolute(within) ? within.split(sep).join('/') : page.sourceFile
57}
58
4359function routeKey(path: string): string {
4460 const encoded = path.split('/').map(segment => encodeURIComponent(decodeURIComponent(segment))).join('/')
4561 return encoded.replace(/\/+$/, '') || '/'
@@ -124,11 +140,11 @@ export function createMarkdownPageCatalog(markdownFiles: readonly string[], docs
124140 return destination
125141 const source = sources.get(target)
126142 if (!source)
127 throw new Error(`Markdown link has no documentation source: ${rawPath}`)
143 throw new Error(`${authoredIn(page, root)}: Markdown link has no documentation source: ${rawPath}`)
128144 const locale = source.explicitLocale ? source.locale : page.locale
129145 const selected = byPage.get(`${locale}\0${source.path}`)
130146 if (!selected)
131 throw new Error(`Markdown link has no rendered page: ${rawPath}`)
147 throw new Error(`${authoredIn(page, root)}: Markdown link has no rendered page: ${rawPath}`)
132148 return `${localizeUrl(language, locale, routeFor(selected.relativePath))}${suffix}`
133149 },
134150 })