File tree Expand file tree Collapse file tree 5 files changed +134
-9
lines changed
Expand file tree Collapse file tree 5 files changed +134
-9
lines changed Original file line number Diff line number Diff line change 1818 registry-url : https://registry.npmjs.org/
1919 - run : npm i pnpm@latest -g
2020 - run : pnpm install
21+ - run : pnpm run test
2122 - run : pnpm run build
2223 - run : pnpm publish --no-git-checks
2324 env :
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ pnpm-debug.log*
88lerna-debug.log *
99
1010node_modules
11+ coverage
1112dist
1213dist-ssr
1314* .local
Original file line number Diff line number Diff line change 11# Anby
2- ![ license] ( https://img.shields.io/npm/l/miniprogram-unicode-segmenter )
3- ![ downloads] ( https://img.shields.io/npm/dt/miniprogram-unicode-segmenter )
4- ![ npm] ( https://img.shields.io/npm/v/miniprogram-unicode-segmenter )
2+ ![ license] ( https://img.shields.io/npm/l/anby )
3+ ![ downloads] ( https://img.shields.io/npm/dt/anby )
4+ ![ npm] ( https://img.shields.io/npm/v/anby )
5+
6+ 安装 ` pnpm install anby `
57
68## parseHTML
79
8- 基于状态机实现的 html 解析器
10+ 基于状态机实现的 html 宽松解析器,转换为 js 对象,方便做富文本渲染。
11+
12+ ``` javascript
13+ import { parseHTML } from ' anby'
14+
15+ parseHTML (' <p>123</p>' ).doc
16+
17+ // result
18+ {
19+ type: ' doc' ,
20+ content: [{
21+ type: ' p' ,
22+ content: [{
23+ type: ' text' ,
24+ text: ' 123'
25+ }]
26+ }]
27+ }
28+ ```
29+
30+ ### 配置项
31+
32+ #### alias
33+ 标签别名,用于更改 tag 名称
34+
35+ ``` js
36+ parseHTML (' <p>123</p>' , {
37+ alias: {
38+ p: ' paragraph'
39+ }
40+ }).doc
41+
42+ // result
43+ {
44+ type: ' doc' ,
45+ content: [{
46+ type: ' paragraph' ,
47+ content: [{
48+ type: ' text' ,
49+ text: ' 123'
50+ }]
51+ }]
52+ }
53+ ```
54+
55+ 也可以根据父子关系修改,如 ` 'audio > p' `
56+
57+ ``` js
58+ parseHTML (' <audio><p>123</p></audio>' , {
59+ alias: {
60+ ' audio > p' : ' audioText'
61+ }
62+ }).doc
63+
64+ // result
65+ {
66+ type: ' doc' ,
67+ content: [{
68+ type: ' audio' ,
69+ content: [{
70+ type: ' audioText' ,
71+ content: [{
72+ type: ' text' ,
73+ text: ' 123'
74+ }]
75+ }]
76+ }]
77+ }
78+ ```
79+
80+ #### markRule
81+ 节点解析为编辑器 marks。类似 tiptap 的 mark 规则,将包裹修饰的标签,转换为 marks 属性
82+
83+ ``` js
84+ parseHTML (' <p><strong>加粗</strong></p>' , {
85+ markRule: [{
86+ type: ' strong' ,
87+ mark: {type: ' bold' }
88+ }]
89+ }).doc
90+
91+ // result
92+ {
93+ type: ' doc' ,
94+ content: [{
95+ type: ' p' ,
96+ content: [{
97+ type: ' text' ,
98+ marks: [
99+ {type: ' bold' }
100+ ],
101+ text: ' 加粗'
102+ }]
103+ }]
104+ }
105+ ```
106+
107+ #### selfClose
108+ 自定义自闭合标签
109+
110+ 常见的自闭合标签库已处理,可以额外指定自闭合标签
111+
112+ 注意:如果标签自带反斜杠,则无需配置,例如 ` <note /> `
113+
114+ ``` js
115+ parseHTML (' <p><note>123</p>' , {
116+ selfClose: [' note' ]
117+ }).doc
118+
119+ {
120+ type: ' doc' ,
121+ content: [{
122+ type: ' p' ,
123+ content: [{
124+ type: ' note' ,
125+ }, {
126+ type: ' text' ,
127+ text: ' 123'
128+ }]
129+ }]
130+ }
131+ ```
Original file line number Diff line number Diff line change 1313 "changelog" : " conventional-changelog -i CHANGELOG.md -s" ,
1414 "lint" : " tsc --noEmit" ,
1515 "build" : " tsc && vite build" ,
16- "test" : " vitest" ,
16+ "test" : " vitest run " ,
1717 "coverage" : " vitest run --coverage"
1818 },
1919 "keywords" : [],
Original file line number Diff line number Diff line change @@ -41,12 +41,12 @@ interface Data {
4141}
4242
4343export interface ParseHTMLConfig {
44- /** 标签别名 */
44+ /** 标签别名,用于更改 tag 名称,也可以根据父子关系修改,如 'audio > p' */
4545 alias ?: Record < string , string >
46- /** 自闭合标签 */
47- selfClose ?: string [ ]
48- /** 节点解析为编辑器 marks */
46+ /** 节点解析为编辑器 marks。类似 tiptap 的 mark 规则,将包裹修饰的标签,转换为 marks 属性 */
4947 markRule ?: MarkRule [ ]
48+ /** 自定义自闭合标签(注意:如果标签自带反斜杠,则无需配置,例如 `<note />`)*/
49+ selfClose ?: string [ ]
5050}
5151
5252
You can’t perform that action at this time.
0 commit comments