x-star-editor 0.0.2 → 0.0.4

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,5 +1,5 @@
1
- var _defaultSchema$tagNam, _defaultSchema$attrib, _defaultSchema$attrib2, _defaultSchema$attrib3, _defaultSchema$attrib4, _defaultSchema$attrib5, _defaultSchema$attrib6, _defaultSchema$attrib7, _defaultSchema$attrib8;
2
1
  function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
3
3
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
4
4
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
5
5
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
@@ -23,17 +23,20 @@ import remarkMath from 'remark-math';
23
23
  import remarkParse from 'remark-parse';
24
24
  import remarkRehype from 'remark-rehype';
25
25
  import { unified } from 'unified';
26
- var schema = _objectSpread(_objectSpread({}, defaultSchema), {}, {
27
- tagNames: [].concat(_toConsumableArray((_defaultSchema$tagNam = defaultSchema.tagNames) !== null && _defaultSchema$tagNam !== void 0 ? _defaultSchema$tagNam : []), ['video', 'custom']),
28
- attributes: _objectSpread(_objectSpread({}, defaultSchema.attributes), {}, {
29
- pre: [].concat(_toConsumableArray((_defaultSchema$attrib = (_defaultSchema$attrib2 = defaultSchema.attributes) === null || _defaultSchema$attrib2 === void 0 ? void 0 : _defaultSchema$attrib2.pre) !== null && _defaultSchema$attrib !== void 0 ? _defaultSchema$attrib : []), ['className', 'style']),
30
- code: [].concat(_toConsumableArray((_defaultSchema$attrib3 = (_defaultSchema$attrib4 = defaultSchema.attributes) === null || _defaultSchema$attrib4 === void 0 ? void 0 : _defaultSchema$attrib4.code) !== null && _defaultSchema$attrib3 !== void 0 ? _defaultSchema$attrib3 : []), ['className', 'style']),
31
- div: [].concat(_toConsumableArray((_defaultSchema$attrib5 = (_defaultSchema$attrib6 = defaultSchema.attributes) === null || _defaultSchema$attrib6 === void 0 ? void 0 : _defaultSchema$attrib6.div) !== null && _defaultSchema$attrib5 !== void 0 ? _defaultSchema$attrib5 : []), ['className', 'style']),
32
- span: [].concat(_toConsumableArray((_defaultSchema$attrib7 = (_defaultSchema$attrib8 = defaultSchema.attributes) === null || _defaultSchema$attrib8 === void 0 ? void 0 : _defaultSchema$attrib8.span) !== null && _defaultSchema$attrib7 !== void 0 ? _defaultSchema$attrib7 : []), ['className', 'style', 'line']),
33
- video: ['src'],
34
- custom: ['meta', 'value']
35
- })
36
- });
26
+ import { prefix } from "./global";
27
+ export var getDefaultSchema = function getDefaultSchema() {
28
+ var _defaultSchema$tagNam, _defaultSchema$attrib, _defaultSchema$attrib2, _defaultSchema$attrib3, _defaultSchema$attrib4, _defaultSchema$attrib5, _defaultSchema$attrib6, _defaultSchema$attrib7, _defaultSchema$attrib8;
29
+ return _objectSpread(_objectSpread({}, defaultSchema), {}, {
30
+ tagNames: [].concat(_toConsumableArray((_defaultSchema$tagNam = defaultSchema.tagNames) !== null && _defaultSchema$tagNam !== void 0 ? _defaultSchema$tagNam : []), ['custom']),
31
+ attributes: _objectSpread(_objectSpread({}, defaultSchema.attributes), {}, {
32
+ pre: [].concat(_toConsumableArray((_defaultSchema$attrib = (_defaultSchema$attrib2 = defaultSchema.attributes) === null || _defaultSchema$attrib2 === void 0 ? void 0 : _defaultSchema$attrib2.pre) !== null && _defaultSchema$attrib !== void 0 ? _defaultSchema$attrib : []), ['className', 'style']),
33
+ code: [].concat(_toConsumableArray((_defaultSchema$attrib3 = (_defaultSchema$attrib4 = defaultSchema.attributes) === null || _defaultSchema$attrib4 === void 0 ? void 0 : _defaultSchema$attrib4.code) !== null && _defaultSchema$attrib3 !== void 0 ? _defaultSchema$attrib3 : []), ['className', 'style']),
34
+ div: [].concat(_toConsumableArray((_defaultSchema$attrib5 = (_defaultSchema$attrib6 = defaultSchema.attributes) === null || _defaultSchema$attrib6 === void 0 ? void 0 : _defaultSchema$attrib6.div) !== null && _defaultSchema$attrib5 !== void 0 ? _defaultSchema$attrib5 : []), ['className', 'style']),
35
+ span: [].concat(_toConsumableArray((_defaultSchema$attrib7 = (_defaultSchema$attrib8 = defaultSchema.attributes) === null || _defaultSchema$attrib8 === void 0 ? void 0 : _defaultSchema$attrib8.span) !== null && _defaultSchema$attrib7 !== void 0 ? _defaultSchema$attrib7 : []), ['className', 'style', 'line']),
36
+ custom: ['meta', 'value']
37
+ })
38
+ });
39
+ };
37
40
 
38
41
  /**
39
42
  * 将 Markdown 文本解析为 Mdast 树并转换为 Hast 树的处理器
@@ -53,7 +56,25 @@ var processor = unified().use(remarkParse).use(remarkBreaks).use(remarkGfm).use(
53
56
  }
54
57
  }).use(rehypePrism, {
55
58
  ignoreMissing: true
56
- }).use(rehypeKatex).use(rehypeRaw).use(rehypeSanitize, schema).freeze();
59
+ }).use(rehypeKatex).use(rehypeRaw).use(function () {
60
+ return function (root) {
61
+ var _iterator = _createForOfIteratorHelper(root.children),
62
+ _step;
63
+ try {
64
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
65
+ var node = _step.value;
66
+ if ('properties' in node && node.properties) {
67
+ var _node$position;
68
+ node.properties.line = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.line;
69
+ }
70
+ }
71
+ } catch (err) {
72
+ _iterator.e(err);
73
+ } finally {
74
+ _iterator.f();
75
+ }
76
+ };
77
+ }).freeze();
57
78
 
58
79
  /**
59
80
  * Mdast 节点
@@ -69,7 +90,7 @@ export var editorRender = function editorRender(sourceCode) {
69
90
  /**
70
91
  * CSS 类名前缀
71
92
  */
72
- var classNamePrefix = 'x-star-md-editor-';
93
+ var classNamePrefix = "".concat(prefix, "md-editor-");
73
94
 
74
95
  /**
75
96
  * 扫描偏移量,表示已经添加到 DOM 树的文本
@@ -211,51 +232,60 @@ export var editorRender = function editorRender(sourceCode) {
211
232
  * @param parentEndOffset 父节点终点偏移量
212
233
  */
213
234
  var getChildren = function getChildren(nodes, parentEndOffset) {
214
- for (var i = 0; i < nodes.length; i++) {
215
- var _node$position, _node$position2;
216
- var node = nodes[i];
217
- var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.offset;
218
- var endOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : _node$position2.end.offset;
219
- if (startOffset === undefined || endOffset === undefined) {
220
- continue;
221
- }
222
- // 由于 unified 的问题,起点偏移量可能小于扫描偏移量,应修正
223
- if (startOffset < scanOffset) {
224
- startOffset = scanOffset;
225
- }
226
- // 如果起点偏移量大于等于终点偏移量,则不用添加任何文本
227
- if (startOffset >= endOffset) {
228
- continue;
229
- }
235
+ var _iterator2 = _createForOfIteratorHelper(nodes),
236
+ _step2;
237
+ try {
238
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
239
+ var _node$position2, _node$position3;
240
+ var node = _step2.value;
241
+ var startOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : _node$position2.start.offset;
242
+ var endOffset = (_node$position3 = node.position) === null || _node$position3 === void 0 ? void 0 : _node$position3.end.offset;
243
+ if (startOffset === undefined || endOffset === undefined) {
244
+ continue;
245
+ }
246
+ // 由于 unified 的问题,起点偏移量可能小于扫描偏移量,应修正
247
+ if (startOffset < scanOffset) {
248
+ startOffset = scanOffset;
249
+ }
250
+ // 如果起点偏移量大于等于终点偏移量,则不用添加任何文本
251
+ if (startOffset >= endOffset) {
252
+ continue;
253
+ }
230
254
 
231
- // 添加扫描偏移量与起点偏移量间的分隔文本
232
- addText(sourceCode.slice(scanOffset, startOffset), true);
255
+ // 添加扫描偏移量与起点偏移量间的分隔文本
256
+ addText(sourceCode.slice(scanOffset, startOffset), true);
233
257
 
234
- // 为 Mdast 节点创建 DOM 元素
235
- stack.push({
236
- node: node,
237
- el: document.createElement(stack.length ? 'span' : 'div'),
238
- first: true
239
- });
240
- if ('children' in node) {
241
- getChildren(node.children, endOffset);
242
- } else {
243
- addText(sourceCode.slice(scanOffset, endOffset));
244
- }
245
- if (stack.length > 1) {
246
- pushElement(stack.length - 1, true);
247
- } else {
248
- // 如果栈大小为 1,说明为块级元素终点
249
- stack[0].el.append("\u200B");
250
- pushElement(0, true);
251
- block.setAttribute('index', "".concat(i));
252
- pushBlock();
253
- scanOffset++;
258
+ // 为 Mdast 节点创建 DOM 元素
259
+ stack.push({
260
+ node: node,
261
+ el: document.createElement(stack.length ? 'span' : 'div'),
262
+ first: true
263
+ });
264
+ if ('children' in node) {
265
+ getChildren(node.children, endOffset);
266
+ } else {
267
+ addText(sourceCode.slice(scanOffset, endOffset));
268
+ }
269
+ if (stack.length > 1) {
270
+ pushElement(stack.length - 1, true);
271
+ } else {
272
+ var _node$position4;
273
+ // 如果栈大小为 1,说明为块级元素终点
274
+ stack[0].el.append("\u200B");
275
+ pushElement(0, true);
276
+ block.setAttribute('line', "".concat((_node$position4 = node.position) === null || _node$position4 === void 0 ? void 0 : _node$position4.start.line));
277
+ pushBlock();
278
+ scanOffset++;
279
+ }
280
+ stack.pop();
254
281
  }
255
- stack.pop();
256
- }
257
282
 
258
- // 添加扫描偏移量与父节点终点偏移量间的分隔文本
283
+ // 添加扫描偏移量与父节点终点偏移量间的分隔文本
284
+ } catch (err) {
285
+ _iterator2.e(err);
286
+ } finally {
287
+ _iterator2.f();
288
+ }
259
289
  addText(sourceCode.slice(scanOffset, parentEndOffset), true);
260
290
  };
261
291
  getChildren(processor.parse(sourceCode).children, sourceCode.length);
@@ -274,7 +304,7 @@ export var editorRender = function editorRender(sourceCode) {
274
304
  * @returns React 虚拟 DOM 树
275
305
  */
276
306
  export var viewerRender = function viewerRender(sourceCode, options) {
277
- return toJsxRuntime(processor.runSync(processor.parse(sourceCode)), {
307
+ return toJsxRuntime(processor().use(rehypeSanitize, options.customSchema).runSync(processor.parse(sourceCode)), {
278
308
  Fragment: Fragment,
279
309
  jsx: jsx,
280
310
  jsxs: jsxs,
package/package.json CHANGED
@@ -1,29 +1,26 @@
1
1
  {
2
2
  "name": "x-star-editor",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "An editor developed by turingstar",
5
+ "license": "MIT",
5
6
  "module": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
7
11
  "scripts": {
8
- "start": "npm run dev",
9
- "dev": "dumi dev",
10
12
  "build": "father build",
11
13
  "build:watch": "father dev",
14
+ "dev": "dumi dev",
12
15
  "docs:build": "dumi build",
13
- "prepare": "husky install && dumi setup",
14
16
  "doctor": "father doctor",
15
17
  "lint": "npm run lint:es && npm run lint:css",
16
18
  "lint:css": "stylelint \"{src,test}/**/*.{css,less}\"",
17
19
  "lint:es": "eslint \"{src,test}/**/*.{js,jsx,ts,tsx}\"",
18
- "prepublishOnly": "father doctor && npm run build"
20
+ "prepare": "husky install && dumi setup",
21
+ "prepublishOnly": "father doctor && npm run build",
22
+ "start": "npm run dev"
19
23
  },
20
- "authors": [
21
- "XYD Tech Team tech@xinyoudui.com"
22
- ],
23
- "license": "MIT",
24
- "files": [
25
- "dist"
26
- ],
27
24
  "commitlint": {
28
25
  "extends": [
29
26
  "@commitlint/config-conventional"
@@ -46,14 +43,22 @@
46
43
  "prettier --parser=typescript --write"
47
44
  ]
48
45
  },
49
- "publishConfig": {
50
- "access": "public"
51
- },
52
- "peerDependencies": {
53
- "react": ">=16.9.0",
54
- "react-dom": ">=16.9.0"
46
+ "dependencies": {
47
+ "classnames": "^2.3.2",
48
+ "hast-util-to-jsx-runtime": "^1.2.0",
49
+ "rehype-katex": "^6.0.3",
50
+ "rehype-prism-plus": "^1.6.1",
51
+ "rehype-raw": "^6.1.1",
52
+ "rehype-sanitize": "^5.0.1",
53
+ "remark-breaks": "^3.0.3",
54
+ "remark-gfm": "^3.0.1",
55
+ "remark-math": "^5.1.1",
56
+ "remark-parse": "^10.0.2",
57
+ "remark-rehype": "^10.1.0",
58
+ "unified": "^10.1.2"
55
59
  },
56
60
  "devDependencies": {
61
+ "@babel/core": "^7.22.10",
57
62
  "@commitlint/cli": "^17.1.2",
58
63
  "@commitlint/config-conventional": "^17.1.0",
59
64
  "@types/react": "^18.0.0",
@@ -64,25 +69,25 @@
64
69
  "father": "^4.1.0",
65
70
  "husky": "^8.0.1",
66
71
  "lint-staged": "^13.0.3",
72
+ "postcss": "^8.4.28",
67
73
  "prettier": "^2.7.1",
68
74
  "prettier-plugin-organize-imports": "^3.0.0",
69
75
  "prettier-plugin-packagejson": "^2.2.18",
70
76
  "react": "^18.0.0",
71
77
  "react-dom": "^18.0.0",
72
- "stylelint": "^14.9.1"
78
+ "styled-components": "^6.0.7",
79
+ "stylelint": "^14.9.1",
80
+ "typescript": "^5.1.6",
81
+ "webpack": "^5.88.2"
73
82
  },
74
- "dependencies": {
75
- "classnames": "^2.3.2",
76
- "hast-util-to-jsx-runtime": "^1.2.0",
77
- "rehype-katex": "^6.0.3",
78
- "rehype-prism-plus": "^1.6.1",
79
- "rehype-raw": "^6.1.1",
80
- "rehype-sanitize": "^5.0.1",
81
- "remark-breaks": "^3.0.3",
82
- "remark-gfm": "^3.0.1",
83
- "remark-math": "^5.1.1",
84
- "remark-parse": "^10.0.2",
85
- "remark-rehype": "^10.1.0",
86
- "unified": "^10.1.2"
87
- }
83
+ "peerDependencies": {
84
+ "react": ">=16.9.0",
85
+ "react-dom": ">=16.9.0"
86
+ },
87
+ "publishConfig": {
88
+ "access": "public"
89
+ },
90
+ "authors": [
91
+ "XYD Tech Team tech@xinyoudui.com"
92
+ ]
88
93
  }
@@ -1,19 +0,0 @@
1
- .container {
2
- display: flex;
3
- flex-wrap: wrap;
4
- }
5
-
6
- .editor {
7
- flex: 1;
8
- border-bottom-right-radius: 0;
9
- }
10
-
11
- .viewer {
12
- flex: 1;
13
- border-radius: 0;
14
- border-bottom-right-radius: 0.2em;
15
- }
16
-
17
- .toolbar {
18
- width: 100%;
19
- }
@@ -1,81 +0,0 @@
1
- .container {
2
- margin: 0.5em 1em 1em 1em;
3
- }
4
-
5
- .tabs {
6
- display: flex;
7
- }
8
-
9
- .tab {
10
- padding: 0.4em 1em;
11
- border-bottom: 1px solid #f5771280;
12
- cursor: pointer;
13
- user-select: none;
14
- }
15
-
16
- .tab.active {
17
- color: #f57712;
18
- border-bottom: 2px solid #f57712;
19
- }
20
-
21
- .field {
22
- margin: 0.8em;
23
- display: flex;
24
- flex-direction: column;
25
- gap: 0.2em;
26
- font-size: 0.9em;
27
- }
28
-
29
- .file-container {
30
- position: relative;
31
- display: flex;
32
- align-items: center;
33
- justify-content: space-between;
34
- }
35
-
36
- .hidden {
37
- position: absolute;
38
- inset: 0;
39
- opacity: 0;
40
- }
41
-
42
- .input {
43
- width: 20em;
44
- padding: 0.4em;
45
- border: 1px solid #d0d0d0;
46
- border-radius: 0.2em;
47
- outline: none;
48
- }
49
-
50
- .input:focus {
51
- border: 1px solid #f57712;
52
- }
53
-
54
- .input.file-input {
55
- width: 14em;
56
- }
57
-
58
- .buttons {
59
- display: flex;
60
- flex-direction: row-reverse;
61
- gap: 0.4em;
62
- }
63
-
64
- .button {
65
- padding: 0.4em 1em;
66
- background-color: #f0f0f0;
67
- border: 1px solid #d0d0d0;
68
- border-radius: 0.2em;
69
- cursor: pointer;
70
- user-select: none;
71
- }
72
-
73
- .button.primary {
74
- color: #ffffff;
75
- background-color: #f57712;
76
- border: none;
77
- }
78
-
79
- .button.file-button {
80
- padding: 0.3em 0.4em;
81
- }
@@ -1,116 +0,0 @@
1
- .button-container {
2
- position: relative;
3
- }
4
-
5
- .button {
6
- width: 2em;
7
- height: 2em;
8
- display: flex;
9
- align-items: center;
10
- justify-content: center;
11
- font-family: Consolas;
12
- border: 1px solid transparent;
13
- cursor: pointer;
14
- user-select: none;
15
- }
16
-
17
- .button-hover {
18
- background-color: #f8f8f8;
19
- border: 1px solid #f5771280;
20
- border-radius: 0.2em;
21
- }
22
-
23
- .tooltip {
24
- position: absolute;
25
- top: 3em;
26
- width: max-content;
27
- padding: 0.4em;
28
- font-size: 0.8em;
29
- color: #f8f8f8;
30
- background-color: #022679;
31
- border-radius: 0.2em;
32
- z-index: 2;
33
- }
34
-
35
- .tooltip::before {
36
- content: '';
37
- position: absolute;
38
- top: -0.3em;
39
- left: 50%;
40
- border-top: 0.6em solid #022679;
41
- border-right: 0.6em solid transparent;
42
- transform-origin: 0 0;
43
- transform: rotate(45deg);
44
- }
45
-
46
- .popover {
47
- position: absolute;
48
- width: max-content;
49
- background-color: #f8f8f8;
50
- border: 1px solid #f5771280;
51
- border-radius: 0.2em;
52
- box-shadow: 0 2px 4px #00000020;
53
- opacity: 0;
54
- z-index: 1;
55
- }
56
-
57
- .option {
58
- padding: 4px 8px;
59
- cursor: pointer;
60
- user-select: none;
61
- }
62
-
63
- .option:hover {
64
- background-color: #f0f0f0;
65
- }
66
-
67
- .icon {
68
- width: 45%;
69
- height: 45%;
70
- background-size: contain;
71
- }
72
-
73
- .icon.code-library {
74
- background-image: url(icons/code-library.svg);
75
- }
76
-
77
- .icon.italic {
78
- background-image: url(icons/italic.svg);
79
- }
80
-
81
- .icon.bold {
82
- background-image: url(icons/bold.svg);
83
- }
84
-
85
- .icon.code {
86
- background-image: url(icons/code.svg);
87
- }
88
-
89
- .icon.link {
90
- background-image: url(icons/link.svg);
91
- }
92
-
93
- .icon.image {
94
- background-image: url(icons/image.svg);
95
- }
96
-
97
- .icon.strikethrough {
98
- background-image: url(icons/strikethrough.svg);
99
- }
100
-
101
- .toolbar {
102
- margin: 0 -1px;
103
- padding: 0.2em;
104
- display: flex;
105
- flex-wrap: wrap;
106
- gap: 0.2em;
107
- background-color: #f5771210;
108
- border: 1px solid #f5771220;
109
- border-top-left-radius: 0.2em;
110
- border-top-right-radius: 0.2em;
111
- }
112
-
113
- .divider {
114
- margin: 0.4em;
115
- border: 1px solid #f5771280;
116
- }