@8btc/mditor 0.0.25 → 0.0.26
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/dist/index.css +2 -2
- package/dist/index.js +152 -6
- package/dist/index.min.js +1 -1
- package/dist/js/lute/lute.min.js +29 -1
- package/dist/method.js +148 -3
- package/dist/method.min.js +1 -1
- package/dist/ts/markdown/customRender.d.ts +73 -0
- package/dist/types/index.d.ts +16 -0
- package/package.json +1 -1
- package/src/ts/markdown/customRender.ts +203 -0
- package/src/ts/markdown/previewRender.ts +5 -0
- package/src/ts/preview/index.ts +3 -1
- package/src/ts/util/Options.ts +1 -0
|
@@ -0,0 +1,203 @@
|
|
|
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
|
+
}
|
|
@@ -25,6 +25,7 @@ import { setLute } from "./setLute";
|
|
|
25
25
|
import { speechRender } from "./speechRender";
|
|
26
26
|
import { selectionRender } from "./selectionRender";
|
|
27
27
|
import { bindMathSelectionListener } from "../util/mathSelection";
|
|
28
|
+
import customRender from "./customRender";
|
|
28
29
|
|
|
29
30
|
const mergeOptions = (options?: IPreviewOptions) => {
|
|
30
31
|
const defaultOption: IPreviewOptions = {
|
|
@@ -173,6 +174,10 @@ export const previewRender = async (
|
|
|
173
174
|
);
|
|
174
175
|
}
|
|
175
176
|
|
|
177
|
+
console.log(previewElement, "previewElement");
|
|
178
|
+
|
|
179
|
+
customRender(previewElement, mergedOptions.customComponents || {});
|
|
180
|
+
|
|
176
181
|
setContentTheme(mergedOptions.theme.current, mergedOptions.theme.path);
|
|
177
182
|
if (mergedOptions.anchor === 1) {
|
|
178
183
|
previewElement.classList.add("vditor-reset--anchor");
|
package/src/ts/preview/index.ts
CHANGED
|
@@ -102,7 +102,9 @@ export class Preview {
|
|
|
102
102
|
this.element.appendChild(this.previewElement);
|
|
103
103
|
|
|
104
104
|
// 绑定数学公式选中高亮监听
|
|
105
|
-
this.mathSelectionCleanup = bindMathSelectionListener(
|
|
105
|
+
this.mathSelectionCleanup = bindMathSelectionListener(
|
|
106
|
+
this.previewElement
|
|
107
|
+
);
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
public unbindListener() {
|