@8btc/mditor 0.0.26 → 0.0.28

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.
@@ -1,73 +0,0 @@
1
- /**
2
- * 自定义组件渲染模块
3
- * 支持将 HTML 标签(如 <read-button label="文献解读">)转换为自定义 HTML
4
- */
5
- /**
6
- * 解析 HTML 属性字符串
7
- * @param str 属性字符串,如 'label="文献解读" id="test"'
8
- * @returns 属性对象
9
- */
10
- export declare function parseAttributes(str: string): Record<string, string>;
11
- /**
12
- * 从 HTML 字符串中提取自定义标签
13
- * @param html HTML 字符串
14
- * @param tagName 标签名称,如 'read-button'
15
- * @returns 匹配的标签信息数组
16
- */
17
- export declare function extractCustomTags(html: string, tagName: string): Array<{
18
- fullTag: string;
19
- attributes: Record<string, string>;
20
- content: string;
21
- }>;
22
- /**
23
- * 自定义组件配置类型
24
- */
25
- export interface ICustomComponent {
26
- /**
27
- * HTML 渲染函数 - 用于生成 HTML 字符串
28
- * @param props 组件属性对象
29
- * @returns HTML 字符串
30
- */
31
- renderHTML: (props: Record<string, any>) => string;
32
- /**
33
- * 组件显示名称
34
- */
35
- displayName?: string;
36
- }
37
- /**
38
- * 自定义组件集合
39
- */
40
- export type ICustomComponents = Record<string, ICustomComponent>;
41
- /**
42
- * 使用 HTML 字符串渲染自定义标签
43
- * @param html HTML 字符串
44
- * @param tagName 标签名称
45
- * @param component 自定义组件配置
46
- * @returns 修改后的 HTML 字符串
47
- */
48
- export declare function renderCustomComponentHTML(html: string, tagName: string, component: ICustomComponent): string;
49
- /**
50
- * 在 DOM 元素中查找并渲染自定义组件
51
- * @param element DOM 元素
52
- * @param customComponents 自定义组件集合
53
- */
54
- export declare function renderCustomComponents(element: HTMLElement, customComponents: ICustomComponents): void;
55
- /**
56
- * 批量处理自定义组件 - 用于预处理 HTML 字符串
57
- * @param html HTML 字符串
58
- * @param customComponents 自定义组件集合
59
- * @returns 处理后的 HTML 字符串
60
- */
61
- export declare function processCustomComponents(html: string, customComponents: ICustomComponents): string;
62
- /**
63
- * 处理 markdown 中的自定义组件
64
- * 将 markdown 中的自定义标签转换为对应的 HTML
65
- * @param markdown markdown 字符串
66
- * @param customComponents 自定义组件集合
67
- * @returns 处理后的 markdown 字符串
68
- */
69
- export declare function processMarkdownCustomComponents(markdown: string, customComponents: ICustomComponents): string;
70
- /**
71
- * 导出默认函数和其他公共 API
72
- */
73
- export default function customRender(element: HTMLElement, customComponents: ICustomComponents): void;
@@ -1,203 +0,0 @@
1
- /**
2
- * 自定义组件渲染模块
3
- * 支持将 HTML 标签(如 <read-button label="文献解读">)转换为自定义 HTML
4
- */
5
-
6
- /**
7
- * 解析 HTML 属性字符串
8
- * @param str 属性字符串,如 'label="文献解读" id="test"'
9
- * @returns 属性对象
10
- */
11
- export function parseAttributes(str: string): Record<string, string> {
12
- const attributes: Record<string, string> = {};
13
- if (!str) return attributes;
14
-
15
- const attrRegex = /(\w+)="([^"]*)"/g;
16
- let match;
17
- while ((match = attrRegex.exec(str)) !== null) {
18
- attributes[match[1]] = match[2];
19
- }
20
- return attributes;
21
- }
22
-
23
- /**
24
- * 从 HTML 字符串中提取自定义标签
25
- * @param html HTML 字符串
26
- * @param tagName 标签名称,如 'read-button'
27
- * @returns 匹配的标签信息数组
28
- */
29
- export function extractCustomTags(
30
- html: string,
31
- tagName: string
32
- ): Array<{
33
- fullTag: string;
34
- attributes: Record<string, string>;
35
- content: string;
36
- }> {
37
- const results: Array<{
38
- fullTag: string;
39
- attributes: Record<string, string>;
40
- content: string;
41
- }> = [];
42
-
43
- // 匹配自闭合标签和非自闭合标签
44
- const selfClosingRegex = new RegExp(`<${tagName}\\s+([^>]*)\\s*/>`, "g");
45
- const openCloseRegex = new RegExp(
46
- `<${tagName}\\s+([^>]*)>([^<]*)</${tagName}>`,
47
- "g"
48
- );
49
-
50
- let match;
51
-
52
- // 处理自闭合标签
53
- while ((match = selfClosingRegex.exec(html)) !== null) {
54
- results.push({
55
- fullTag: match[0],
56
- attributes: parseAttributes(match[1]),
57
- content: "",
58
- });
59
- }
60
-
61
- // 处理开闭合标签
62
- while ((match = openCloseRegex.exec(html)) !== null) {
63
- results.push({
64
- fullTag: match[0],
65
- attributes: parseAttributes(match[1]),
66
- content: match[2],
67
- });
68
- }
69
-
70
- return results;
71
- }
72
-
73
- /**
74
- * 自定义组件配置类型
75
- */
76
- export interface ICustomComponent {
77
- /**
78
- * HTML 渲染函数 - 用于生成 HTML 字符串
79
- * @param props 组件属性对象
80
- * @returns HTML 字符串
81
- */
82
- renderHTML: (props: Record<string, any>) => string;
83
-
84
- /**
85
- * 组件显示名称
86
- */
87
- displayName?: string;
88
- }
89
-
90
- /**
91
- * 自定义组件集合
92
- */
93
- export type ICustomComponents = Record<string, ICustomComponent>;
94
-
95
- /**
96
- * 使用 HTML 字符串渲染自定义标签
97
- * @param html HTML 字符串
98
- * @param tagName 标签名称
99
- * @param component 自定义组件配置
100
- * @returns 修改后的 HTML 字符串
101
- */
102
- export function renderCustomComponentHTML(
103
- html: string,
104
- tagName: string,
105
- component: ICustomComponent
106
- ): string {
107
- const tags = extractCustomTags(html, tagName);
108
- let result = html;
109
-
110
- // 反向遍历以保持位置正确性
111
- for (let i = tags.length - 1; i >= 0; i--) {
112
- const tag = tags[i];
113
- const htmlString = component.renderHTML(tag.attributes);
114
- result = result.replace(tag.fullTag, htmlString);
115
- }
116
-
117
- return result;
118
- }
119
-
120
- /**
121
- * 在 DOM 元素中查找并渲染自定义组件
122
- * @param element DOM 元素
123
- * @param customComponents 自定义组件集合
124
- */
125
- export function renderCustomComponents(
126
- element: HTMLElement,
127
- customComponents: ICustomComponents
128
- ): void {
129
- Object.keys(customComponents).forEach((tagName) => {
130
- const component = customComponents[tagName];
131
- const customElements = element.querySelectorAll(tagName);
132
- console.log(element, customComponents, "customComponents");
133
- // 反向遍历以避免 DOM 修改时的索引问题
134
- Array.from(customElements)
135
- .reverse()
136
- .forEach((el) => {
137
- const attributes: Record<string, string> = {};
138
- el.getAttributeNames().forEach((name) => {
139
- attributes[name] = el.getAttribute(name) || "";
140
- });
141
- // 使用 innerHTML 保留内部 HTML 内容,而不仅仅是纯文本
142
- const content = el.innerHTML || "";
143
-
144
- // 使用 HTML 渲染
145
- const htmlString = component.renderHTML({
146
- ...attributes,
147
- children: content,
148
- });
149
- el.outerHTML = htmlString;
150
- });
151
- });
152
- }
153
-
154
- /**
155
- * 批量处理自定义组件 - 用于预处理 HTML 字符串
156
- * @param html HTML 字符串
157
- * @param customComponents 自定义组件集合
158
- * @returns 处理后的 HTML 字符串
159
- */
160
- export function processCustomComponents(
161
- html: string,
162
- customComponents: ICustomComponents
163
- ): string {
164
- let result = html;
165
-
166
- Object.keys(customComponents).forEach((tagName) => {
167
- const component = customComponents[tagName];
168
- result = renderCustomComponentHTML(result, tagName, component);
169
- });
170
-
171
- return result;
172
- }
173
-
174
- /**
175
- * 处理 markdown 中的自定义组件
176
- * 将 markdown 中的自定义标签转换为对应的 HTML
177
- * @param markdown markdown 字符串
178
- * @param customComponents 自定义组件集合
179
- * @returns 处理后的 markdown 字符串
180
- */
181
- export function processMarkdownCustomComponents(
182
- markdown: string,
183
- customComponents: ICustomComponents
184
- ): string {
185
- let result = markdown;
186
-
187
- Object.keys(customComponents).forEach((tagName) => {
188
- const component = customComponents[tagName];
189
- result = renderCustomComponentHTML(result, tagName, component);
190
- });
191
-
192
- return result;
193
- }
194
-
195
- /**
196
- * 导出默认函数和其他公共 API
197
- */
198
- export default function customRender(
199
- element: HTMLElement,
200
- customComponents: ICustomComponents
201
- ): void {
202
- renderCustomComponents(element, customComponents);
203
- }