@rbbtsn0w/wechat-markdown 1.1.1 → 1.3.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/README.md +12 -1
- package/dist/media/mermaid.d.ts +11 -1
- package/dist/media/mermaid.d.ts.map +1 -1
- package/dist/media/mermaid.js +58 -9
- package/dist/media/mermaid.js.map +1 -1
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -18,13 +18,24 @@ An extensible, headless TypeScript SDK designed to convert Markdown into pixel-p
|
|
|
18
18
|
|
|
19
19
|
## 📦 Installation
|
|
20
20
|
|
|
21
|
+
```bash
|
|
22
|
+
# Via npm
|
|
23
|
+
npm install @rbbtsn0w/wechat-markdown
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
For GitHub Packages, add the scope-specific registry to `.npmrc` before installing:
|
|
27
|
+
|
|
28
|
+
```ini
|
|
29
|
+
@rbbtsn0w:registry=https://npm.pkg.github.com
|
|
30
|
+
```
|
|
31
|
+
|
|
21
32
|
```bash
|
|
22
33
|
npm install @rbbtsn0w/wechat-markdown
|
|
23
34
|
```
|
|
24
35
|
|
|
25
36
|
## Releasing
|
|
26
37
|
|
|
27
|
-
Releases from `main` publish to npm using trusted publishing
|
|
38
|
+
Releases from `main` publish to npm using trusted publishing and GitHub Packages using `GITHUB_TOKEN`. A package that has never been published to npm must be bootstrapped manually because it has no npm settings page yet:
|
|
28
39
|
|
|
29
40
|
```bash
|
|
30
41
|
npm login
|
package/dist/media/mermaid.d.ts
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
+
export interface MermaidRendererOptions {
|
|
2
|
+
timeoutMs?: number;
|
|
3
|
+
maxRetries?: number;
|
|
4
|
+
}
|
|
1
5
|
export declare class MermaidRenderer {
|
|
2
6
|
private tempDir;
|
|
3
|
-
|
|
7
|
+
private timeoutMs;
|
|
8
|
+
private maxRetries;
|
|
9
|
+
constructor(tempDir?: string, options?: MermaidRendererOptions);
|
|
4
10
|
getHash(mermaidCode: string): string;
|
|
5
11
|
getTargetPath(hash: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Preprocesses mermaid code to quote unquoted node labels with special characters like &, ?, (, )
|
|
14
|
+
*/
|
|
15
|
+
sanitizeMermaid(code: string): string;
|
|
6
16
|
renderToImage(mermaidCode: string): Promise<string>;
|
|
7
17
|
}
|
|
8
18
|
//# sourceMappingURL=mermaid.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mermaid.d.ts","sourceRoot":"","sources":["../../src/media/mermaid.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mermaid.d.ts","sourceRoot":"","sources":["../../src/media/mermaid.ts"],"names":[],"mappings":"AAQA,MAAM,WAAW,sBAAsB;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,eAAe;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,UAAU,CAAS;gBAEf,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,sBAAsB;IAS9D,OAAO,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM;IAKpC,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAInC;;OAEG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAM/B,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CA2D1D"}
|
package/dist/media/mermaid.js
CHANGED
|
@@ -10,35 +10,84 @@ const os_1 = __importDefault(require("os"));
|
|
|
10
10
|
const crypto_1 = __importDefault(require("crypto"));
|
|
11
11
|
const axios_1 = __importDefault(require("axios"));
|
|
12
12
|
const pako_1 = __importDefault(require("pako"));
|
|
13
|
+
const sharp_1 = __importDefault(require("sharp"));
|
|
13
14
|
class MermaidRenderer {
|
|
14
15
|
tempDir;
|
|
15
|
-
|
|
16
|
+
timeoutMs;
|
|
17
|
+
maxRetries;
|
|
18
|
+
constructor(tempDir, options) {
|
|
16
19
|
this.tempDir = tempDir || path_1.default.join(os_1.default.tmpdir(), 'wechat-markdown-mermaid');
|
|
20
|
+
this.timeoutMs = options?.timeoutMs ?? 30000;
|
|
21
|
+
this.maxRetries = options?.maxRetries ?? 3;
|
|
17
22
|
if (!fs_1.default.existsSync(this.tempDir)) {
|
|
18
23
|
fs_1.default.mkdirSync(this.tempDir, { recursive: true });
|
|
19
24
|
}
|
|
20
25
|
}
|
|
21
26
|
getHash(mermaidCode) {
|
|
22
|
-
|
|
27
|
+
const sanitized = this.sanitizeMermaid(mermaidCode.trim());
|
|
28
|
+
return crypto_1.default.createHash('md5').update(sanitized).digest('hex');
|
|
23
29
|
}
|
|
24
30
|
getTargetPath(hash) {
|
|
25
31
|
return path_1.default.join(this.tempDir, `${hash}.png`);
|
|
26
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Preprocesses mermaid code to quote unquoted node labels with special characters like &, ?, (, )
|
|
35
|
+
*/
|
|
36
|
+
sanitizeMermaid(code) {
|
|
37
|
+
return code
|
|
38
|
+
.replace(/\[([^\]"'\n]+[&?()][^\]"'\n]*)\]/g, '["$1"]')
|
|
39
|
+
.replace(/\{([^}"'\n]+[&?()][^}"'\n]*)\}/g, '{"$1"}');
|
|
40
|
+
}
|
|
27
41
|
async renderToImage(mermaidCode) {
|
|
28
|
-
const
|
|
42
|
+
const sanitized = this.sanitizeMermaid(mermaidCode.trim());
|
|
43
|
+
const hash = this.getHash(sanitized);
|
|
29
44
|
const targetPath = this.getTargetPath(hash);
|
|
30
45
|
if (fs_1.default.existsSync(targetPath)) {
|
|
31
46
|
return targetPath;
|
|
32
47
|
}
|
|
33
|
-
const data = Buffer.from(
|
|
48
|
+
const data = Buffer.from(sanitized, 'utf8');
|
|
34
49
|
const compressed = pako_1.default.deflate(data, { level: 9 });
|
|
35
|
-
const result = Buffer.from(compressed)
|
|
50
|
+
const result = Buffer.from(compressed)
|
|
51
|
+
.toString('base64')
|
|
36
52
|
.replace(/\+/g, '-')
|
|
37
53
|
.replace(/\//g, '_');
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
54
|
+
const urls = [
|
|
55
|
+
`https://kroki.io/mermaid/png/${result}`,
|
|
56
|
+
`https://kroki.io/mermaid/svg/${result}`,
|
|
57
|
+
];
|
|
58
|
+
let lastError = null;
|
|
59
|
+
for (const url of urls) {
|
|
60
|
+
for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
|
|
61
|
+
try {
|
|
62
|
+
const isSvg = url.includes('/svg/');
|
|
63
|
+
const response = await axios_1.default.get(url, {
|
|
64
|
+
responseType: isSvg ? 'text' : 'arraybuffer',
|
|
65
|
+
timeout: this.timeoutMs,
|
|
66
|
+
});
|
|
67
|
+
if (isSvg) {
|
|
68
|
+
const svgBuffer = Buffer.from(response.data, 'utf8');
|
|
69
|
+
await (0, sharp_1.default)(svgBuffer).png({ quality: 95 }).toFile(targetPath);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
fs_1.default.writeFileSync(targetPath, Buffer.from(response.data));
|
|
73
|
+
}
|
|
74
|
+
return targetPath;
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
lastError = err;
|
|
78
|
+
const status = err?.response?.status;
|
|
79
|
+
// Fail fast on client errors (4xx) other than rate limiting (429)
|
|
80
|
+
if (status && status >= 400 && status < 500 && status !== 429) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
if (attempt < this.maxRetries) {
|
|
84
|
+
const backoffMs = Math.min(1000 * Math.pow(2, attempt - 1), 8000);
|
|
85
|
+
await new Promise((res) => setTimeout(res, backoffMs));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
throw new Error(`Failed to render Mermaid diagram after retries: ${lastError?.message || 'Unknown error'}`);
|
|
42
91
|
}
|
|
43
92
|
}
|
|
44
93
|
exports.MermaidRenderer = MermaidRenderer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mermaid.js","sourceRoot":"","sources":["../../src/media/mermaid.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,4CAAoB;AACpB,oDAA4B;AAC5B,kDAA0B;AAC1B,gDAAwB;
|
|
1
|
+
{"version":3,"file":"mermaid.js","sourceRoot":"","sources":["../../src/media/mermaid.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,4CAAoB;AACpB,oDAA4B;AAC5B,kDAA0B;AAC1B,gDAAwB;AACxB,kDAA0B;AAO1B,MAAa,eAAe;IAClB,OAAO,CAAS;IAChB,SAAS,CAAS;IAClB,UAAU,CAAS;IAE3B,YAAY,OAAgB,EAAE,OAAgC;QAC5D,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,cAAI,CAAC,IAAI,CAAC,YAAE,CAAC,MAAM,EAAE,EAAE,yBAAyB,CAAC,CAAC;QAC5E,IAAI,CAAC,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,KAAK,CAAC;QAC7C,IAAI,CAAC,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,CAAC,CAAC;QAC3C,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;YACjC,YAAE,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;IAED,OAAO,CAAC,WAAmB;QACzB,MAAM,SAAS,GAAG,IAAI,CAAC,eAAe,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC;QAC3D,OAAO,gBAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;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CAAC,EAAE,IAAI,CAAC,CAAC;wBAClE,MAAM,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC;oBACzD,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CACb,mDAAmD,SAAS,EAAE,OAAO,IAAI,eAAe,EAAE,CAC3F,CAAC;IACJ,CAAC;CACF;AA3FD,0CA2FC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rbbtsn0w/wechat-markdown",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "High-fidelity Markdown to WeChat Official Account HTML converter SDK & middleware",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,10 +24,12 @@
|
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"build": "tsc",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
27
28
|
"test": "vitest run",
|
|
28
29
|
"test:watch": "vitest",
|
|
29
30
|
"lint": "eslint 'src/**/*.{ts,js}'",
|
|
30
|
-
"format": "prettier --write 'src/**/*.{ts,js}'"
|
|
31
|
+
"format": "prettier --write 'src/**/*.{ts,js}'",
|
|
32
|
+
"audit:prod": "npm audit --omit=dev --audit-level=high"
|
|
31
33
|
},
|
|
32
34
|
"keywords": [
|
|
33
35
|
"markdown",
|
|
@@ -50,11 +52,12 @@
|
|
|
50
52
|
"mathjax-full": "^3.2.2",
|
|
51
53
|
"pako": "^2.1.0",
|
|
52
54
|
"sanitize-html": "^2.17.0",
|
|
53
|
-
"sharp": "^0.
|
|
55
|
+
"sharp": "^0.35.4",
|
|
54
56
|
"yaml": "^2.8.2"
|
|
55
57
|
},
|
|
56
58
|
"devDependencies": {
|
|
57
59
|
"@semantic-release/changelog": "^6.0.3",
|
|
60
|
+
"@semantic-release/exec": "^7.1.0",
|
|
58
61
|
"@semantic-release/git": "^10.0.1",
|
|
59
62
|
"@semantic-release/github": "^12.0.0",
|
|
60
63
|
"@semantic-release/npm": "^13.1.5",
|