Skip to content

Commit 326fae9

Browse files
committed
fix(site): fix out-of-docs README and dotdir link rewriting
- Check out-of-docs links before README rewrite so ../../README.md gets GitHub-redirected with the filename intact - Remove [^.] guard so .github/ paths are also rewritten to GitHub - README→index rewrite only applies to links staying within docs/ Signed-off-by: Juntao Wang <juntwang@redhat.com>
1 parent e3f7d6c commit 326fae9

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

website/.vitepress/config.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,9 @@ export default defineConfig({
337337
return defaultCodeInline(tokens, idx, options, env, self)
338338
}
339339

340-
// Rewrite README.md links to directory index (README→index rewrite),
341-
// and convert relative links pointing outside docs/ to GitHub URLs.
340+
// Rewrite relative links that escape the docs/ directory to GitHub
341+
// source URLs, and rewrite README.md links to directory index paths
342+
// (only for links that stay within docs/).
342343
md.core.ruler.push('rewrite-links', (state) => {
343344
for (const token of state.tokens) {
344345
if (!token.children) continue
@@ -347,23 +348,24 @@ export default defineConfig({
347348
const href = child.attrGet('href')
348349
if (!href || href.startsWith('http') || href.startsWith('#') || href.startsWith('mailto:')) continue
349350

350-
if (/README\.md(#.*)?$/.test(href)) {
351-
child.attrSet('href', href.replace(/README\.md(#.*)?$/, (_: string, anchor: string) => anchor || './'))
351+
// Check if the link escapes docs/ (more ../ than directory depth)
352+
const docPath = state.env?.relativePath || ''
353+
const docDir = docPath.split('/').slice(0, -1)
354+
const parts = href.split('#')
355+
const linkPath = parts[0]
356+
const anchor = parts[1] ? '#' + parts[1] : ''
357+
const segments = linkPath.split('/')
358+
let depth = 0
359+
for (const s of segments) { if (s === '..') depth++; else break }
360+
if (depth > docDir.length) {
361+
const remainder = segments.slice(depth).join('/')
362+
child.attrSet('href', 'https://github.com/fullsend-ai/fullsend/tree/main/' + remainder + anchor)
363+
continue
352364
}
353365

354-
if (/\.\.\/(\.\.\/)*[^.][^/]*/.test(href)) {
355-
const docPath = state.env?.relativePath || ''
356-
const docDir = docPath.split('/').slice(0, -1)
357-
const parts = href.split('#')
358-
const linkPath = parts[0]
359-
const anchor = parts[1] ? '#' + parts[1] : ''
360-
const segments = linkPath.split('/')
361-
let depth = 0
362-
for (const s of segments) { if (s === '..') depth++; else break }
363-
if (depth > docDir.length) {
364-
const remainder = segments.slice(depth).join('/')
365-
child.attrSet('href', 'https://github.com/fullsend-ai/fullsend/tree/main/' + remainder + anchor)
366-
}
366+
// For links staying within docs/, rewrite README.md to directory index
367+
if (/README\.md(#.*)?$/.test(href)) {
368+
child.attrSet('href', href.replace(/README\.md(#.*)?$/, (_: string, a: string) => a || './'))
367369
}
368370
}
369371
}

0 commit comments

Comments
 (0)