@ckpack/markdown-wx 1.0.0 → 1.0.1
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 +31 -0
- package/miniprogram_dist/components/markdown-wx/markdown-wx.d.ts +35 -0
- package/miniprogram_dist/components/markdown-wx/markdown-wx.js +112 -0
- package/miniprogram_dist/components/markdown-wx/markdown-wx.json +3 -0
- package/miniprogram_dist/components/markdown-wx/markdown-wx.wxml +3 -0
- package/miniprogram_dist/components/markdown-wx/markdown-wx.wxss +11 -0
- package/miniprogram_dist/index.js +3 -0
- package/{dist/index.js → miniprogram_dist/src-BX9uvc_6.js} +1 -1
- package/package.json +11 -7
- /package/{dist → miniprogram_dist}/index.d.ts +0 -0
- /package/{dist → miniprogram_dist}/server/index.d.mts +0 -0
- /package/{dist → miniprogram_dist}/server/index.mjs +0 -0
package/README.md
CHANGED
|
@@ -120,6 +120,37 @@ const html = await createMarkdownWx({
|
|
|
120
120
|
}).render('$E=mc^2$');
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
+
### 小程序组件 (markdown-wx)
|
|
124
|
+
提供一个微信小程序自定义组件,内部基于 `createMarkdownWx` 渲染。
|
|
125
|
+
|
|
126
|
+
组件属性:
|
|
127
|
+
- src: markdown 字符串
|
|
128
|
+
- html: 直接传入 html (优先于 src)
|
|
129
|
+
- options: MarkdownWxOptions (建议仅使用可序列化字段,如 math.endpoint)
|
|
130
|
+
- env: 透传给 render 的 env
|
|
131
|
+
- autoRender: 是否自动渲染 (默认 true)
|
|
132
|
+
- selectable: rich-text selectable (默认 false)
|
|
133
|
+
|
|
134
|
+
事件:
|
|
135
|
+
- rendered: 渲染成功,detail { html, src }
|
|
136
|
+
- error: 渲染失败,detail { error }
|
|
137
|
+
|
|
138
|
+
示例:
|
|
139
|
+
```xml
|
|
140
|
+
<markdown-wx id="md" src="{{markdown}}" options="{{mdOptions}}" bind:rendered="onRendered" />
|
|
141
|
+
```
|
|
142
|
+
```js
|
|
143
|
+
Page({
|
|
144
|
+
data: {
|
|
145
|
+
markdown: '# Hello $E=mc^2$',
|
|
146
|
+
mdOptions: { math: { endpoint: 'https://example.com/katex' } }
|
|
147
|
+
},
|
|
148
|
+
onRendered(e) {
|
|
149
|
+
console.log('html', e.detail.html);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
123
154
|
## server
|
|
124
155
|
|
|
125
156
|
### renderFormulaToSvg(formula, options?)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region src/components/markdown-wx/markdown-wx.d.ts
|
|
2
|
+
declare const _default: WechatMiniprogram.Component.Identifier<{
|
|
3
|
+
nodes: string;
|
|
4
|
+
error: string;
|
|
5
|
+
rendering: boolean;
|
|
6
|
+
}, {
|
|
7
|
+
src: {
|
|
8
|
+
type: StringConstructor;
|
|
9
|
+
value: string;
|
|
10
|
+
};
|
|
11
|
+
html: {
|
|
12
|
+
type: StringConstructor;
|
|
13
|
+
value: string;
|
|
14
|
+
};
|
|
15
|
+
options: {
|
|
16
|
+
type: ObjectConstructor;
|
|
17
|
+
value: null;
|
|
18
|
+
};
|
|
19
|
+
env: {
|
|
20
|
+
type: ObjectConstructor;
|
|
21
|
+
value: null;
|
|
22
|
+
};
|
|
23
|
+
autoRender: {
|
|
24
|
+
type: BooleanConstructor;
|
|
25
|
+
value: boolean;
|
|
26
|
+
};
|
|
27
|
+
selectable: {
|
|
28
|
+
type: BooleanConstructor;
|
|
29
|
+
value: boolean;
|
|
30
|
+
};
|
|
31
|
+
}, {
|
|
32
|
+
render(): Promise<void>;
|
|
33
|
+
}>;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { _default as default };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { r as createWxRequestFetchLike, t as createMarkdownWx } from "../../src-BX9uvc_6.js";
|
|
2
|
+
|
|
3
|
+
//#region src/components/markdown-wx/markdown-wx.ts
|
|
4
|
+
function isNonEmptyString(value) {
|
|
5
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
6
|
+
}
|
|
7
|
+
function normalizeOptions(options) {
|
|
8
|
+
if (!options) return {};
|
|
9
|
+
if (!options.math || options.math.fetch) return options;
|
|
10
|
+
return {
|
|
11
|
+
...options,
|
|
12
|
+
math: {
|
|
13
|
+
...options.math,
|
|
14
|
+
fetch: createWxRequestFetchLike()
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
var markdown_wx_default = Component({
|
|
19
|
+
properties: {
|
|
20
|
+
src: {
|
|
21
|
+
type: String,
|
|
22
|
+
value: ""
|
|
23
|
+
},
|
|
24
|
+
html: {
|
|
25
|
+
type: String,
|
|
26
|
+
value: ""
|
|
27
|
+
},
|
|
28
|
+
options: {
|
|
29
|
+
type: Object,
|
|
30
|
+
value: null
|
|
31
|
+
},
|
|
32
|
+
env: {
|
|
33
|
+
type: Object,
|
|
34
|
+
value: null
|
|
35
|
+
},
|
|
36
|
+
autoRender: {
|
|
37
|
+
type: Boolean,
|
|
38
|
+
value: true
|
|
39
|
+
},
|
|
40
|
+
selectable: {
|
|
41
|
+
type: Boolean,
|
|
42
|
+
value: false
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
data: {
|
|
46
|
+
nodes: "",
|
|
47
|
+
error: "",
|
|
48
|
+
rendering: false
|
|
49
|
+
},
|
|
50
|
+
observers: { "src, options, env, html, autoRender": function() {
|
|
51
|
+
if (this.properties.autoRender) this.render();
|
|
52
|
+
} },
|
|
53
|
+
methods: { async render() {
|
|
54
|
+
const instance = this;
|
|
55
|
+
const token = (instance._renderToken ?? 0) + 1;
|
|
56
|
+
instance._renderToken = token;
|
|
57
|
+
const { src, html, options, env } = this.properties;
|
|
58
|
+
this.setData({
|
|
59
|
+
rendering: true,
|
|
60
|
+
error: ""
|
|
61
|
+
});
|
|
62
|
+
try {
|
|
63
|
+
if (isNonEmptyString(html)) {
|
|
64
|
+
if (instance._renderToken === token) {
|
|
65
|
+
this.setData({
|
|
66
|
+
nodes: html,
|
|
67
|
+
rendering: false
|
|
68
|
+
});
|
|
69
|
+
this.triggerEvent("rendered", {
|
|
70
|
+
html,
|
|
71
|
+
src
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (!isNonEmptyString(src)) {
|
|
77
|
+
if (instance._renderToken === token) {
|
|
78
|
+
this.setData({
|
|
79
|
+
nodes: "",
|
|
80
|
+
rendering: false
|
|
81
|
+
});
|
|
82
|
+
this.triggerEvent("rendered", {
|
|
83
|
+
html: "",
|
|
84
|
+
src
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const rendered = await createMarkdownWx(normalizeOptions(options)).render(src, env ?? void 0);
|
|
90
|
+
if (instance._renderToken !== token) return;
|
|
91
|
+
this.setData({
|
|
92
|
+
nodes: rendered,
|
|
93
|
+
rendering: false
|
|
94
|
+
});
|
|
95
|
+
this.triggerEvent("rendered", {
|
|
96
|
+
html: rendered,
|
|
97
|
+
src
|
|
98
|
+
});
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (instance._renderToken !== token) return;
|
|
101
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
102
|
+
this.setData({
|
|
103
|
+
error: message,
|
|
104
|
+
rendering: false
|
|
105
|
+
});
|
|
106
|
+
this.triggerEvent("error", { error: message });
|
|
107
|
+
}
|
|
108
|
+
} }
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
//#endregion
|
|
112
|
+
export { markdown_wx_default as default };
|
|
@@ -212,4 +212,4 @@ async function renderMarkdownToWx(src, options = {}) {
|
|
|
212
212
|
}
|
|
213
213
|
|
|
214
214
|
//#endregion
|
|
215
|
-
export {
|
|
215
|
+
export { mathPlugin as i, renderMarkdownToWx as n, createWxRequestFetchLike as r, createMarkdownWx as t };
|
package/package.json
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckpack/markdown-wx",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"private": false,
|
|
6
6
|
"packageManager": "pnpm@10.12.1+sha512.f0dda8580f0ee9481c5c79a1d927b9164f2c478e90992ad268bbb2465a736984391d6333d2c327913578b2804af33474ca554ba29c04a8b13060a717675ae3ac",
|
|
7
7
|
"description": "将 Markdown 转换为微信小程序富文本格式.",
|
|
8
|
+
"miniprogram": "miniprogram_dist",
|
|
8
9
|
"author": "ckvv",
|
|
9
10
|
"license": "MIT",
|
|
10
11
|
"repository": "https://github.com/ckpack/markdown-wx",
|
|
11
12
|
"exports": {
|
|
12
|
-
".": "./
|
|
13
|
-
"./server": "./
|
|
13
|
+
".": "./miniprogram_dist/index.js",
|
|
14
|
+
"./server": "./miniprogram_dist/server/index.mjs",
|
|
15
|
+
"./components/*": "./miniprogram_dist/components/*",
|
|
14
16
|
"./package.json": "./package.json"
|
|
15
17
|
},
|
|
16
|
-
"main": "./
|
|
17
|
-
"module": "./
|
|
18
|
-
"types": "./
|
|
18
|
+
"main": "./miniprogram_dist/index.js",
|
|
19
|
+
"module": "./miniprogram_dist/index.js",
|
|
20
|
+
"types": "./miniprogram_dist/index.d.ts",
|
|
19
21
|
"files": [
|
|
20
|
-
"dist"
|
|
22
|
+
"dist",
|
|
23
|
+
"miniprogram_dist"
|
|
21
24
|
],
|
|
22
25
|
"scripts": {
|
|
23
26
|
"dev": "node --import=tsx src/index.ts",
|
|
@@ -36,6 +39,7 @@
|
|
|
36
39
|
"@types/node": "^25.0.10",
|
|
37
40
|
"eslint": "^9.39.2",
|
|
38
41
|
"lint-staged": "^16.2.7",
|
|
42
|
+
"miniprogram-api-typings": "^5.0.0",
|
|
39
43
|
"simple-git-hooks": "^2.13.1",
|
|
40
44
|
"tsdown": "^0.20.1",
|
|
41
45
|
"typescript": "^5.9.3",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|