multi-publisher 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +370 -0
  3. package/dist/adapters/baijiahao.d.ts +14 -0
  4. package/dist/adapters/baijiahao.js +77 -0
  5. package/dist/adapters/base-adapter.d.ts +41 -0
  6. package/dist/adapters/base-adapter.js +84 -0
  7. package/dist/adapters/bilibili.d.ts +14 -0
  8. package/dist/adapters/bilibili.js +86 -0
  9. package/dist/adapters/cnblogs.d.ts +14 -0
  10. package/dist/adapters/cnblogs.js +90 -0
  11. package/dist/adapters/csdn.d.ts +29 -0
  12. package/dist/adapters/csdn.js +174 -0
  13. package/dist/adapters/cto51.d.ts +14 -0
  14. package/dist/adapters/cto51.js +72 -0
  15. package/dist/adapters/douban.d.ts +14 -0
  16. package/dist/adapters/douban.js +49 -0
  17. package/dist/adapters/eastmoney.d.ts +14 -0
  18. package/dist/adapters/eastmoney.js +47 -0
  19. package/dist/adapters/imooc.d.ts +14 -0
  20. package/dist/adapters/imooc.js +78 -0
  21. package/dist/adapters/index.d.ts +26 -0
  22. package/dist/adapters/index.js +22 -0
  23. package/dist/adapters/interface.d.ts +44 -0
  24. package/dist/adapters/interface.js +1 -0
  25. package/dist/adapters/juejin.d.ts +23 -0
  26. package/dist/adapters/juejin.js +166 -0
  27. package/dist/adapters/oschina.d.ts +14 -0
  28. package/dist/adapters/oschina.js +77 -0
  29. package/dist/adapters/registry.d.ts +61 -0
  30. package/dist/adapters/registry.js +146 -0
  31. package/dist/adapters/segmentfault.d.ts +14 -0
  32. package/dist/adapters/segmentfault.js +73 -0
  33. package/dist/adapters/sohu.d.ts +14 -0
  34. package/dist/adapters/sohu.js +72 -0
  35. package/dist/adapters/toutiao.d.ts +9 -0
  36. package/dist/adapters/toutiao.js +199 -0
  37. package/dist/adapters/wechat-publisher.d.ts +55 -0
  38. package/dist/adapters/wechat-publisher.js +210 -0
  39. package/dist/adapters/weibo.d.ts +14 -0
  40. package/dist/adapters/weibo.js +99 -0
  41. package/dist/adapters/weixin.d.ts +14 -0
  42. package/dist/adapters/weixin.js +63 -0
  43. package/dist/adapters/woshipm.d.ts +14 -0
  44. package/dist/adapters/woshipm.js +72 -0
  45. package/dist/adapters/xiaohongshu.d.ts +9 -0
  46. package/dist/adapters/xiaohongshu.js +199 -0
  47. package/dist/adapters/xueqiu.d.ts +14 -0
  48. package/dist/adapters/xueqiu.js +74 -0
  49. package/dist/adapters/yuque.d.ts +13 -0
  50. package/dist/adapters/yuque.js +25 -0
  51. package/dist/adapters/zhihu.d.ts +23 -0
  52. package/dist/adapters/zhihu.js +142 -0
  53. package/dist/cli/cookie.d.ts +5 -0
  54. package/dist/cli/cookie.js +80 -0
  55. package/dist/cli/credential.d.ts +6 -0
  56. package/dist/cli/credential.js +32 -0
  57. package/dist/cli/index.d.ts +5 -0
  58. package/dist/cli/index.js +72 -0
  59. package/dist/cli/login.d.ts +3 -0
  60. package/dist/cli/login.js +101 -0
  61. package/dist/cli/platforms.d.ts +1 -0
  62. package/dist/cli/platforms.js +35 -0
  63. package/dist/cli/publish-all.d.ts +6 -0
  64. package/dist/cli/publish-all.js +101 -0
  65. package/dist/cli/publish.d.ts +7 -0
  66. package/dist/cli/publish.js +78 -0
  67. package/dist/cli/render.d.ts +9 -0
  68. package/dist/cli/render.js +40 -0
  69. package/dist/commands/login.d.ts +43 -0
  70. package/dist/commands/login.js +248 -0
  71. package/dist/commands/publish.d.ts +19 -0
  72. package/dist/commands/publish.js +275 -0
  73. package/dist/config.d.ts +177 -0
  74. package/dist/config.js +333 -0
  75. package/dist/core/mathjax.d.ts +4 -0
  76. package/dist/core/mathjax.js +33 -0
  77. package/dist/core/parser.d.ts +17 -0
  78. package/dist/core/parser.js +42 -0
  79. package/dist/core/renderer.d.ts +18 -0
  80. package/dist/core/renderer.js +30 -0
  81. package/dist/core/styler.d.ts +8 -0
  82. package/dist/core/styler.js +11 -0
  83. package/dist/core/theme.d.ts +9 -0
  84. package/dist/core/theme.js +420 -0
  85. package/dist/index.d.ts +8 -0
  86. package/dist/index.js +7 -0
  87. package/dist/runtime/browser-runtime.d.ts +79 -0
  88. package/dist/runtime/browser-runtime.js +501 -0
  89. package/dist/runtime/index.d.ts +12 -0
  90. package/dist/runtime/index.js +1 -0
  91. package/dist/runtime/node-runtime.d.ts +5 -0
  92. package/dist/runtime/node-runtime.js +25 -0
  93. package/dist/tools/xiaohongshu-sniffer.d.ts +1 -0
  94. package/dist/tools/xiaohongshu-sniffer.js +54 -0
  95. package/package.json +62 -0
  96. package/themes/all-themes-preview.html +522 -0
  97. package/themes/previews/cyberpunk.png +0 -0
  98. package/themes/previews/minimal.png +0 -0
  99. package/themes/previews/nord.png +0 -0
  100. package/themes/previews/wechat.png +0 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,370 @@
1
+ # multi-publisher
2
+
3
+ **一行命令,Markdown 发布到全网。**
4
+
5
+ 将排版好的 Markdown 文章一键发布到微信公众号、知乎、掘金、CSDN 等 20+ 平台,无需手动复制粘贴。支持浏览器自动登录获取 Cookie,12 套渲染主题,是内容创作者的效率神器。
6
+
7
+ ---
8
+
9
+ ## 功能特性
10
+
11
+ | 特性 | 说明 |
12
+ |------|------|
13
+ | **多平台支持** | 微信公众号、知乎、掘金、CSDN、微博、小红书、B站等 20+ 平台 |
14
+ | **浏览器自动登录** | Playwright 驱动扫码/账号登录,Cookie 自动获取保存 |
15
+ | **14 套渲染主题** | default、wechat、modern、minimal、cyberpunk、nord、paper、darkelite、sunset、zen、retro、midnight、brutalism、neumorphism |
16
+ | **Markdown 直发** | front-matter 元数据、代码高亮、LaTeX 公式 |
17
+ | **CI/CD 友好** | 纯命令行无需浏览器,配置文件统一管理 |
18
+ | **草稿箱发布** | 微信公众号 → 草稿箱,其他平台 → 各自草稿箱 |
19
+
20
+ ---
21
+
22
+ ## 安装
23
+
24
+ ```bash
25
+ # 源码安装
26
+ git clone <repo-url>
27
+ cd multi-publisher
28
+ npm install
29
+ npm run build
30
+
31
+ # 链接为全局命令
32
+ npm link
33
+ mpub --help
34
+ ```
35
+
36
+ **Node.js ≥ 18** required.
37
+
38
+ ---
39
+
40
+ ## 快速开始
41
+
42
+ ### 1. 配置平台登录
43
+
44
+ **方式一:浏览器自动登录(推荐)**
45
+
46
+ ```bash
47
+ # 登录知乎
48
+ mpub login -p zhihu
49
+
50
+ # 登录掘金
51
+ mpub login -p juejin
52
+
53
+ # 登录小红书
54
+ mpub login -p xiaohongshu
55
+
56
+ # 查看所有支持平台
57
+ mpub login --help
58
+ ```
59
+
60
+ **方式二:手动 Cookie 配置**
61
+
62
+ ```bash
63
+ # 交互式输入 Cookie
64
+ mpub cookie --platform zhihu --set
65
+
66
+ # 检查 Cookie 状态
67
+ mpub cookie --platform zhihu --check
68
+ ```
69
+
70
+ ### 2. 发布文章
71
+
72
+ ```bash
73
+ # 发布到微信公众号(默认)
74
+ mpub publish -f article.md
75
+
76
+ # 发布到知乎草稿
77
+ mpub publish -f article.md -p zhihu
78
+
79
+ # 一键发布到所有已登录平台
80
+ mpub publish-all -f article.md
81
+
82
+ # 渲染预览(不发布)
83
+ mpub render -f article.md
84
+
85
+ # 指定主题渲染
86
+ mpub render -f article.md -t cyberpunk
87
+ ```
88
+
89
+ ---
90
+
91
+ ## 支持的平台
92
+
93
+ ### 浏览器自动登录状态
94
+
95
+ | 平台 | 登录命令 | Cookie 获取 | 草稿发布 | 备注 |
96
+ |------|----------|-------------|----------|------|
97
+ | 微信公众号 | `mpub credential --set` | ✅ | ✅ | AppID + AppSecret |
98
+ | 知乎 | `mpub login -p zhihu` | ✅ | ✅ | 直接发布草稿 |
99
+ | 掘金 | `mpub login -p juejin` | ✅ | ✅ | 直接发布草稿 |
100
+ | CSDN | `mpub login -p csdn` | ✅ | ✅ | 直接发布草稿 |
101
+ | 小红书 | `mpub login -p xiaohongshu` | ✅ | 🔄 测试中 | 需要更多测试 |
102
+ | 简书 | `mpub login -p jianshu` | ✅ | ❌ 待实现 | |
103
+ | 微博 | `mpub login -p weibo` | ✅ | ❌ 待实现 | |
104
+ | 头条号 | `mpub login -p toutiao` | ✅ | 🔄 测试中 | 有反爬机制 |
105
+ | 百家号 | `mpub login -p baijiahao` | ✅ | ❌ 待实现 | |
106
+ | B站 | `mpub login -p bilibili` | ✅ | 🔄 测试中 | 需要更多测试 |
107
+ | 思否 | `mpub login -p segmentfault` | ✅ | ❌ 待实现 | |
108
+ | 博客园 | `mpub login -p cnblogs` | ✅ | ❌ 待实现 | |
109
+ | 开源中国 | `mpub login -p oschina` | ✅ | ❌ 待实现 | |
110
+ | 慕课网 | `mpub login -p imooc` | ✅ | ❌ 待实现 | |
111
+ | 雪球 | `mpub login -p xueqiu` | ✅ | ❌ 待实现 | |
112
+ | 人人都是产品经理 | `mpub login -p woshipm` | ✅ | ❌ 待实现 | |
113
+ | 豆瓣 | `mpub login -p douban` | ✅ | ❌ 待实现 | |
114
+ | 搜狐号 | `mpub login -p sohu` | ✅ | ❌ 待实现 | |
115
+ | 东方财富 | `mpub login -p eastmoney` | ✅ | ❌ 待实现 | |
116
+ | 51CTO | `mpub login -p cto51` | ✅ | ❌ 待实现 | |
117
+
118
+ - ✅ 已验证可用
119
+ - 🔄 测试中,可能存在问题
120
+ - ❌ 待实现,适配器框架已搭建但未测试发布功能
121
+
122
+ ### 已验证平台
123
+
124
+ **登录 + 草稿发布**:知乎、掘金、CSDN、微信公众号
125
+
126
+ **仅登录验证**:小红书、微博、B站、头条号等(Cookie 获取成功,草稿发布待测试)
127
+
128
+ ```bash
129
+ # 查看所有支持平台
130
+ mpub platforms
131
+ ```
132
+
133
+ ---
134
+
135
+ ## 主题系统
136
+
137
+ ### 🎨 14 套内置主题
138
+
139
+ 完整预览:[themes/all-themes-preview.html](themes/all-themes-preview.html)
140
+
141
+ **主题截图示例:**
142
+
143
+ | 风格 | 代表主题 | 预览 |
144
+ |------|----------|------|
145
+ | 技术博客 | Cyberpunk | ![Cyberpunk](themes/previews/cyberpunk.png) |
146
+ | 程序员 | Nord | ![Nord](themes/previews/nord.png) |
147
+ | 微信风格 | Wechat | ![Wechat](themes/previews/wechat.png) |
148
+ | 简约 | Minimal | ![Minimal](themes/previews/minimal.png) |
149
+
150
+ **命令行预览:**
151
+ ```bash
152
+ mpub render -f article.md -t cyberpunk
153
+ mpub render -f article.md -t nord
154
+ mpub render -f article.md -t paper
155
+ ```
156
+
157
+ **全部 14 主题:** default、wechat、modern、minimal、cyberpunk、nord、paper、darkelite、sunset、zen、retro、midnight、brutalism、neumorphism
158
+
159
+ ### 使用主题
160
+
161
+ ```bash
162
+ # 渲染时指定主题
163
+ mpub render -f article.md -t cyberpunk
164
+ mpub render -f article.md -t nord
165
+ mpub render -f article.md -t paper
166
+
167
+ # 发布时指定主题
168
+ mpub publish -f article.md -t modern -p zhihu
169
+ ```
170
+
171
+ ### 自定义主题
172
+
173
+ ```bash
174
+ # 使用自定义 CSS 文件
175
+ mpub render -f article.md --custom-theme ./my-theme.css
176
+ ```
177
+
178
+ ```css
179
+ /* my-theme.css 示例 */
180
+ body { font-family: 'PingFang SC', sans-serif; }
181
+ h1 { color: #1a1a1a; font-size: 1.5em; }
182
+ pre { background: #f6f8fa; border-radius: 6px; }
183
+ ```
184
+
185
+ ---
186
+
187
+ ## 文章格式
188
+
189
+ ### front-matter 元数据
190
+
191
+ ```markdown
192
+ ---
193
+ title: 文章标题
194
+ author: 作者名
195
+ cover: https://example.com/cover.jpg # 封面图片 URL
196
+ summary: 文章摘要(可选) # 微信作者留言摘要
197
+ source_url: https://original.url # 原文链接(可选)
198
+ tags: [技术, 前端, JavaScript] # 标签(可选)
199
+ ---
200
+
201
+ 正文内容...
202
+ ```
203
+
204
+ ---
205
+
206
+ ## 配置文件
207
+
208
+ 所有配置存储在统一文件中,**无分散文件**:
209
+
210
+ ```json
211
+ // ~/.config/multi-publisher/config.json (Linux/macOS)
212
+ // %APPDATA%/multi-publisher/config.json (Windows)
213
+ {
214
+ "version": 1,
215
+ "weixin": {
216
+ "appId": "wx...",
217
+ "appSecret": "..."
218
+ },
219
+ "zhihu": {
220
+ "cookies": { "z_c0": "..." }
221
+ },
222
+ "juejin": {
223
+ "cookies": { "uid_tt": "..." }
224
+ }
225
+ }
226
+ ```
227
+
228
+ ```bash
229
+ # 查看配置文件路径
230
+ mpub credential --location
231
+ ```
232
+
233
+ ---
234
+
235
+ ## 项目结构
236
+
237
+ ```
238
+ src/
239
+ ├── index.ts # CLI 入口,命令注册
240
+ ├── config.ts # 统一配置管理(ConfigStore)
241
+
242
+ ├── cli/ # 命令行接口
243
+ │ ├── index.ts # Commander.js 入口
244
+ │ ├── publish.ts # publish 命令
245
+ │ ├── render.ts # render 命令(预览)
246
+ │ ├── platforms.ts # platforms 命令
247
+ │ ├── credential.ts # credential 命令(微信凭据)
248
+ │ ├── cookie.ts # cookie 命令(Cookie 管理)
249
+ │ ├── login.ts # login 命令(浏览器自动登录)
250
+ │ └── publish-all.ts # publish-all 命令
251
+
252
+ ├── core/ # 核心渲染引擎
253
+ │ ├── parser.ts # Markdown + front-matter 解析
254
+ │ ├── renderer.ts # 渲染管道(Markdown → HTML)
255
+ │ ├── styler.ts # CSS 内联(juice)
256
+ │ ├── mathjax.ts # LaTeX → SVG/PNG 公式
257
+ │ └── theme.ts # 主题加载与切换
258
+
259
+ ├── adapters/ # 平台适配器
260
+ │ ├── interface.ts # IPlatformAdapter 接口定义
261
+ │ ├── index.ts # 适配器注册表
262
+ │ ├── base-adapter.ts # 基础适配器
263
+ │ ├── registry.ts # 平台注册与管理
264
+ │ ├── wechat-publisher.ts # 微信公众号发布核心
265
+ │ ├── weixin.ts # 微信公众号适配器
266
+ │ ├── zhihu.ts # 知乎适配器
267
+ │ ├── juejin.ts # 掘金适配器
268
+ │ └── ... # 其他平台适配器
269
+
270
+ └── runtime/ # 运行时抽象
271
+ ├── index.ts # RuntimeInterface 接口
272
+ ├── node-runtime.ts # Node.js 运行时实现
273
+ └── browser-runtime.ts # Playwright 浏览器运行时
274
+ ```
275
+
276
+ ### 核心流程
277
+
278
+ ```
279
+ Markdown 文件
280
+
281
+
282
+ ┌─────────────┐ ┌──────────────┐ ┌────────────────┐
283
+ │ parser.ts │───▶│ renderer.ts │───▶│ styler.ts │
284
+ │ front-matter│ │ AST → HTML │ │ CSS 内联 │
285
+ │ Markdown 解析│ │ 代码高亮/LaTeX│ │ 主题应用 │
286
+ └─────────────┘ └──────────────┘ └────────────────┘
287
+
288
+ ┌──────────────────────────┘
289
+
290
+ ┌───────────────┐ ┌──────────────────────┐
291
+ │ wechat-publish│ │ platform adapter │
292
+ │ 图片上传 → media_id│ │ 调用平台 API 发布 │
293
+ └───────────────┘ └──────────────────────┘
294
+ ```
295
+
296
+ ### 适配器接口
297
+
298
+ ```typescript
299
+ interface IPlatformAdapter {
300
+ readonly meta: PlatformMeta // 平台元信息
301
+ init(runtime: RuntimeInterface): Promise<void>
302
+ checkAuth(): Promise<AuthResult> // 认证检查
303
+ publish(article: Article): Promise<SyncResult> // 发布文章
304
+ }
305
+ ```
306
+
307
+ ---
308
+
309
+ ## 开发
310
+
311
+ ```bash
312
+ # 安装依赖
313
+ npm install
314
+
315
+ # 开发模式
316
+ npm run dev
317
+
318
+ # 构建
319
+ npm run build
320
+
321
+ # 类型检查
322
+ npm run typecheck
323
+ ```
324
+
325
+ ### 添加新平台
326
+
327
+ 1. 在 `src/adapters/` 创建 `<platform>.ts`,实现 `IPlatformAdapter` 接口
328
+ 2. 在 `src/adapters/index.ts` 注册
329
+
330
+ ```typescript
331
+ // src/adapters/my-platform.ts
332
+ export class MyPlatformAdapter implements IPlatformAdapter {
333
+ readonly meta: PlatformMeta = {
334
+ id: 'myplatform',
335
+ name: '我的平台',
336
+ icon: 'https://...',
337
+ homepage: 'https://...',
338
+ capabilities: ['article', 'draft'],
339
+ }
340
+ // 实现 init / checkAuth / publish
341
+ }
342
+ ```
343
+
344
+ ```typescript
345
+ // src/adapters/index.ts
346
+ export { MyPlatformAdapter } from './my-platform.js'
347
+ // 添加到导出列表
348
+ ```
349
+
350
+ ---
351
+
352
+ ## FAQ
353
+
354
+ **Q: 微信公众号发布需要什么权限?**
355
+ > 需要已认证的公众号(订阅号或服务号),在微信公众平台 → 开发 → 基本配置获取 AppID 和 AppSecret。
356
+
357
+ **Q: 浏览器自动登录支持哪些平台?**
358
+ > 目前支持知乎、掘金、CSDN、小红书、微博、B站等 20+ 平台,更多平台持续添加中。
359
+
360
+ **Q: 多平台发布失败会怎样?**
361
+ > 已发布成功的平台不受影响,失败平台返回具体错误信息,支持重试。
362
+
363
+ **Q: 可以只发布到草稿箱吗?**
364
+ > 是的,所有平台默认发布到草稿箱,需手动在各平台确认发布。
365
+
366
+ ---
367
+
368
+ ## 许可证
369
+
370
+ Apache-2.0 License
@@ -0,0 +1,14 @@
1
+ /**
2
+ * 百家号适配器
3
+ * 认证方式: Cookie
4
+ */
5
+ import { BaseAdapter } from './base-adapter.js';
6
+ import type { Article, AuthResult, PlatformMeta } from './interface.js';
7
+ export declare class BaijiahaoAdapter extends BaseAdapter {
8
+ readonly meta: PlatformMeta;
9
+ private cookieData;
10
+ init(): Promise<void>;
11
+ protected getCookieConfigKey(): string;
12
+ checkAuth(): Promise<AuthResult>;
13
+ publish(article: Article): Promise<import('./interface.js').SyncResult>;
14
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * 百家号适配器
3
+ * 认证方式: Cookie
4
+ */
5
+ import { BaseAdapter } from './base-adapter.js';
6
+ export class BaijiahaoAdapter extends BaseAdapter {
7
+ meta = {
8
+ id: 'baijiahao',
9
+ name: '百家号',
10
+ icon: 'https://baijiahao.baidu.com/favicon.ico',
11
+ homepage: 'https://baijiahao.baidu.com/',
12
+ capabilities: ['article', 'draft', 'image_upload'],
13
+ };
14
+ cookieData = null;
15
+ async init() {
16
+ this.cookieData = await this.loadCookies();
17
+ }
18
+ getCookieConfigKey() {
19
+ return 'baijiahao';
20
+ }
21
+ async checkAuth() {
22
+ if (!this.cookieData) {
23
+ return { isAuthenticated: false, error: '未配置百家号 Cookie' };
24
+ }
25
+ try {
26
+ const cookieStr = this.buildCookieString(this.cookieData);
27
+ const res = await fetch('https://baijiahao.baidu.com/creator-center/stats', {
28
+ headers: {
29
+ 'Cookie': cookieStr,
30
+ ...this.getCommonHeaders(),
31
+ },
32
+ });
33
+ //百家号登录后会重定向到登录页
34
+ if (res.url.includes('passport.baidu.com')) {
35
+ return { isAuthenticated: false, error: '未登录或登录已过期' };
36
+ }
37
+ return { isAuthenticated: true };
38
+ }
39
+ catch (err) {
40
+ return { isAuthenticated: false, error: err.message };
41
+ }
42
+ }
43
+ async publish(article) {
44
+ const start = Date.now();
45
+ if (!this.cookieData) {
46
+ return this.createErrorResult('未配置百家号 Cookie', start);
47
+ }
48
+ try {
49
+ const cookieStr = this.buildCookieString(this.cookieData);
50
+ // 百家号内容发布 API
51
+ const res = await fetch('https://baijiahao.baidu.com/creator-center/content/save', {
52
+ method: 'POST',
53
+ headers: {
54
+ 'Content-Type': 'application/json',
55
+ 'Cookie': cookieStr,
56
+ ...this.getCommonHeaders(),
57
+ },
58
+ body: JSON.stringify({
59
+ title: article.title,
60
+ content: article.html || article.markdown || '',
61
+ draft: 1,
62
+ cover_image: article.cover || '',
63
+ }),
64
+ });
65
+ const data = await res.json();
66
+ if (data.code !== 0) {
67
+ throw new Error(data.msg || '发布失败');
68
+ }
69
+ const postId = String(data.data?.content_id || '');
70
+ const draftUrl = `https://baijiahao.baidu.com/creator-center/content/edit?content_id=${postId}`;
71
+ return this.createSuccessResult(postId, draftUrl, true, start);
72
+ }
73
+ catch (err) {
74
+ return this.createErrorResult(err.message, start);
75
+ }
76
+ }
77
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * 平台适配器基类
3
+ * 使用模板方法模式定义发布流程骨架
4
+ */
5
+ import type { IPlatformAdapter, Article, SyncResult, AuthResult, PlatformMeta } from './interface.js';
6
+ import type { RuntimeInterface } from '../runtime/index.js';
7
+ export declare abstract class BaseAdapter implements IPlatformAdapter {
8
+ abstract readonly meta: PlatformMeta;
9
+ protected runtime: RuntimeInterface;
10
+ abstract init(runtime: RuntimeInterface): Promise<void>;
11
+ abstract checkAuth(): Promise<AuthResult>;
12
+ abstract publish(article: Article): Promise<SyncResult>;
13
+ /**
14
+ * 获取 Cookie 配置键名(子类覆盖)
15
+ */
16
+ protected abstract getCookieConfigKey(): string;
17
+ /**
18
+ * 构建 Cookie 字符串
19
+ */
20
+ protected buildCookieString(cookies: Record<string, string>): string;
21
+ /**
22
+ * 加载平台 Cookie
23
+ */
24
+ protected loadCookies(): Promise<Record<string, string> | null>;
25
+ /**
26
+ * 默认的内容转换(子类可覆盖)
27
+ */
28
+ protected transformContent(content: string): string;
29
+ /**
30
+ * 默认的通用 HTTP headers
31
+ */
32
+ protected getCommonHeaders(): Record<string, string>;
33
+ /**
34
+ * 通用错误处理
35
+ */
36
+ protected createErrorResult(error: string, start?: number): SyncResult;
37
+ /**
38
+ * 通用成功结果
39
+ */
40
+ protected createSuccessResult(postId: string, postUrl: string, draftOnly?: boolean, start?: number): SyncResult;
41
+ }
@@ -0,0 +1,84 @@
1
+ import { ConfigStore } from '../config.js';
2
+ export class BaseAdapter {
3
+ runtime;
4
+ /**
5
+ * 构建 Cookie 字符串
6
+ */
7
+ buildCookieString(cookies) {
8
+ return Object.entries(cookies)
9
+ .map(([k, v]) => `${k}=${v}`)
10
+ .join('; ');
11
+ }
12
+ /**
13
+ * 加载平台 Cookie
14
+ */
15
+ async loadCookies() {
16
+ const configKey = this.getCookieConfigKey();
17
+ const getterMap = {
18
+ weibo: () => ConfigStore.getWeiboCookies(),
19
+ bilibili: () => ConfigStore.getBilibiliCookies(),
20
+ baijiahao: () => ConfigStore.getBaijiahaoCookies(),
21
+ cnblogs: () => ConfigStore.getCnblogsCookies(),
22
+ douban: () => ConfigStore.getDoubanCookies(),
23
+ eastmoney: () => ConfigStore.getEastmoneyCookies(),
24
+ imooc: () => ConfigStore.getImoocCookies(),
25
+ cto51: () => ConfigStore.getCto51Cookies(),
26
+ oschina: () => ConfigStore.getOschinaCookies(),
27
+ segmentfault: () => ConfigStore.getSegmentfaultCookies(),
28
+ sohu: () => ConfigStore.getSohuCookies(),
29
+ woshipm: () => ConfigStore.getWoshipmCookies(),
30
+ xueqiu: () => ConfigStore.getYueqiuCookies(), // xueqiu (雪球) uses yueqiu config key
31
+ jianshu: () => ConfigStore.getJianshuCookies(),
32
+ xiaohongshu: () => ConfigStore.getXiaohongshuCookies(),
33
+ toutiao: () => ConfigStore.getToutiaoCookies(),
34
+ };
35
+ const getter = getterMap[configKey];
36
+ if (!getter) {
37
+ console.warn(`[${this.meta.id}] 未知的 Cookie 配置键: ${configKey}`);
38
+ return null;
39
+ }
40
+ return getter();
41
+ }
42
+ /**
43
+ * 默认的内容转换(子类可覆盖)
44
+ */
45
+ transformContent(content) {
46
+ return content
47
+ .replace(/<img([^>]+)src="([^"]+)"([^>]*)>/gi, '<figure><img$1src="$2"$3></figure>')
48
+ .replace(/\s*data-(?!draft)[a-z-]+="[^"]*"/gi, '')
49
+ .replace(/\s*style="[^"]*"/gi, '');
50
+ }
51
+ /**
52
+ * 默认的通用 HTTP headers
53
+ */
54
+ getCommonHeaders() {
55
+ return {
56
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
57
+ 'Accept': 'application/json, text/plain, */*',
58
+ };
59
+ }
60
+ /**
61
+ * 通用错误处理
62
+ */
63
+ createErrorResult(error, start) {
64
+ return {
65
+ platform: this.meta.id,
66
+ success: false,
67
+ error,
68
+ timestamp: start ? Date.now() - start : 0,
69
+ };
70
+ }
71
+ /**
72
+ * 通用成功结果
73
+ */
74
+ createSuccessResult(postId, postUrl, draftOnly = true, start) {
75
+ return {
76
+ platform: this.meta.id,
77
+ success: true,
78
+ postId,
79
+ postUrl,
80
+ draftOnly,
81
+ timestamp: start ? Date.now() - start : 0,
82
+ };
83
+ }
84
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * B站 (Bilibili) 适配器
3
+ * 认证方式: Cookie
4
+ */
5
+ import { BaseAdapter } from './base-adapter.js';
6
+ import type { Article, AuthResult, PlatformMeta } from './interface.js';
7
+ export declare class BilibiliAdapter extends BaseAdapter {
8
+ readonly meta: PlatformMeta;
9
+ private cookieData;
10
+ init(): Promise<void>;
11
+ protected getCookieConfigKey(): string;
12
+ checkAuth(): Promise<AuthResult>;
13
+ publish(article: Article): Promise<import('./interface.js').SyncResult>;
14
+ }