-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathgithub.ts
More file actions
58 lines (51 loc) · 2.01 KB
/
Copy pathgithub.ts
File metadata and controls
58 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// GitHub 相关工具方法:集中维护仓库常量与文档路径生成逻辑
const GITHUB_OWNER = "InvolutionHell";
const GITHUB_REPO = "involutionhell";
const DEFAULT_BRANCH = "main";
// 历史路径是 "app/docs";i18n URL 段化改造(2026-05)后 mdx 内容
// 从 app/docs 迁到 content/docs(与路由文件分离,按 fumadocs 推荐)。
// GitHub Edit URL 必须与实际仓库结构一致。
const DOCS_BASE = "content/docs";
const REPO_BASE_URL = `https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}`;
// 拼接路径并清理多余斜杠,避免出现 // 或首尾斜杠
function joinPath(...segments: (string | undefined)[]) {
return segments
.filter((segment) => (segment ?? "").trim().length > 0)
.join("/")
.replace(/\/+/g, "/")
.replace(/^\/+/, "")
.replace(/\/+$/, "");
}
// 将路径逐段 URL 编码,处理中文等特殊字符
function encodeRepoPath(...segments: (string | undefined)[]) {
const joined = joinPath(...segments);
if (!joined) return "";
return joined
.split("/")
.map((segment) => encodeURIComponent(segment))
.join("/");
}
// 构建文档的 GitHub 编辑链接
export function buildDocsEditUrl(relativeDocPath: string) {
const encoded = encodeRepoPath(DOCS_BASE, relativeDocPath);
return `${REPO_BASE_URL}/edit/${DEFAULT_BRANCH}/${encoded}`;
}
// 构建在 GitHub 新建文档的链接,附带 frontmatter 参数
export function buildDocsNewUrl(relativeDir: string, params: URLSearchParams) {
const encodedDir = encodeRepoPath(DOCS_BASE, relativeDir);
const query = params.toString();
const suffix = query ? `?${query}` : "";
return `${REPO_BASE_URL}/new/${DEFAULT_BRANCH}/${encodedDir}${suffix}`;
}
// 帮助预览完整 docs 路径(未编码)
export function normalizeDocsPath(relative: string) {
return joinPath(DOCS_BASE, relative);
}
// 暴露常量给其他场景复用
export const githubConstants = {
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
defaultBranch: DEFAULT_BRANCH,
docsBase: DOCS_BASE,
repoBaseUrl: REPO_BASE_URL,
};