x-star-editor 0.2.3 → 0.2.5
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/components/Toolbar.d.ts +3 -3
- package/dist/components/Toolbar.js +91 -91
- package/dist/icons/EditOnly.d.ts +4 -0
- package/dist/icons/EditOnly.js +15 -0
- package/dist/icons/Help.d.ts +4 -0
- package/dist/icons/Help.js +26 -0
- package/dist/icons/Mermaid.d.ts +4 -0
- package/dist/icons/Mermaid.js +15 -0
- package/dist/icons/TaskList.d.ts +4 -0
- package/dist/icons/TaskList.js +24 -0
- package/dist/icons/ViewOnly.d.ts +4 -0
- package/dist/icons/ViewOnly.js +15 -0
- package/dist/index.d.ts +2 -1
- package/dist/locales/en_US.d.ts +5 -0
- package/dist/locales/en_US.js +6 -1
- package/dist/locales/index.d.ts +20 -0
- package/dist/locales/zh_CN.d.ts +5 -0
- package/dist/locales/zh_CN.js +6 -1
- package/dist/utils/container.js +14 -14
- package/dist/utils/history.d.ts +3 -1
- package/dist/utils/history.js +17 -13
- package/dist/utils/markdown.d.ts +6 -14
- package/dist/utils/markdown.js +72 -137
- package/dist/utils/rehype/rehype-line.d.ts +6 -0
- package/dist/utils/rehype/rehype-line.js +17 -0
- package/dist/utils/rehype/rehype-math.d.ts +13 -0
- package/dist/utils/rehype/rehype-math.js +167 -0
- package/dist/utils/rehype/rehype-raw-positions.d.ts +6 -0
- package/dist/utils/rehype/rehype-raw-positions.js +30 -0
- package/dist/x-star-md-editor/index.js +2 -5
- package/dist/x-star-md-viewer/index.js +8 -22
- package/package.json +4 -2
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import { SKIP, visit } from 'unist-util-visit';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 判断一个 Hast 节点是否属于行内代码或代码块
|
|
6
|
+
*
|
|
7
|
+
* @param node Hast 节点
|
|
8
|
+
* @returns 是否属于行内代码或代码块
|
|
9
|
+
*/
|
|
10
|
+
var isCodeNode = function isCodeNode(node) {
|
|
11
|
+
return node.type === 'element' && node.tagName === 'code';
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 判断一个 Hast 节点是否属于行内公式或公式块
|
|
16
|
+
*
|
|
17
|
+
* @param node Hast 节点
|
|
18
|
+
* @returns 是否属于行内公式或公式块
|
|
19
|
+
*/
|
|
20
|
+
export var isMathNode = function isMathNode(node) {
|
|
21
|
+
var _node$properties, _node$properties2;
|
|
22
|
+
return node.type === 'element' && Array.isArray((_node$properties = node.properties) === null || _node$properties === void 0 ? void 0 : _node$properties.className) && ((_node$properties2 = node.properties) === null || _node$properties2 === void 0 ? void 0 : _node$properties2.className.includes('math'));
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 解析文本中的公式
|
|
27
|
+
*
|
|
28
|
+
* @param text 文本
|
|
29
|
+
* @returns Hast 节点
|
|
30
|
+
*/
|
|
31
|
+
var parseMath = function parseMath(text) {
|
|
32
|
+
var result = [];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 扫描文本
|
|
36
|
+
*/
|
|
37
|
+
var scan = '';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 是否转义
|
|
41
|
+
*/
|
|
42
|
+
var escape = false;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 连续 `$` 的数量
|
|
46
|
+
*/
|
|
47
|
+
var level = 0;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 是否在行内公式中
|
|
51
|
+
*/
|
|
52
|
+
var inline = false;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 是否在公式块中
|
|
56
|
+
*/
|
|
57
|
+
var block = false;
|
|
58
|
+
var push = function push() {
|
|
59
|
+
if (level === 1 && !block) {
|
|
60
|
+
result.push(inline ? {
|
|
61
|
+
type: 'element',
|
|
62
|
+
tagName: 'span',
|
|
63
|
+
properties: {
|
|
64
|
+
className: ['math', 'math-inline']
|
|
65
|
+
},
|
|
66
|
+
children: [{
|
|
67
|
+
type: 'text',
|
|
68
|
+
value: scan
|
|
69
|
+
}]
|
|
70
|
+
} : {
|
|
71
|
+
type: 'text',
|
|
72
|
+
value: scan
|
|
73
|
+
});
|
|
74
|
+
scan = '';
|
|
75
|
+
inline = !inline;
|
|
76
|
+
} else if (level === 2 && !inline) {
|
|
77
|
+
result.push(block ? {
|
|
78
|
+
type: 'element',
|
|
79
|
+
tagName: 'div',
|
|
80
|
+
properties: {
|
|
81
|
+
className: ['math', 'math-display']
|
|
82
|
+
},
|
|
83
|
+
children: [{
|
|
84
|
+
type: 'text',
|
|
85
|
+
value: scan
|
|
86
|
+
}]
|
|
87
|
+
} : {
|
|
88
|
+
type: 'text',
|
|
89
|
+
value: scan
|
|
90
|
+
});
|
|
91
|
+
scan = '';
|
|
92
|
+
block = !block;
|
|
93
|
+
} else if (level) {
|
|
94
|
+
scan += '$'.repeat(level);
|
|
95
|
+
}
|
|
96
|
+
level = 0;
|
|
97
|
+
};
|
|
98
|
+
for (var i = 0; i <= text.length; i++) {
|
|
99
|
+
var _text$i;
|
|
100
|
+
var c = (_text$i = text[i]) !== null && _text$i !== void 0 ? _text$i : '';
|
|
101
|
+
if (escape) {
|
|
102
|
+
if (c === '\\') {
|
|
103
|
+
scan += c;
|
|
104
|
+
} else {
|
|
105
|
+
scan += c === '$' ? c : "\\".concat(c);
|
|
106
|
+
escape = false;
|
|
107
|
+
}
|
|
108
|
+
} else if (c === '$') {
|
|
109
|
+
level++;
|
|
110
|
+
} else {
|
|
111
|
+
push();
|
|
112
|
+
if (c === '\\') {
|
|
113
|
+
escape = true;
|
|
114
|
+
} else {
|
|
115
|
+
scan += c;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (scan) {
|
|
120
|
+
result.push({
|
|
121
|
+
type: 'text',
|
|
122
|
+
value: scan
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 解析属于 Raw 节点的文本中的数学公式
|
|
130
|
+
*/
|
|
131
|
+
var rehypeMath = function rehypeMath() {
|
|
132
|
+
return function (root) {
|
|
133
|
+
var _child$properties;
|
|
134
|
+
var child = root.children[root.children.length - 1];
|
|
135
|
+
if ((child === null || child === void 0 ? void 0 : child.type) !== 'element' || typeof ((_child$properties = child.properties) === null || _child$properties === void 0 ? void 0 : _child$properties.rawPositions) !== 'string') {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
root.children.pop();
|
|
139
|
+
|
|
140
|
+
// 获取 rehype-raw-positions 记录的 Raw 节点的位置信息
|
|
141
|
+
var rawPositions = child.properties.rawPositions.split(' ').map(function (offset) {
|
|
142
|
+
return parseInt(offset);
|
|
143
|
+
});
|
|
144
|
+
var i = 0;
|
|
145
|
+
visit(root, function (node, index, parent) {
|
|
146
|
+
if (isCodeNode(node) || isMathNode(node)) {
|
|
147
|
+
return SKIP;
|
|
148
|
+
}
|
|
149
|
+
if (node.type === 'text' && index !== null && parent) {
|
|
150
|
+
var _node$position, _node$position$start, _node$position2, _node$position2$end;
|
|
151
|
+
var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : (_node$position$start = _node$position.start) === null || _node$position$start === void 0 ? void 0 : _node$position$start.offset;
|
|
152
|
+
var endOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : (_node$position2$end = _node$position2.end) === null || _node$position2$end === void 0 ? void 0 : _node$position2$end.offset;
|
|
153
|
+
if (startOffset === undefined || endOffset === undefined) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
for (; i < rawPositions.length - 1 && rawPositions[i + 1] <= startOffset; i += 2);
|
|
157
|
+
if (i < rawPositions.length - 1 && rawPositions[i] <= startOffset && rawPositions[i + 1] >= endOffset) {
|
|
158
|
+
var _parent$children;
|
|
159
|
+
var result = parseMath(node.value);
|
|
160
|
+
(_parent$children = parent.children).splice.apply(_parent$children, [index, 1].concat(_toConsumableArray(result)));
|
|
161
|
+
return index + result.length;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
export default rehypeMath;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 保存 Raw 节点的位置信息
|
|
5
|
+
*/
|
|
6
|
+
var rehypeRawPositions = function rehypeRawPositions() {
|
|
7
|
+
return function (root) {
|
|
8
|
+
var rawPositions = [];
|
|
9
|
+
visit(root, 'raw', function (node) {
|
|
10
|
+
var _node$position, _node$position$start, _node$position2, _node$position2$end;
|
|
11
|
+
var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : (_node$position$start = _node$position.start) === null || _node$position$start === void 0 ? void 0 : _node$position$start.offset;
|
|
12
|
+
var endOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : (_node$position2$end = _node$position2.end) === null || _node$position2$end === void 0 ? void 0 : _node$position2$end.offset;
|
|
13
|
+
if (startOffset === undefined || endOffset === undefined) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
rawPositions.push(startOffset, endOffset);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// 记录在新的 Hast 节点上,防止被 rehype-raw 删除
|
|
20
|
+
root.children.push({
|
|
21
|
+
type: 'element',
|
|
22
|
+
tagName: 'div',
|
|
23
|
+
properties: {
|
|
24
|
+
rawPositions: rawPositions.join(' ')
|
|
25
|
+
},
|
|
26
|
+
children: []
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export default rehypeRawPositions;
|
|
@@ -238,12 +238,9 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
238
238
|
onInsertFileLatest.current = onInsertFile;
|
|
239
239
|
var options = useMemo(function () {
|
|
240
240
|
return composeHandlers(plugins)({
|
|
241
|
-
toolbarItemMap: getDefaultToolbarItemMap(locale, function () {
|
|
241
|
+
toolbarItemMap: getDefaultToolbarItemMap(locale, function (file, options) {
|
|
242
242
|
var _onInsertFileLatest$c;
|
|
243
|
-
|
|
244
|
-
args[_key] = arguments[_key];
|
|
245
|
-
}
|
|
246
|
-
return (_onInsertFileLatest$c = onInsertFileLatest.current) === null || _onInsertFileLatest$c === void 0 ? void 0 : _onInsertFileLatest$c.call.apply(_onInsertFileLatest$c, [onInsertFileLatest].concat(args));
|
|
243
|
+
return (_onInsertFileLatest$c = onInsertFileLatest.current) === null || _onInsertFileLatest$c === void 0 ? void 0 : _onInsertFileLatest$c.call(onInsertFileLatest, file, options);
|
|
247
244
|
}),
|
|
248
245
|
toolbarItems: getDefaultToolbarItems(),
|
|
249
246
|
keyboardEventHandlers: getDefaultKeyboardEventHandlers()
|