Skip to content

Commit d2cb912

Browse files
authored
Merge pull request #2765 from DaoDaoNoCode/vitepress-followup-fixes
fix(site): rewrite broken README.md and out-of-docs links
2 parents 26045b5 + 9aca1f9 commit d2cb912

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

website/.vitepress/config.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function escapeVueSyntax(src: string): string {
5959
}).join('\n')
6060
}
6161

62-
const KNOWN_TAGS = /^<\/?(?:a|abbr|address|area|article|aside|audio|b|base|bdi|bdo|blockquote|body|br|button|canvas|caption|cite|code|col|colgroup|data|datalist|dd|del|details|dfn|dialog|div|dl|dt|em|embed|fieldset|figcaption|figure|footer|form|h[1-6]|head|header|hgroup|hr|html|i|iframe|img|input|ins|kbd|label|legend|li|link|main|map|mark|menu|meta|meter|nav|noscript|object|ol|optgroup|option|output|p|param|picture|pre|progress|q|rp|rt|ruby|s|samp|script|search|section|select|slot|small|source|span|strong|style|sub|summary|sup|table|tbody|td|template|textarea|tfoot|th|thead|time|title|tr|track|u|ul|var|video|wbr|svg|path|g|circle|rect|line|polyline|polygon|text|defs|use|symbol)[\s>\/!]/i
62+
const KNOWN_TAGS = /^<\/?(?:a|abbr|address|area|article|aside|audio|b|base|bdi|bdo|blockquote|body|br|button|canvas|caption|cite|code|col|colgroup|data|datalist|dd|del|details|dfn|dialog|div|dl|dt|em|embed|fieldset|figcaption|figure|footer|form|h[1-6]|head|header|hgroup|hr|html|i|iframe|img|input|ins|kbd|label|legend|li|link|main|map|mark|menu|meta|meter|nav|noscript|object|ol|optgroup|option|output|p|param|picture|pre|progress|q|rp|rt|ruby|s|samp|script|search|section|select|slot|small|source|span|strong|style|sub|summary|sup|table|tbody|td|template|textarea|tfoot|th|thead|time|title|tr|track|u|ul|var|video|wbr|svg|path|g|circle|rect|line|polyline|polygon|text|defs|use|symbol)[\s>/!]/i
6363

6464
function escapeLine(line: string): string {
6565
let result = ''
@@ -316,7 +316,7 @@ export default defineConfig({
316316
},
317317
preConfig: (md) => {
318318
const defaultParse = md.parse.bind(md)
319-
md.parse = (src: string, env: any) => {
319+
md.parse = (src: string, env: Record<string, unknown>) => {
320320
return defaultParse(escapeVueSyntax(src), env)
321321
}
322322
},
@@ -330,6 +330,41 @@ export default defineConfig({
330330
return defaultCodeInline(tokens, idx, options, env, self)
331331
}
332332

333+
// Rewrite relative links that escape the docs/ directory to GitHub
334+
// source URLs, and rewrite README.md links to directory index paths
335+
// (only for links that stay within docs/).
336+
md.core.ruler.push('rewrite-links', (state) => {
337+
for (const token of state.tokens) {
338+
if (!token.children) continue
339+
for (const child of token.children) {
340+
if (child.type !== 'link_open') continue
341+
const href = child.attrGet('href')
342+
if (!href || href.startsWith('http') || href.startsWith('#') || href.startsWith('mailto:')) continue
343+
344+
// Check if the link escapes docs/ (more ../ than directory depth)
345+
const docPath = state.env?.relativePath || ''
346+
const docDir = docPath.split('/').slice(0, -1)
347+
const parts = href.split('#')
348+
const linkPath = parts[0]
349+
const anchor = parts[1] ? '#' + parts[1] : ''
350+
const segments = linkPath.split('/')
351+
let depth = 0
352+
for (const s of segments) { if (s === '..') depth++; else break }
353+
if (depth > docDir.length) {
354+
const remainder = segments.slice(depth).join('/')
355+
const prefix = /\.[a-zA-Z0-9]+$/.test(remainder) && !remainder.endsWith('/') ? 'blob' : 'tree'
356+
child.attrSet('href', `https://github.com/fullsend-ai/fullsend/${prefix}/main/${remainder}${anchor}`)
357+
continue
358+
}
359+
360+
// For links staying within docs/, rewrite README.md to directory index
361+
if (/README\.md(#.*)?$/.test(href)) {
362+
child.attrSet('href', href.replace(/README\.md(#.*)?$/, (_: string, a: string) => a || './'))
363+
}
364+
}
365+
}
366+
})
367+
333368
const defaultFence = md.renderer.rules.fence!.bind(md.renderer.rules)
334369
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
335370
if (tokens[idx].info.trim() === 'mermaid') {

0 commit comments

Comments
 (0)