@wenyan-md/core 1.0.6 → 1.0.8

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.
package/README.md CHANGED
@@ -1,3 +1,229 @@
1
1
  <div align="center">
2
- <img alt = "logo" src="https://raw.githubusercontent.com/caol64/wenyan/main/Data/256-mac.png" />
2
+ <img alt="logo" src="https://raw.githubusercontent.com/caol64/wenyan/main/Data/256-mac.png" />
3
3
  </div>
4
+
5
+ # 文颜 CORE
6
+
7
+ 「文颜」是一款多平台排版美化工具,让你将 Markdown 一键发布至微信公众号、知乎、今日头条等主流写作平台。
8
+
9
+ **文颜**现已推出多个版本:
10
+
11
+ * [macOS App Store 版](https://github.com/caol64/wenyan) - MAC 桌面应用
12
+ * [Windows + Linux 版](https://github.com/caol64/wenyan-pc) - 跨平台桌面应用
13
+ * [CLI 版本](https://github.com/caol64/wenyan-cli) - CI/CD 或脚本自动化发布公众号文章
14
+ * [MCP 版本](https://github.com/caol64/wenyan-mcp) - 让 AI 自动发布公众号文章
15
+
16
+ 本项目是 **文颜的核心库文件**,你可以将其方便地嵌入自己的应用中,以实现排版美化和自动发布功能。
17
+
18
+ ## 功能
19
+
20
+ * 使用内置主题对 Markdown 内容排版
21
+ * 支持图片自动上传
22
+ * 支持数学公式渲染
23
+ * 一键发布文章到微信公众号草稿箱
24
+
25
+ ## 主题效果
26
+
27
+ 👉 [内置主题预览](https://yuzhi.tech/docs/wenyan/theme)
28
+
29
+ 文颜采用了多个开源的 Typora 主题,在此向各位作者表示感谢:
30
+
31
+ - [Orange Heart](https://github.com/evgo2017/typora-theme-orange-heart)
32
+ - [Rainbow](https://github.com/thezbm/typora-theme-rainbow)
33
+ - [Lapis](https://github.com/YiNNx/typora-theme-lapis)
34
+ - [Pie](https://github.com/kevinzhao2233/typora-theme-pie)
35
+ - [Maize](https://github.com/BEATREE/typora-maize-theme)
36
+ - [Purple](https://github.com/hliu202/typora-purple-theme)
37
+ - [物理猫-薄荷](https://github.com/sumruler/typora-theme-phycat)
38
+
39
+ ## 安装方式
40
+
41
+ ```bash
42
+ pnpm add @wenyan-md/core
43
+ # 或者
44
+ npm install @wenyan-md/core
45
+ # 或者
46
+ yarn add @wenyan-md/core
47
+ ```
48
+
49
+ ## 使用示例
50
+
51
+ ### 1. Markdown 排版美化
52
+
53
+ ```ts
54
+ import { getGzhContent } from "@wenyan-md/core/wrapper";
55
+
56
+ const inputContent = "# Hello, Wenyan";
57
+ const theme = "lapis";
58
+ const highlightTheme = "solarized-light";
59
+ const isMacStyle = true;
60
+
61
+ const { title, cover, content, description } = await getGzhContent(
62
+ inputContent,
63
+ theme,
64
+ highlightTheme,
65
+ isMacStyle
66
+ );
67
+ ```
68
+
69
+ #### 参数说明
70
+
71
+ | 参数名 | 类型 | 说明 |
72
+ | ---------------- | --------- | ----------------------------------------------- |
73
+ | `inputContent` | `string` | 输入的 Markdown 文本 |
74
+ | `theme` | `string` | 排版主题 ID(如 `"lapis"`, `"default"` 等,见下文) |
75
+ | `highlightTheme` | `string` | 代码高亮主题(如 `"github"`, `"solarized-light"`, 见下文) |
76
+ | `isMacStyle` | `boolean` | 代码块是否启用 Mac 风格 |
77
+
78
+ 排版主题可选参数:
79
+
80
+ - default
81
+ - orangeheart
82
+ - rainbow
83
+ - lapis
84
+ - pie
85
+ - maize
86
+ - purple
87
+ - phycat
88
+
89
+ 高亮主题可选参数:
90
+
91
+ - atom-one-dark
92
+ - atom-one-light
93
+ - dracula
94
+ - github-dark
95
+ - github
96
+ - monokai
97
+ - solarized-dark
98
+ - solarized-light
99
+ - xcode
100
+
101
+ #### 返回值
102
+
103
+ | 字段 | 类型 | 说明 |
104
+ | ------------- | -------- | ---------------------- |
105
+ | `title` | `string` | `frontmatter`中的文章标题,见下文 |
106
+ | `cover` | `string` | `frontmatter`中的文章封面图,见下文 |
107
+ | `content` | `string` | 转换后的 HTML 文章内容,发布接口需要用到 |
108
+ | `description` | `string` | `frontmatter`中的文章简介,见下文 |
109
+
110
+ ---
111
+
112
+ ### 2. 发布到微信公众号草稿箱
113
+
114
+ ```ts
115
+ import { publishToDraft } from "@wenyan-md/core/publish";
116
+
117
+ // 首先确保环境变量有效
118
+ const wechatAppId = process.env.WECHAT_APP_ID;
119
+ const wechatAppSecret = process.env.WECHAT_APP_SECRET;
120
+
121
+ if (!wechatAppId || !wechatAppSecret) {
122
+ console.error("WECHAT_APP_ID and WECHAT_APP_SECRET must be set as environment variables.");
123
+ process.exit(1);
124
+ }
125
+
126
+ const data = await publishToDraft(title, content, cover);
127
+
128
+ if (data.media_id) {
129
+ console.log(`上传成功,media_id: ${data.media_id}`);
130
+ } else {
131
+ console.error(`上传失败,\n${data}`);
132
+ }
133
+ ```
134
+
135
+ #### 参数说明
136
+
137
+ | 参数名 | 类型 | 说明 |
138
+ | --------- | -------- | ---------- |
139
+ | `title` | `string` | 文章标题 |
140
+ | `content` | `string` | 文章 HTML 内容 |
141
+ | `cover` | `string` | 封面图 URL |
142
+
143
+ #### 返回值
144
+
145
+ 返回 **微信公众号 API 的响应对象**,常见字段:
146
+
147
+ | 字段 | 类型 | 说明 |
148
+ | ---------- | -------- | --------------------- |
149
+ | `media_id` | `string` | 草稿的 media\_id,后续发布时需要 |
150
+
151
+ ## 环境变量
152
+
153
+ 在使用 `publishToDraft` 前,需要在环境中配置:
154
+
155
+ * `WECHAT_APP_ID`
156
+ * `WECHAT_APP_SECRET`
157
+
158
+ 推荐通过 `.env` 文件或 CI/CD 环境变量注入。
159
+
160
+ ## 浏览器直接使用
161
+
162
+ 除了通过 `npm` 安装外,你也可以直接在浏览器环境中引入打包好的版本(IIFE 格式),无需构建工具。
163
+
164
+ 推荐使用 **[unpkg](https://unpkg.com/)** 或 **[jsDelivr](https://www.jsdelivr.com/)**。
165
+
166
+ ```html
167
+ <!-- 从 unpkg 引入 -->
168
+ <script src="https://unpkg.com/@wenyan-md/core/dist/browser/wenyan-core.js"></script>
169
+
170
+ <!-- 或者从 jsDelivr 引入 -->
171
+ <script src="https://cdn.jsdelivr.net/npm/@wenyan-md/core/dist/browser/wenyan-core.js"></script>
172
+
173
+ <script>
174
+ // 使用全局变量 WenyanCore
175
+ const { configureMarked, renderMarkdown, themes } = WenyanCore;
176
+
177
+ (async () => {
178
+ configureMarked();
179
+ const input = "# Hello from Browser";
180
+ const content = await renderMarkdown(input);
181
+ const theme = themes["lapis"];
182
+ const styledCss = await theme.getCss();
183
+ const style = document.createElement("style");
184
+ style.textContent = styledCss;
185
+ document.head.appendChild(style);
186
+ document.body.innerHTML = content;
187
+ })();
188
+ </script>
189
+ ```
190
+
191
+ 这样你就可以在 **任意前端项目** 或 **纯静态页面** 中直接使用文颜的功能。
192
+
193
+ ## 微信公众号 IP 白名单
194
+
195
+ 请务必将服务器 IP 加入公众号平台的 IP 白名单,以确保上传接口调用成功。
196
+ 详细配置说明请参考:[https://yuzhi.tech/docs/wenyan/upload](https://yuzhi.tech/docs/wenyan/upload)
197
+
198
+ ## 配置说明(Frontmatter)
199
+
200
+ 为了可以正确上传文章,需要在每一篇 Markdown 文章的开头添加一段`frontmatter`,提供`title`、`cover`两个字段:
201
+
202
+ ```md
203
+ ---
204
+ title: 在本地跑一个大语言模型(2) - 给模型提供外部知识库
205
+ cover: /Users/lei/Downloads/result_image.jpg
206
+ description: 本文介绍如何为本地大语言模型提供外部知识库。
207
+ ---
208
+ ```
209
+
210
+ * `title` 是文章标题,必填。
211
+ * `cover` 是文章封面,支持本地路径和网络图片:
212
+
213
+ * 如果正文有至少一张图片,可省略,此时将使用其中一张作为封面;
214
+ * 如果正文无图片,则必须提供 cover。
215
+
216
+ ## 关于图片自动上传
217
+
218
+ * 支持图片路径:
219
+
220
+ * 本地路径(如:`/Users/lei/Downloads/result_image.jpg`)
221
+ * 网络路径(如:`https://example.com/image.jpg`)
222
+
223
+ ## 赞助
224
+
225
+ 如果您觉得不错,可以给我家猫咪买点罐头吃。[喂猫❤️](https://yuzhi.tech/sponsor)
226
+
227
+ ## License
228
+
229
+ Apache License Version 2.0