本文档介绍 Content Hub 的仓库引用注入器功能,用于在发布到不同平台时自动添加项目仓库的引用链接。
仓库引用注入器会自动在文章内容末尾添加:
如果你在维护一个开源项目,博客文章发布到第三方平台时:
将技术文章同步发布到多个平台:
Content Hub 作为内容管理的中心:
pnpm transform:html content/posts/test.md \
--repo-owner "Charliechen114514" \
--repo-name "post_waver" \
--repo-desc "Content Hub" \
--repo-branch "main"
参数说明:
| 参数 | 必填 | 默认值 | 说明 |
|---|---|---|---|
--repo-owner |
✅ | - | GitHub 用户名或组织名 |
--repo-name |
✅ | - | 仓库名称 |
--repo-desc |
❌ | 仓库名 | 仓库的描述文本 |
--repo-branch |
❌ | main | Git 分支名 |
import { injectRepoReference } from '@content-hub/core'
const content = injectRepoReference(
htmlContent,
post,
{
owner: 'Charliechen114514',
repo: 'post_waver',
branch: 'main',
description: 'Content Hub - 本地优先的内容管理系统'
},
'html'
)
在项目根目录创建 .env 文件:
REPO_OWNER=Charliechen114514
REPO_NAME=post_waver
REPO_DESC=Content Hub
REPO_BRANCH=main
然后在脚本中读取:
const repoConfig = {
owner: process.env.REPO_OWNER!,
repo: process.env.REPO_NAME!,
description: process.env.REPO_DESC,
branch: process.env.REPO_BRANCH || 'main'
}
---
*本文首发于 [Content Hub](https://github.com/Charliechen114514/post_waver),查看源码和更多文章请访问仓库。*
*文章链接:https://github.com/Charliechen114514/post_waver/blob/main/content/posts/test.md*
渲染效果:
本文首发于 Content Hub,查看源码和更多文章请访问仓库。 文章链接:https://github.com/Charliechen114514/post_waver/blob/main/content/posts/test.md
---
<p style="font-size: 14px; color: #666; margin-top: 40px;">
本文首发于 <a href="https://github.com/Charliechen114514/post_waver">Content Hub</a>,查看源码和更多文章请访问仓库。<br/>
文章链接:<a href="https://github.com/Charliechen114514/post_waver/blob/main/content/posts/test.md">https://github.com/Charliechen114514/post_waver/blob/main/content/posts/test.md</a>
</p>
渲染效果:
本文首发于 Content Hub,查看源码和更多文章请访问仓库。 文章链接:https://github.com/Charliechen114514/post_waver/blob/main/content/posts/test.md
---
<p style="margin-top: 40px; padding-top: 20px; border-top: 1px solid #ddd;">
<strong>来源:</strong><a href="https://github.com/Charliechen114514/post_waver">Content Hub</a><br/>
<strong>原文链接:</strong><a href="https://github.com/Charliechen114514/post_waver/blob/main/content/posts/test.md">https://github.com/Charliechen114514/post_waver/blob/main/content/posts/test.md</a></p>
渲染效果:
来源:Content Hub 原文链接:https://github.com/Charliechen114514/post_waver/blob/main/content/posts/test.md
injectRepoReference(content, post, repoConfig, platform): string为内容注入仓库引用链接。
类型签名:
function injectRepoReference(
content: string,
post: Post,
repoConfig: RepoConfig,
platform: string
): string
参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
content |
string |
✅ | 转换后的文章内容 |
post |
Post |
✅ | 文章对象(包含 filepath 等信息) |
repoConfig |
RepoConfig |
✅ | 仓库配置对象 |
platform |
string |
✅ | 目标平台(’juejin’ | ‘wechat’ | ‘html’) |
RepoConfig 类型:
interface RepoConfig {
/** 仓库拥有者 */
owner: string
/** 仓库名称 */
repo: string
/** 分支(默认 main) */
branch?: string
/** 仓库描述(可选) */
description?: string
}
返回值:
string:注入了仓库引用的内容示例:
import { injectRepoReference } from '@content-hub/core'
import type { Post } from '@content-hub/core'
const post: Post = {
id: 'test-post',
filepath: 'content/posts/test.md',
frontmatter: {
title: 'Test Post',
date: '2026-04-02',
tags: ['test']
},
content: '# Test\n\nContent here...',
ast: null,
contentHash: 'abc123',
scannedAt: new Date()
}
const result = injectRepoReference(
'<h1>Test</h1><p>Content here...</p>',
post,
{
owner: 'Charliechen114514',
repo: 'post_waver',
branch: 'main',
description: 'Content Hub'
},
'html'
)
console.log(result)
// 输出带有仓库引用的 HTML
如果需要自定义引用格式,可以直接修改 packages/core/src/repo-injector.ts 中的 generateRepoFooter 函数:
function generateRepoFooter(
repoUrl: string,
articleUrl: string,
config: RepoConfig,
platform: string
): string {
// 添加你的自定义平台
switch (platform) {
case 'custom-platform':
return `
---
<div class="custom-footer">
来自 ${config.repo} 的文章
<a href="${repoUrl}">查看仓库</a>
</div>
`.trim()
// ... 其他平台
}
}
如果需要为不同文章配置不同的仓库,可以在文章的 Frontmatter 中添加配置:
---
title: My Post
date: 2026-04-02
tags:
- test
repo:
owner: another-user
repo: another-repo
description: Another Project
---
然后在转换时读取这个配置。
建议在项目根目录的 .env 文件中统一配置仓库信息:
REPO_OWNER=your-username
REPO_NAME=your-repo
REPO_DESC=Your Project Description
REPO_BRANCH=main
main 或 master 分支dev 或 develop 分支确保生成的链接有效:
除了仓库引用,还可以在文章开头添加版权声明:
---
title: My Post
date: 2026-04-02
tags:
- test
copyright: >
本文首发于 [Content Hub](https://github.com/user/repo),
转载请注明出处。
---
原因:
解决方案:
post.filepath 是否正确repoConfig.branch 与实际分支名一致原因:
解决方案:
原因:GitHub 链接通常较长
解决方案:
文档版本:v1.0 最后更新:2026-04-02 维护者:Content Hub Team