wenyan-md-cli-evlon 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.
- package/LICENSE +201 -0
- package/README.docker.md +178 -0
- package/README.md +171 -0
- package/dist/cli.js +139 -0
- package/dist/commands/serve.js +228 -0
- package/dist/index.js +1 -0
- package/dist/types/cli.d.ts +4 -0
- package/dist/types/commands/serve.d.ts +6 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/utils.d.ts +6 -0
- package/dist/utils.js +48 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.docker.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img alt="logo" src="https://raw.githubusercontent.com/caol64/wenyan/main/Data/256-mac.png" width="120" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
# wenyan-cli (Docker)
|
|
6
|
+
|
|
7
|
+
**Render Markdown to beautifully styled articles and publish to content platforms — powered by Docker.**
|
|
8
|
+
|
|
9
|
+
> This image bundles **wenyan CLI** and all required runtime dependencies.
|
|
10
|
+
> No local Node.js or npm environment required.
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
13
|
+
|
|
14
|
+
### Pull image
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
docker pull caol64/wenyan-cli
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Show help
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
docker run --rm caol64/wenyan-cli --help
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Basic Usage
|
|
27
|
+
|
|
28
|
+
Render and publish a Markdown file to WeChat Official Account draft box:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
docker run --rm \
|
|
32
|
+
--env-file .env \
|
|
33
|
+
-e HOST_FILE_PATH=$(pwd) \
|
|
34
|
+
-v $(pwd):/mnt/host-downloads \
|
|
35
|
+
caol64/wenyan-cli \
|
|
36
|
+
publish -f ./article.md
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Render Markdown content directly:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
docker run --rm caol64/wenyan-cli \
|
|
43
|
+
render "# Hello Wenyan"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Working with Local Files (Recommended)
|
|
47
|
+
|
|
48
|
+
When using local Markdown or image files, mount the current directory:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
docker run --rm \
|
|
52
|
+
-e HOST_FILE_PATH=$(pwd) \
|
|
53
|
+
-v $(pwd):/mnt/host-downloads \
|
|
54
|
+
caol64/wenyan-cli \
|
|
55
|
+
publish -f ./example.md
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**How it works:**
|
|
59
|
+
|
|
60
|
+
| Path | Description |
|
|
61
|
+
| --------------------- | ----------------------------- |
|
|
62
|
+
| `HOST_FILE_PATH` | Absolute path on host machine |
|
|
63
|
+
| `/mnt/host-downloads` | Mounted path inside container |
|
|
64
|
+
|
|
65
|
+
All file paths in Markdown (cover / images) should reference host paths.
|
|
66
|
+
|
|
67
|
+
## Input Methods
|
|
68
|
+
|
|
69
|
+
`publish` supports **exactly one** input source:
|
|
70
|
+
|
|
71
|
+
- `-f <file>` — read Markdown from local file
|
|
72
|
+
- `<input-content>` — inline Markdown string
|
|
73
|
+
- `stdin` — pipe from another command
|
|
74
|
+
|
|
75
|
+
Examples:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
cat article.md | docker run --rm -i caol64/wenyan-cli render
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
docker run --rm caol64/wenyan-cli render "# Title"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Options
|
|
86
|
+
|
|
87
|
+
Commonly used options:
|
|
88
|
+
|
|
89
|
+
- `-t, --theme` — theme ID (default: `default`)
|
|
90
|
+
- `-h, --highlight` — code highlight theme
|
|
91
|
+
- `--no-mac-style` — disable macOS-style code blocks
|
|
92
|
+
- `--no-footnote` — disable link-to-footnote conversion
|
|
93
|
+
|
|
94
|
+
## Markdown Frontmatter (Required)
|
|
95
|
+
|
|
96
|
+
Each Markdown file must include frontmatter:
|
|
97
|
+
|
|
98
|
+
```md
|
|
99
|
+
---
|
|
100
|
+
title: My Article Title
|
|
101
|
+
cover: /absolute/path/to/cover.jpg
|
|
102
|
+
---
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
- `title` — article title (required)
|
|
106
|
+
- `cover` — optional cover image (local or remote)
|
|
107
|
+
|
|
108
|
+
## Environment Variables
|
|
109
|
+
|
|
110
|
+
Publishing to WeChat requires:
|
|
111
|
+
|
|
112
|
+
- `WECHAT_APP_ID`
|
|
113
|
+
- `WECHAT_APP_SECRET`
|
|
114
|
+
|
|
115
|
+
Recommended usage with `.env` file:
|
|
116
|
+
|
|
117
|
+
```env
|
|
118
|
+
WECHAT_APP_ID=xxx
|
|
119
|
+
WECHAT_APP_SECRET=yyy
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Image Details
|
|
123
|
+
|
|
124
|
+
- Entrypoint: `wenyan`
|
|
125
|
+
- Runtime: Node.js (bundled)
|
|
126
|
+
- Architecture: `linux/amd64`, `linux/arm64`
|
|
127
|
+
|
|
128
|
+
## Server Mode
|
|
129
|
+
|
|
130
|
+
Deploy on a cloud server with fixed IP to solve WeChat API whitelist requirements:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
docker run -d --name wenyan-server \
|
|
134
|
+
-p 3000:3000 \
|
|
135
|
+
--env-file .env \
|
|
136
|
+
caol64/wenyan-cli \
|
|
137
|
+
serve --port 3000
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Then call the REST API from your local machine:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# Health check
|
|
144
|
+
curl http://your-server-ip:3000/health
|
|
145
|
+
|
|
146
|
+
# Render
|
|
147
|
+
curl -X POST http://your-server-ip:3000/render \
|
|
148
|
+
-H "Content-Type: application/json" \
|
|
149
|
+
-d '{"content": "# Hello World", "theme": "default"}'
|
|
150
|
+
|
|
151
|
+
# Publish
|
|
152
|
+
curl -X POST http://your-server-ip:3000/publish \
|
|
153
|
+
-H "Content-Type: application/json" \
|
|
154
|
+
-d '{"file": "/mnt/host-downloads/article.md"}'
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
> **Note:** Add your server's public IP to WeChat Official Account whitelist once, and it works permanently.
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
Apache License Version 2.0
|
|
162
|
+
|
|
163
|
+
### Tip
|
|
164
|
+
|
|
165
|
+
For frequent usage, create an alias:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
alias wenyan='docker run --rm \
|
|
169
|
+
-e HOST_FILE_PATH=$(pwd) \
|
|
170
|
+
-v $(pwd):/mnt/host-downloads \
|
|
171
|
+
caol64/wenyan-cli'
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Then use it like a native CLI:
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
wenyan publish -f article.md
|
|
178
|
+
```
|
package/README.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img alt="logo" src="https://raw.githubusercontent.com/caol64/wenyan/main/Data/256-mac.png" width="128" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
# 文颜 CLI
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@wenyan-md/cli)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+

|
|
10
|
+
[](https://hub.docker.com/r/caol64/wenyan-cli)
|
|
11
|
+
[](https://github.com/caol64/wenyan-cli)
|
|
12
|
+
|
|
13
|
+
## 简介
|
|
14
|
+
|
|
15
|
+
**[文颜(Wenyan)](https://wenyan.yuzhi.tech)** 是一款多平台 Markdown 排版与发布工具,支持将 Markdown 一键转换并发布至:
|
|
16
|
+
|
|
17
|
+
- 微信公众号
|
|
18
|
+
- 知乎
|
|
19
|
+
- 今日头条
|
|
20
|
+
- 以及其它内容平台(持续扩展中)
|
|
21
|
+
|
|
22
|
+
文颜的目标是:**让写作者专注内容,而不是排版和平台适配**。
|
|
23
|
+
|
|
24
|
+
## 文颜的不同版本
|
|
25
|
+
|
|
26
|
+
文颜目前提供多种形态,覆盖不同使用场景:
|
|
27
|
+
|
|
28
|
+
- [macOS App Store 版](https://github.com/caol64/wenyan) - MAC 桌面应用
|
|
29
|
+
- [跨平台桌面版](https://github.com/caol64/wenyan-pc) - Windows/Linux
|
|
30
|
+
- 👉[CLI 版本](https://github.com/caol64/wenyan-cli) - 本项目
|
|
31
|
+
- [MCP 版本](https://github.com/caol64/wenyan-mcp) - AI 自动发文
|
|
32
|
+
|
|
33
|
+
## 特性
|
|
34
|
+
|
|
35
|
+
- 一键发布 Markdown 到微信公众号草稿箱
|
|
36
|
+
- 自动上传本地图片与封面
|
|
37
|
+
- 支持远程 Server 发布(绕过 IP 白名单限制)
|
|
38
|
+
- 内置多套精美排版主题
|
|
39
|
+
- 支持自定义主题
|
|
40
|
+
- 可作为 CI/CD 自动发文工具
|
|
41
|
+
- 可集成 AI Agent 自动发布
|
|
42
|
+
|
|
43
|
+
## 快速开始
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# 安装
|
|
47
|
+
npm install -g @wenyan-md/cli
|
|
48
|
+
# 发布文章到公众号
|
|
49
|
+
wenyan publish -f article.md
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## 命令概览
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
wenyan <command> [options]
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
| 命令 | 说明 |
|
|
59
|
+
| ------- | --------- |
|
|
60
|
+
| [publish](docs/publish.md) | 发布文章 |
|
|
61
|
+
| render | 渲染 HTML |
|
|
62
|
+
| [theme](docs/theme.md) | 管理主题 |
|
|
63
|
+
| [serve](docs/server.md) | 启动 Server |
|
|
64
|
+
|
|
65
|
+
## 概念
|
|
66
|
+
|
|
67
|
+
### 内容输入
|
|
68
|
+
|
|
69
|
+
内容输入是指如何把 Markdown 文章分发给 `wenyan-cli`,支持以下四种方式:
|
|
70
|
+
|
|
71
|
+
| 方式 | 示例 | 说明 |
|
|
72
|
+
| ------- | --------- |--------- |
|
|
73
|
+
| 本地路径(推荐) | `wenyan publish -f article.md` |`cli`直接读取磁盘上的文章 |
|
|
74
|
+
| URL | `wenyan publish -f http://test.md` |`cli`直接读取网络上的文章 |
|
|
75
|
+
| 参数 | `wenyan publish "# 文章"` |适用于快速发布短内容 |
|
|
76
|
+
| 管道 | `cat article.md \| wenyan publish` |适用于 CI/CD,脚本批量发布 |
|
|
77
|
+
|
|
78
|
+
### 环境变量配置
|
|
79
|
+
|
|
80
|
+
> [!IMPORTANT]
|
|
81
|
+
>
|
|
82
|
+
> 请确保运行文颜的机器已配置如下环境变量,否则上传接口将调用失败。
|
|
83
|
+
|
|
84
|
+
- `WECHAT_APP_ID`
|
|
85
|
+
- `WECHAT_APP_SECRET`
|
|
86
|
+
|
|
87
|
+
### 微信公众号 IP 白名单
|
|
88
|
+
|
|
89
|
+
> [!IMPORTANT]
|
|
90
|
+
>
|
|
91
|
+
> 请确保运行文颜的机器 IP 已加入微信公众号后台的 IP 白名单,否则上传接口将调用失败。
|
|
92
|
+
|
|
93
|
+
配置说明文档:[https://yuzhi.tech/docs/wenyan/upload](https://yuzhi.tech/docs/wenyan/upload)
|
|
94
|
+
|
|
95
|
+
### 文章格式
|
|
96
|
+
|
|
97
|
+
为了正确上传文章,每篇 Markdown 顶部需要包含一段 `frontmatter`:
|
|
98
|
+
|
|
99
|
+
```md
|
|
100
|
+
---
|
|
101
|
+
title: 在本地跑一个大语言模型(2) - 给模型提供外部知识库
|
|
102
|
+
cover: /Users/xxx/image.jpg
|
|
103
|
+
author: xxx
|
|
104
|
+
source_url: http://
|
|
105
|
+
---
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
字段说明:
|
|
109
|
+
|
|
110
|
+
- `title` 文章标题(必填)
|
|
111
|
+
- `cover` 文章封面
|
|
112
|
+
- 本地路径或网络图片
|
|
113
|
+
- 如果正文中已有图片,可省略
|
|
114
|
+
- `author` 文章作者
|
|
115
|
+
- `source_url` 原文地址
|
|
116
|
+
|
|
117
|
+
**[示例文章](tests/publish.md)**
|
|
118
|
+
|
|
119
|
+
### 文内图片和文章封面
|
|
120
|
+
|
|
121
|
+
把文章发布到公众号之前,文颜会按照微信要求自动处理文章内的所有图片,将其上传到公众号素材库。目前文颜对于以下两种图片都能很好的支持:
|
|
122
|
+
|
|
123
|
+
- 本地硬盘绝对路径(如:`/Users/xxx/image.jpg`)
|
|
124
|
+
- 网络路径(如:`https://example.com/image.jpg`)
|
|
125
|
+
|
|
126
|
+
仅当“内容输入”方式为“本地路径”时,以下路径也能完美支持:
|
|
127
|
+
|
|
128
|
+
- 当前文章的相对路径(如:`./assets/image.png`)
|
|
129
|
+
|
|
130
|
+
## Server 模式
|
|
131
|
+
|
|
132
|
+
相较于纯本地运行的**本地模式(Local Mode)**,`wenyan-cli`还提供了 **远程客户端模式(Client–Server Mode)**。两种模式运行效果完全一致,你可以根据运行环境和网络条件选择最合适的方式。
|
|
133
|
+
|
|
134
|
+
在本地模式下,CLI 直接调用微信公众号 API 完成图片上传和草稿发布。
|
|
135
|
+
|
|
136
|
+
```mermaid
|
|
137
|
+
flowchart LR
|
|
138
|
+
CLI[Wenyan CLI] --> Wechat[公众号 API]
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
在远程客户端模式下,CLI 作为客户端,将发布请求发送到部署在云服务器上的 Wenyan Server,由 Server 完成微信公众号 API 调用。
|
|
142
|
+
|
|
143
|
+
```mermaid
|
|
144
|
+
flowchart LR
|
|
145
|
+
CLI[Wenyan CLI] --> Server[Wenyan Server] --> Wechat[公众号 API]
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
**适用于:**
|
|
149
|
+
|
|
150
|
+
* 无本地固定 IP,需频繁添加IP 白名单的用户
|
|
151
|
+
* 需团队协作的用户
|
|
152
|
+
* 支持 CI/CD 自动发布
|
|
153
|
+
* 支持 AI Agent 自动发布
|
|
154
|
+
|
|
155
|
+
**[Server 模式部署](docs/server.md)**
|
|
156
|
+
|
|
157
|
+
客户端调用 Server 发布:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
wenyan publish -f article.md --server https://api.example.com --api-key your-api-key
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## 赞助
|
|
164
|
+
|
|
165
|
+
如果你觉得文颜对你有帮助,可以给我家猫咪买点罐头 ❤️
|
|
166
|
+
|
|
167
|
+
[https://yuzhi.tech/sponsor](https://yuzhi.tech/sponsor)
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
Apache License Version 2.0
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import pkg from "../package.json" with { type: "json" };
|
|
4
|
+
import { addTheme, listThemes, prepareRenderContext, removeTheme, renderAndPublish, renderAndPublishToServer, } from "@wenyan-md/core/wrapper";
|
|
5
|
+
import { getInputContent } from "./utils.js";
|
|
6
|
+
export function createProgram(version = pkg.version) {
|
|
7
|
+
const program = new Command();
|
|
8
|
+
program
|
|
9
|
+
.name("wenyan")
|
|
10
|
+
.description("A CLI for WenYan Markdown Render.")
|
|
11
|
+
.version(version, "-v, --version", "output the current version")
|
|
12
|
+
.action(() => {
|
|
13
|
+
program.outputHelp();
|
|
14
|
+
});
|
|
15
|
+
const addCommonOptions = (cmd) => {
|
|
16
|
+
return cmd
|
|
17
|
+
.argument("[input-content]", "markdown content (string input)")
|
|
18
|
+
.option("-f, --file <path>", "read markdown content from local file or web URL")
|
|
19
|
+
.option("-t, --theme <theme-id>", "ID of the theme to use", "default")
|
|
20
|
+
.option("-h, --highlight <highlight-theme-id>", "ID of the code highlight theme to use", "solarized-light")
|
|
21
|
+
.option("-c, --custom-theme <path>", "path to custom theme CSS file")
|
|
22
|
+
.option("--mac-style", "display codeblock with mac style", true)
|
|
23
|
+
.option("--no-mac-style", "disable mac style")
|
|
24
|
+
.option("--footnote", "convert link to footnote", true)
|
|
25
|
+
.option("--no-footnote", "disable footnote");
|
|
26
|
+
};
|
|
27
|
+
const pubCmd = program
|
|
28
|
+
.command("publish")
|
|
29
|
+
.description("Render a markdown file to styled HTML and publish to wechat GZH");
|
|
30
|
+
// 先添加公共选项,再追加 publish 专属选项
|
|
31
|
+
addCommonOptions(pubCmd)
|
|
32
|
+
.option("--server <url>", "Server URL to publish through (e.g. https://api.yourdomain.com)")
|
|
33
|
+
.option("--api-key <apiKey>", "API key for the remote server")
|
|
34
|
+
.option("--app-id [id]", "WeChat ID to publish to</id>, if not specified, will use the env variable WECHAT_APP_ID")
|
|
35
|
+
.option("--app-secret [secret]", "WeChat Secret to publish to</secret>, if not specified, will use the env variable WECHAT_APP_SECRET")
|
|
36
|
+
.action(async (inputContent, options) => {
|
|
37
|
+
await runCommandWrapper(async () => {
|
|
38
|
+
// 如果传入了 --server,则走客户端(远程)模式
|
|
39
|
+
if (options.server) {
|
|
40
|
+
if (!options.appId) {
|
|
41
|
+
options.appId = process.env.WECHAT_APP_ID;
|
|
42
|
+
}
|
|
43
|
+
if (!options.appSecret) {
|
|
44
|
+
options.appSecret = process.env.WECHAT_APP_SECRET;
|
|
45
|
+
}
|
|
46
|
+
options.clientVersion = version; // 将 CLI 版本传递给服务器,便于调试和兼容性处理
|
|
47
|
+
const mediaId = await renderAndPublishToServer(inputContent, options, getInputContent);
|
|
48
|
+
console.log(`发布成功,Media ID: ${mediaId}`);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// 走原有的本地直接发布模式
|
|
52
|
+
const mediaId = await renderAndPublish(inputContent, options, getInputContent);
|
|
53
|
+
console.log(`发布成功,Media ID: ${mediaId}`);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
const renderCmd = program.command("render").description("Render a markdown file to styled HTML");
|
|
58
|
+
addCommonOptions(renderCmd).action(async (inputContent, options) => {
|
|
59
|
+
await runCommandWrapper(async () => {
|
|
60
|
+
const { gzhContent } = await prepareRenderContext(inputContent, options, getInputContent);
|
|
61
|
+
console.log(gzhContent.content);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
program
|
|
65
|
+
.command("theme")
|
|
66
|
+
.description("Manage themes")
|
|
67
|
+
.option("-l, --list", "List all available themes")
|
|
68
|
+
.option("--add", "Add a new custom theme")
|
|
69
|
+
.option("--name <name>", "Name of the new custom theme")
|
|
70
|
+
.option("--path <path>", "Path to the new custom theme CSS file")
|
|
71
|
+
.option("--rm <name>", "Name of the custom theme to remove")
|
|
72
|
+
.action(async (options) => {
|
|
73
|
+
await runCommandWrapper(async () => {
|
|
74
|
+
const { list, add, name, path, rm } = options;
|
|
75
|
+
if (list) {
|
|
76
|
+
const themes = await listThemes();
|
|
77
|
+
console.log("内置主题:");
|
|
78
|
+
themes
|
|
79
|
+
.filter((theme) => theme.isBuiltin)
|
|
80
|
+
.forEach((theme) => {
|
|
81
|
+
console.log(`- ${theme.id}: ${theme.description ?? ""}`);
|
|
82
|
+
});
|
|
83
|
+
const customThemes = themes.filter((theme) => !theme.isBuiltin);
|
|
84
|
+
if (customThemes.length > 0) {
|
|
85
|
+
console.log("\n自定义主题:");
|
|
86
|
+
customThemes.forEach((theme) => {
|
|
87
|
+
console.log(`- ${theme.id}: ${theme.description ?? ""}`);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (add) {
|
|
93
|
+
await addTheme(name, path);
|
|
94
|
+
console.log(`主题 "${name}" 已添加`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (rm) {
|
|
98
|
+
await removeTheme(rm);
|
|
99
|
+
console.log(`主题 "${rm}" 已删除`);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
program
|
|
104
|
+
.command("serve")
|
|
105
|
+
.description("Start a server to provide HTTP API for rendering and publishing")
|
|
106
|
+
.option("-p, --port <port>", "Port to listen on (default: 3000)", "3000")
|
|
107
|
+
.option("--api-key <apiKey>", "API key for authentication")
|
|
108
|
+
.action(async (options) => {
|
|
109
|
+
try {
|
|
110
|
+
const { serveCommand } = await import("./commands/serve.js");
|
|
111
|
+
const port = options.port ? parseInt(options.port, 10) : 3000;
|
|
112
|
+
await serveCommand({ port, version, apiKey: options.apiKey });
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error(error.message);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return program;
|
|
120
|
+
}
|
|
121
|
+
// --- 统一的错误处理包装器 ---
|
|
122
|
+
async function runCommandWrapper(action) {
|
|
123
|
+
try {
|
|
124
|
+
await action();
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
if (error instanceof Error) {
|
|
128
|
+
console.error(error.message);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
console.error("An unexpected error occurred:", error);
|
|
132
|
+
}
|
|
133
|
+
process.exit(1);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
export const program = createProgram();
|
|
137
|
+
if (import.meta.main) {
|
|
138
|
+
program.parse(process.argv);
|
|
139
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import crypto from "node:crypto";
|
|
5
|
+
import { configDir } from "@wenyan-md/core/wrapper";
|
|
6
|
+
import multer from "multer";
|
|
7
|
+
import { publishToWechatDraft } from "@wenyan-md/core/wrapper";
|
|
8
|
+
class AppError extends Error {
|
|
9
|
+
message;
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.message = message;
|
|
13
|
+
this.name = "AppError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const UPLOAD_TTL_MS = 10 * 60 * 1000; // 10 minutes
|
|
17
|
+
const UPLOAD_DIR = path.join(configDir, "uploads");
|
|
18
|
+
export async function serveCommand(options) {
|
|
19
|
+
// 确保临时目录存在
|
|
20
|
+
await fs.mkdir(UPLOAD_DIR, { recursive: true });
|
|
21
|
+
// 服务启动时立即执行一次后台清理
|
|
22
|
+
cleanupOldUploads();
|
|
23
|
+
// 定期清理过期的上传文件
|
|
24
|
+
setInterval(cleanupOldUploads, UPLOAD_TTL_MS).unref();
|
|
25
|
+
const app = express();
|
|
26
|
+
const port = options.port || 3000;
|
|
27
|
+
const auth = createAuthHandler(options);
|
|
28
|
+
app.use(express.json({ limit: "10mb" }));
|
|
29
|
+
const storage = multer.diskStorage({
|
|
30
|
+
destination: (req, file, cb) => {
|
|
31
|
+
cb(null, UPLOAD_DIR);
|
|
32
|
+
},
|
|
33
|
+
filename: (req, file, cb) => {
|
|
34
|
+
const fileId = crypto.randomUUID();
|
|
35
|
+
const ext = file.originalname.split(".").pop() || "";
|
|
36
|
+
cb(null, ext ? `${fileId}.${ext}` : fileId);
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
const upload = multer({
|
|
40
|
+
storage,
|
|
41
|
+
limits: {
|
|
42
|
+
fileSize: 10 * 1024 * 1024, // 10MB
|
|
43
|
+
},
|
|
44
|
+
fileFilter: (req, file, cb) => {
|
|
45
|
+
const ext = file.originalname.split(".").pop()?.toLowerCase();
|
|
46
|
+
// 1. 定义允许的图片类型
|
|
47
|
+
const allowedImageTypes = ["image/jpeg", "image/png", "image/gif", "image/webp", "image/svg+xml"];
|
|
48
|
+
const allowedImageExts = ["jpg", "jpeg", "png", "gif", "webp", "svg"];
|
|
49
|
+
// 2. 分别判断文件大类
|
|
50
|
+
const isImage = allowedImageTypes.includes(file.mimetype) || (ext && allowedImageExts.includes(ext));
|
|
51
|
+
const isMarkdown = ext === "md" || file.mimetype === "text/markdown" || file.mimetype === "text/plain";
|
|
52
|
+
const isCss = ext === "css" || file.mimetype === "text/css";
|
|
53
|
+
const isJson = ext === "json" || file.mimetype === "application/json";
|
|
54
|
+
// 3. 综合放行逻辑
|
|
55
|
+
if (isImage || isMarkdown || isCss || isJson) {
|
|
56
|
+
cb(null, true);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
cb(new AppError("不支持的文件类型,仅支持图片、Markdown、CSS 和 JSON 文件"));
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
// 健康检查
|
|
64
|
+
app.get("/health", (_req, res) => {
|
|
65
|
+
res.json({ status: "ok", service: "wenyan-cli", version: options.version || "unknown" });
|
|
66
|
+
});
|
|
67
|
+
// 鉴权探针
|
|
68
|
+
app.get("/verify", auth, (_req, res) => {
|
|
69
|
+
res.json({ success: true, message: "Authorized" });
|
|
70
|
+
});
|
|
71
|
+
// 发布接口 - 读取 json 文件内容并发布
|
|
72
|
+
app.post("/publish", auth, async (req, res) => {
|
|
73
|
+
const body = req.body;
|
|
74
|
+
validateRequest(body);
|
|
75
|
+
// 根据 fileId 去找刚上传的 json 文件并读取内容
|
|
76
|
+
const files = await fs.readdir(UPLOAD_DIR);
|
|
77
|
+
const matchedFile = files.find((f) => f === body.fileId);
|
|
78
|
+
if (!matchedFile) {
|
|
79
|
+
throw new AppError(`文件不存在或已过期,请重新上传 (ID: ${body.fileId})`);
|
|
80
|
+
}
|
|
81
|
+
// 简单的防呆校验,防止直接提交纯图片的 fileId 到发布接口
|
|
82
|
+
const ext = path.extname(matchedFile).toLowerCase();
|
|
83
|
+
if (ext !== ".json") {
|
|
84
|
+
throw new AppError("请提供 JSON 文件的 fileId,不能直接发布图片文件");
|
|
85
|
+
}
|
|
86
|
+
// 找到上传文件并提取文本内容
|
|
87
|
+
const filePath = path.join(UPLOAD_DIR, matchedFile);
|
|
88
|
+
const fileContent = await fs.readFile(filePath, "utf-8");
|
|
89
|
+
const gzhContent = JSON.parse(fileContent);
|
|
90
|
+
if (!gzhContent.title)
|
|
91
|
+
throw new AppError("未能找到文章标题");
|
|
92
|
+
// 公共的 asset:// 替换逻辑
|
|
93
|
+
const resolveAssetPath = (assetUrl) => {
|
|
94
|
+
const assetFileId = assetUrl.replace("asset://", "");
|
|
95
|
+
const matchedAsset = files.find((f) => f === assetFileId || path.parse(f).name === assetFileId);
|
|
96
|
+
return matchedAsset ? path.join(UPLOAD_DIR, matchedAsset) : assetUrl;
|
|
97
|
+
};
|
|
98
|
+
// 替换 HTML 内容里的 asset://
|
|
99
|
+
gzhContent.content = gzhContent.content.replace(/(<img\b[^>]*?\bsrc\s*=\s*["'])(asset:\/\/[^"']+)(["'])/gi, (_match, prefix, assetUrl, suffix) => prefix + resolveAssetPath(assetUrl) + suffix);
|
|
100
|
+
// 替换封面里的 asset://
|
|
101
|
+
if (gzhContent.cover && gzhContent.cover.startsWith("asset://")) {
|
|
102
|
+
gzhContent.cover = resolveAssetPath(gzhContent.cover);
|
|
103
|
+
}
|
|
104
|
+
const data = await publishToWechatDraft({
|
|
105
|
+
title: gzhContent.title,
|
|
106
|
+
content: gzhContent.content,
|
|
107
|
+
cover: gzhContent.cover,
|
|
108
|
+
author: gzhContent.author,
|
|
109
|
+
source_url: gzhContent.source_url,
|
|
110
|
+
}, {
|
|
111
|
+
appId: body.appId,
|
|
112
|
+
appSecret: body.appSecret
|
|
113
|
+
});
|
|
114
|
+
if (data.media_id) {
|
|
115
|
+
res.json({
|
|
116
|
+
media_id: data.media_id,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
throw new AppError(`发布到微信公众号失败,\n${data}`);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
// 上传接口
|
|
124
|
+
app.post("/upload", auth, upload.single("file"), async (req, res) => {
|
|
125
|
+
if (!req.file) {
|
|
126
|
+
throw new AppError("未找到上传的文件");
|
|
127
|
+
}
|
|
128
|
+
const newFilename = req.file.filename;
|
|
129
|
+
res.json({
|
|
130
|
+
success: true,
|
|
131
|
+
data: {
|
|
132
|
+
fileId: newFilename,
|
|
133
|
+
originalFilename: req.file.originalname,
|
|
134
|
+
mimetype: req.file.mimetype,
|
|
135
|
+
size: req.file.size,
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
app.use(errorHandler);
|
|
140
|
+
return new Promise((resolve, reject) => {
|
|
141
|
+
const server = app.listen(port, () => {
|
|
142
|
+
console.log(`文颜 Server 已启动,监听端口 ${port}`);
|
|
143
|
+
console.log(`健康检查:http://localhost:${port}/health`);
|
|
144
|
+
console.log(`鉴权探针:http://localhost:${port}/verify`);
|
|
145
|
+
console.log(`发布接口:POST http://localhost:${port}/publish`);
|
|
146
|
+
console.log(`上传接口:POST http://localhost:${port}/upload`);
|
|
147
|
+
});
|
|
148
|
+
server.on("error", (err) => {
|
|
149
|
+
if (err.code === "EADDRINUSE") {
|
|
150
|
+
console.error(`端口 ${port} 已被占用`);
|
|
151
|
+
reject(new Error(`端口 ${port} 已被占用`));
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
reject(err);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
process.on("SIGINT", () => {
|
|
158
|
+
console.log("\n正在关闭服务器...");
|
|
159
|
+
server.close(() => {
|
|
160
|
+
console.log("服务器已关闭");
|
|
161
|
+
resolve();
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
process.on("SIGTERM", () => {
|
|
165
|
+
server.close(() => resolve());
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function errorHandler(error, _req, res, next) {
|
|
170
|
+
if (res.headersSent) {
|
|
171
|
+
return next(error);
|
|
172
|
+
}
|
|
173
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
174
|
+
// 修复:multer 抛出的文件限制错误(如超出大小),应判断为客户端 400 错误
|
|
175
|
+
const isAppError = error instanceof AppError;
|
|
176
|
+
const isMulterError = error.name === "MulterError";
|
|
177
|
+
const statusCode = isAppError || isMulterError ? 400 : 500;
|
|
178
|
+
if (statusCode === 500) {
|
|
179
|
+
console.error("[Server Error]:", error);
|
|
180
|
+
}
|
|
181
|
+
res.status(statusCode).json({
|
|
182
|
+
code: -1,
|
|
183
|
+
desc: message,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function createAuthHandler(config) {
|
|
187
|
+
return (req, res, next) => {
|
|
188
|
+
if (!config.apiKey) {
|
|
189
|
+
return next();
|
|
190
|
+
}
|
|
191
|
+
const clientApiKey = req.headers["x-api-key"];
|
|
192
|
+
if (clientApiKey === config.apiKey) {
|
|
193
|
+
next();
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
res.status(401).json({
|
|
197
|
+
code: -1,
|
|
198
|
+
desc: "Unauthorized: Invalid API Key",
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function validateRequest(req) {
|
|
204
|
+
if (!req.fileId) {
|
|
205
|
+
throw new AppError("缺少必要参数:fileId");
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async function cleanupOldUploads() {
|
|
209
|
+
try {
|
|
210
|
+
const files = await fs.readdir(UPLOAD_DIR);
|
|
211
|
+
const now = Date.now();
|
|
212
|
+
for (const file of files) {
|
|
213
|
+
const filePath = path.join(UPLOAD_DIR, file);
|
|
214
|
+
try {
|
|
215
|
+
const stats = await fs.stat(filePath);
|
|
216
|
+
if (now - stats.mtimeMs > UPLOAD_TTL_MS) {
|
|
217
|
+
await fs.unlink(filePath);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
catch (_e) {
|
|
221
|
+
// 忽略单个文件处理错误
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
catch (e) {
|
|
226
|
+
console.error("Cleanup task error:", e);
|
|
227
|
+
}
|
|
228
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function readStdin(): Promise<string>;
|
|
2
|
+
export declare function readStream(stream: NodeJS.ReadableStream): Promise<string>;
|
|
3
|
+
export declare function getInputContent(inputContent?: string, file?: string): Promise<{
|
|
4
|
+
content: string;
|
|
5
|
+
absoluteDirPath: string | undefined;
|
|
6
|
+
}>;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { getNormalizeFilePath } from "@wenyan-md/core/wrapper";
|
|
4
|
+
export function readStdin() {
|
|
5
|
+
return readStream(process.stdin);
|
|
6
|
+
}
|
|
7
|
+
export async function readStream(stream) {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
let data = "";
|
|
10
|
+
stream.setEncoding?.("utf8");
|
|
11
|
+
const onData = (chunk) => (data += chunk);
|
|
12
|
+
const onEnd = () => {
|
|
13
|
+
cleanup();
|
|
14
|
+
resolve(data);
|
|
15
|
+
};
|
|
16
|
+
const onError = (err) => {
|
|
17
|
+
cleanup();
|
|
18
|
+
reject(err);
|
|
19
|
+
};
|
|
20
|
+
const cleanup = () => {
|
|
21
|
+
stream.removeListener("data", onData);
|
|
22
|
+
stream.removeListener("end", onEnd);
|
|
23
|
+
stream.removeListener("error", onError);
|
|
24
|
+
};
|
|
25
|
+
stream.on("data", onData);
|
|
26
|
+
stream.on("end", onEnd);
|
|
27
|
+
stream.on("error", onError);
|
|
28
|
+
stream.resume?.();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export async function getInputContent(inputContent, file) {
|
|
32
|
+
let absoluteDirPath = undefined;
|
|
33
|
+
// 1. 尝试从 Stdin 读取
|
|
34
|
+
if (!inputContent && !process.stdin.isTTY) {
|
|
35
|
+
inputContent = await readStdin();
|
|
36
|
+
}
|
|
37
|
+
// 2. 尝试从文件读取
|
|
38
|
+
if (!inputContent && file) {
|
|
39
|
+
const normalizePath = getNormalizeFilePath(file);
|
|
40
|
+
inputContent = await fs.readFile(normalizePath, "utf-8");
|
|
41
|
+
absoluteDirPath = path.dirname(normalizePath);
|
|
42
|
+
}
|
|
43
|
+
// 3. 校验输入
|
|
44
|
+
if (!inputContent) {
|
|
45
|
+
throw new Error("missing input-content (no argument, no stdin, and no file).");
|
|
46
|
+
}
|
|
47
|
+
return { content: inputContent, absoluteDirPath };
|
|
48
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wenyan-md-cli-evlon",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A CLI tool for Wenyan markdown rendering & publishing",
|
|
5
|
+
"author": "Lei <caol64@gmail.com> (https://github.com/caol64)",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"公众号",
|
|
9
|
+
"markdown",
|
|
10
|
+
"文颜",
|
|
11
|
+
"wenyan",
|
|
12
|
+
"wechat",
|
|
13
|
+
"publishing",
|
|
14
|
+
"renderer"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"bin": {
|
|
18
|
+
"wenyan": "./dist/cli.js"
|
|
19
|
+
},
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/types/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"homepage": "https://github.com/caol64/wenyan-cli#readme",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/caol64/wenyan-cli.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/caol64/wenyan-cli/issues"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc",
|
|
35
|
+
"upgrade:core": "pnpm update @wenyan-md/core",
|
|
36
|
+
"prepublishOnly": "pnpm upgrade:core && pnpm build",
|
|
37
|
+
"test": "node --import tsx --test",
|
|
38
|
+
"test:watch": "node --import tsx --test --watch",
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"lint": "eslint . --fix",
|
|
41
|
+
"test:bin": "pnpm build && node ./dist/cli.js render -f tests/publish.md -c tests/manhua.css --no-mac-style",
|
|
42
|
+
"test:realPublish": "pnpm build && node --env-file=.env.test ./dist/cli.js publish -f tests/publish.md -t phycat",
|
|
43
|
+
"test:serverPublish": "pnpm build && node --env-file=.env.test ./dist/cli.js publish -f tests/publish.md -c tests/manhua.css --no-mac-style --server http://localhost:3000",
|
|
44
|
+
"test:serve": "pnpm build && node --env-file=.env.test ./dist/cli.js serve"
|
|
45
|
+
},
|
|
46
|
+
"packageManager": "pnpm@10.7.1",
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@wenyan-md/core": "npm:evlon-wenyan-md-core@^3.0.3",
|
|
49
|
+
"commander": "^14.0.0",
|
|
50
|
+
"express": "^5.2.1",
|
|
51
|
+
"form-data-encoder": "^4.1.0",
|
|
52
|
+
"formdata-node": "^6.0.3",
|
|
53
|
+
"jsdom": "^27.4.0",
|
|
54
|
+
"multer": "^2.1.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@eslint/js": "^10.0.1",
|
|
58
|
+
"@types/express": "^5.0.6",
|
|
59
|
+
"@types/multer": "^2.0.0",
|
|
60
|
+
"@types/node": "^24.3.0",
|
|
61
|
+
"eslint": "^10.1.0",
|
|
62
|
+
"globals": "^17.4.0",
|
|
63
|
+
"tsx": "^4.21.0",
|
|
64
|
+
"typescript": "^5.9.2",
|
|
65
|
+
"typescript-eslint": "^8.58.0"
|
|
66
|
+
}
|
|
67
|
+
}
|