x-star-editor 0.2.5 → 0.2.6

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.
@@ -190,10 +190,10 @@ export var editorRender = function editorRender(sourceCode) {
190
190
  _step;
191
191
  try {
192
192
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
193
- var _node$position, _node$position$start, _node$position2, _node$position2$end;
193
+ var _node$position, _node$position2;
194
194
  var node = _step.value;
195
- 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;
196
- 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;
195
+ var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.offset;
196
+ var endOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : _node$position2.end.offset;
197
197
  if (startOffset === undefined || endOffset === undefined) {
198
198
  continue;
199
199
  }
@@ -223,12 +223,12 @@ export var editorRender = function editorRender(sourceCode) {
223
223
  if (stack.length > 1) {
224
224
  pushElement(stack.length - 1);
225
225
  } else {
226
- var _node$position3, _node$position3$start;
226
+ var _node$position3;
227
227
  // 如果栈大小为 1,说明为块级元素终点
228
228
  stack[0].element.append("\u200B");
229
229
  pushElement(0);
230
230
  // 记录行号,用于同步滚动
231
- block.dataset.line = "".concat((_node$position3 = node.position) === null || _node$position3 === void 0 ? void 0 : (_node$position3$start = _node$position3.start) === null || _node$position3$start === void 0 ? void 0 : _node$position3$start.line);
231
+ block.dataset.line = "".concat((_node$position3 = node.position) === null || _node$position3 === void 0 ? void 0 : _node$position3.start.line);
232
232
  pushBlock();
233
233
  scanOffset++;
234
234
  }
@@ -394,10 +394,9 @@ var toMarkdownProcessor = unified().use(rehypeRemark, {
394
394
  return isMathNode(node) ? h(node, 'inlineMath', hastToText(node)) : defaultHandlers.span(h, node);
395
395
  },
396
396
  custom: function custom(h, node) {
397
- var _node$properties, _node$properties2;
398
397
  return h(node, 'math', {
399
- meta: (_node$properties = node.properties) === null || _node$properties === void 0 ? void 0 : _node$properties.meta
400
- }, (_node$properties2 = node.properties) === null || _node$properties2 === void 0 ? void 0 : _node$properties2.value);
398
+ meta: node.properties.meta
399
+ }, node.properties.value);
401
400
  }
402
401
  }
403
402
  }).use(remarkMath).use(remarkGfm).use(remarkStringify).freeze();
@@ -6,10 +6,10 @@ import { SKIP, visit } from 'unist-util-visit';
6
6
  var rehypeLine = function rehypeLine() {
7
7
  return function (root) {
8
8
  return visit(root, 'element', function (node) {
9
- var _node$properties, _node$position, _node$position$start;
9
+ var _node$properties, _node$position;
10
10
  (_node$properties = node.properties) !== null && _node$properties !== void 0 ? _node$properties : node.properties = {};
11
11
  // 记录行号,用于同步滚动
12
- node.properties['data-line'] = (_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.line;
12
+ node.properties['data-line'] = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.line;
13
13
  return SKIP;
14
14
  });
15
15
  };
@@ -1,5 +1,5 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
- import { SKIP, visit } from 'unist-util-visit';
2
+ import { EXIT, SKIP, visit } from 'unist-util-visit';
3
3
 
4
4
  /**
5
5
  * 判断一个 Hast 节点是否属于行内代码或代码块
@@ -130,31 +130,39 @@ var parseMath = function parseMath(text) {
130
130
  */
131
131
  var rehypeMath = function rehypeMath() {
132
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();
133
+ var rawPositions = [];
134
+ var i = 0;
139
135
 
140
136
  // 获取 rehype-raw-positions 记录的 Raw 节点的位置信息
141
- var rawPositions = child.properties.rawPositions.split(' ').map(function (offset) {
142
- return parseInt(offset);
143
- });
144
- var i = 0;
137
+ visit(root, {
138
+ tagName: 'raw-positions'
139
+ }, function (node, index, parent) {
140
+ var _node$properties3;
141
+ if (typeof ((_node$properties3 = node.properties) === null || _node$properties3 === void 0 ? void 0 : _node$properties3.value) === 'string') {
142
+ rawPositions = node.properties.value.split(' ').map(function (offset) {
143
+ return parseInt(offset);
144
+ });
145
+ }
146
+ if (index !== null && parent) {
147
+ parent.children.splice(index);
148
+ }
149
+ return EXIT;
150
+ }, true);
151
+ if (rawPositions.length < 2) {
152
+ return;
153
+ }
145
154
  visit(root, function (node, index, parent) {
146
155
  if (isCodeNode(node) || isMathNode(node)) {
147
156
  return SKIP;
148
157
  }
149
158
  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) {
159
+ var _node$position;
160
+ var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.offset;
161
+ if (startOffset === undefined) {
154
162
  return;
155
163
  }
156
164
  for (; i < rawPositions.length - 1 && rawPositions[i + 1] <= startOffset; i += 2);
157
- if (i < rawPositions.length - 1 && rawPositions[i] <= startOffset && rawPositions[i + 1] >= endOffset) {
165
+ if (i < rawPositions.length - 1 && rawPositions[i] <= startOffset) {
158
166
  var _parent$children;
159
167
  var result = parseMath(node.value);
160
168
  (_parent$children = parent.children).splice.apply(_parent$children, [index, 1].concat(_toConsumableArray(result)));
@@ -7,9 +7,9 @@ var rehypeRawPositions = function rehypeRawPositions() {
7
7
  return function (root) {
8
8
  var rawPositions = [];
9
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;
10
+ var _node$position, _node$position2;
11
+ var startOffset = (_node$position = node.position) === null || _node$position === 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.offset;
13
13
  if (startOffset === undefined || endOffset === undefined) {
14
14
  return;
15
15
  }
@@ -19,9 +19,9 @@ var rehypeRawPositions = function rehypeRawPositions() {
19
19
  // 记录在新的 Hast 节点上,防止被 rehype-raw 删除
20
20
  root.children.push({
21
21
  type: 'element',
22
- tagName: 'div',
22
+ tagName: 'raw-positions',
23
23
  properties: {
24
- rawPositions: rawPositions.join(' ')
24
+ value: rawPositions.join(' ')
25
25
  },
26
26
  children: []
27
27
  });