Skip to content
Merged
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
import taskLists from 'markdown-it-task-lists'
import { statSync } from 'node:fs'
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vitepress'

const repoRoot = fileURLToPath(new URL('../../', import.meta.url))
const sourceRepo = 'https://github.com/redai-studio/Relax'
const sourceRef = 'main'

function toSourceUrl(href: string, documentPath?: string): string {
if (!documentPath) return href
if (!href.startsWith('../') && !href.startsWith('./')) return href

const [, pathname, suffix] = href.match(/^([^?#]*)(.*)$/)!
let targetPath: string
try {
targetPath = resolve(dirname(documentPath), decodeURIComponent(pathname))
} catch {
return href
}

const repoPath = relative(repoRoot, targetPath)
const pathParts = repoPath.split(sep)
if (isAbsolute(repoPath) || pathParts[0] === '..' || pathParts[0] === 'docs') return href

const targetStats = statSync(targetPath, { throwIfNoEntry: false })
if (!targetStats) return href

const kind = targetStats.isDirectory() ? 'tree' : 'blob'
const urlPath = pathParts.map(encodeURIComponent).join('/')
return `${sourceRepo}/${kind}/${sourceRef}/${urlPath}${suffix}`
}

// https://vitepress.dev/reference/site-config
export default defineConfig({
vite: {
Expand Down Expand Up @@ -215,6 +246,14 @@ export default defineConfig({
math: true,
config(md) {
md.use(taskLists)

const renderLink = md.renderer.rules.link_open!
md.renderer.rules.link_open = (tokens, index, options, env, self) => {
const token = tokens[index]
const href = token.attrGet('href')
if (href) token.attrSet('href', toSourceUrl(href, env.path))
return renderLink(tokens, index, options, env, self)
}
}
},

Expand Down