@seafile/sdoc-editor 3.0.170 → 3.0.172

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.
@@ -5,48 +5,106 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports["default"] = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/toConsumableArray"));
8
9
  var _slugid = _interopRequireDefault(require("slugid"));
9
10
  var _constants = require("../../../constants");
10
11
  var _prismjs = require("../../code-block/prismjs");
12
+ var BLOCK_LINE_TAGS = ['DIV', 'P'];
13
+ var normalizeCodeText = function normalizeCodeText() {
14
+ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
15
+ return text.replace(/\u00a0/g, ' ');
16
+ };
17
+ var isFormattingWhitespace = function isFormattingWhitespace() {
18
+ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
19
+ return text !== '' && text.trim() === '';
20
+ };
21
+ var appendTextToLines = function appendTextToLines(lines) {
22
+ var text = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
23
+ var segments = normalizeCodeText(text).replace(/\r\n?/g, '\n').split('\n');
24
+ lines[lines.length - 1] += segments[0] || '';
25
+ if (segments.length > 1) {
26
+ lines.push.apply(lines, (0, _toConsumableArray2["default"])(segments.slice(1)));
27
+ }
28
+ };
29
+ var _extractCodeLines = function extractCodeLines(nodes) {
30
+ var lines = [''];
31
+ Array.from(nodes).forEach(function (node) {
32
+ if (node.nodeName === 'BR') {
33
+ lines.push('');
34
+ return;
35
+ }
36
+ if (node.nodeType === 3) {
37
+ var text = node.textContent || '';
38
+ if (isFormattingWhitespace(text)) return;
39
+ appendTextToLines(lines, text);
40
+ return;
41
+ }
42
+ if (BLOCK_LINE_TAGS.includes(node.nodeName)) {
43
+ var blockLines = _extractCodeLines(node.childNodes);
44
+ if (blockLines.length === 0) {
45
+ lines.push('');
46
+ return;
47
+ }
48
+ if (lines.length === 1 && lines[0] === '') {
49
+ lines = blockLines;
50
+ } else {
51
+ lines[lines.length - 1] += blockLines[0] || '';
52
+ if (blockLines.length > 1) {
53
+ var _lines;
54
+ (_lines = lines).push.apply(_lines, (0, _toConsumableArray2["default"])(blockLines.slice(1)));
55
+ }
56
+ }
57
+ var nextNode = node.nextSibling;
58
+ var hasNextBlockSibling = nextNode && BLOCK_LINE_TAGS.includes(nextNode.nodeName);
59
+ if (hasNextBlockSibling && blockLines[blockLines.length - 1] !== '') {
60
+ lines.push('');
61
+ }
62
+ return;
63
+ }
64
+ var nestedLines = _extractCodeLines(node.childNodes);
65
+ if (nestedLines.length === 0) {
66
+ return;
67
+ }
68
+ if (lines.length === 1 && lines[0] === '') {
69
+ lines = nestedLines;
70
+ return;
71
+ }
72
+ lines[lines.length - 1] += nestedLines[0] || '';
73
+ if (nestedLines.length > 1) {
74
+ var _lines2;
75
+ (_lines2 = lines).push.apply(_lines2, (0, _toConsumableArray2["default"])(nestedLines.slice(1)));
76
+ }
77
+ });
78
+ return lines;
79
+ };
80
+ var buildCodeLines = function buildCodeLines(element) {
81
+ var lines = _extractCodeLines(element.childNodes);
82
+ return lines.map(function (text) {
83
+ return {
84
+ id: _slugid["default"].nice(),
85
+ type: _constants.CODE_LINE,
86
+ children: [{
87
+ id: _slugid["default"].nice(),
88
+ text: text
89
+ }]
90
+ };
91
+ });
92
+ };
11
93
  var codeBlockRule = function codeBlockRule(element, parseChild) {
12
94
  var nodeName = element.nodeName,
13
95
  childNodes = element.childNodes;
14
96
  if (nodeName === 'PRE') {
15
- var children = Array.from(childNodes).filter(function (item) {
16
- return item.nodeName === 'CODE';
17
- });
18
- var codeChild = children[0];
19
- if (codeChild) {
20
- var lang = codeChild.getAttribute('lang');
21
- lang = (0, _prismjs.genCodeLangs)().find(function (item) {
22
- return item.value === lang;
23
- }) || 'plaintext';
24
- return {
25
- id: _slugid["default"].nice(),
26
- language: lang,
27
- type: _constants.CODE_BLOCK,
28
- children: parseChild(children)
29
- };
30
- } else {
31
- var _lang = 'plaintext';
32
- var content = childNodes[0].textContent;
33
- var _children = content.split('\n').map(function (text) {
34
- return {
35
- id: _slugid["default"].nice(),
36
- type: _constants.CODE_LINE,
37
- children: [{
38
- id: _slugid["default"].nice(),
39
- text: text
40
- }]
41
- };
42
- });
43
- return {
44
- id: _slugid["default"].nice(),
45
- language: _lang,
46
- type: _constants.CODE_BLOCK,
47
- children: _children
48
- };
49
- }
97
+ var codeChild = element.querySelector('code');
98
+ var lang = codeChild === null || codeChild === void 0 ? void 0 : codeChild.getAttribute('lang');
99
+ lang = (0, _prismjs.genCodeLangs)().find(function (item) {
100
+ return item.value === lang;
101
+ }) || 'plaintext';
102
+ return {
103
+ id: _slugid["default"].nice(),
104
+ language: lang,
105
+ type: _constants.CODE_BLOCK,
106
+ children: buildCodeLines(codeChild || element)
107
+ };
50
108
  }
51
109
  if (nodeName === 'CODE' && element.parentElement.nodeName === 'PRE') {
52
110
  var childIsP = Array.from(childNodes).every(function (n) {
@@ -64,8 +122,8 @@ var codeBlockRule = function codeBlockRule(element, parseChild) {
64
122
  };
65
123
  });
66
124
  }
67
- var _content = element.textContent;
68
- var hasNewLine = _content.indexOf('\n') > -1;
125
+ var content = element.textContent;
126
+ var hasNewLine = content.indexOf('\n') > -1;
69
127
  if (!hasNewLine) {
70
128
  return {
71
129
  id: _slugid["default"].nice(),
@@ -76,7 +134,7 @@ var codeBlockRule = function codeBlockRule(element, parseChild) {
76
134
  }]
77
135
  };
78
136
  }
79
- var codes = _content.split('\n');
137
+ var codes = content.split('\n');
80
138
  return codes.map(function (item) {
81
139
  return {
82
140
  id: _slugid["default"].nice(),
@@ -5,49 +5,106 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports["default"] = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/toConsumableArray"));
8
9
  var _slugid = _interopRequireDefault(require("slugid"));
9
10
  var _constants = require("../constants");
10
11
  var _helper = require("../helper");
12
+ var BLOCK_LINE_TAGS = ['DIV', 'P'];
13
+ var normalizeCodeText = function normalizeCodeText() {
14
+ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
15
+ return text.replace(/\u00a0/g, ' ');
16
+ };
17
+ var isFormattingWhitespace = function isFormattingWhitespace() {
18
+ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
19
+ return text !== '' && text.trim() === '';
20
+ };
21
+ var appendTextToLines = function appendTextToLines(lines) {
22
+ var text = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
23
+ var segments = normalizeCodeText(text).replace(/\r\n?/g, '\n').split('\n');
24
+ lines[lines.length - 1] += segments[0] || '';
25
+ if (segments.length > 1) {
26
+ lines.push.apply(lines, (0, _toConsumableArray2["default"])(segments.slice(1)));
27
+ }
28
+ };
29
+ var _extractCodeLines = function extractCodeLines(nodes) {
30
+ var lines = [''];
31
+ Array.from(nodes).forEach(function (node) {
32
+ if (node.nodeName === 'BR') {
33
+ lines.push('');
34
+ return;
35
+ }
36
+ if (node.nodeType === 3) {
37
+ var text = node.textContent || '';
38
+ if (isFormattingWhitespace(text)) return;
39
+ appendTextToLines(lines, text);
40
+ return;
41
+ }
42
+ if (BLOCK_LINE_TAGS.includes(node.nodeName)) {
43
+ var blockLines = _extractCodeLines(node.childNodes);
44
+ if (blockLines.length === 0) {
45
+ lines.push('');
46
+ return;
47
+ }
48
+ if (lines.length === 1 && lines[0] === '') {
49
+ lines = blockLines;
50
+ } else {
51
+ lines[lines.length - 1] += blockLines[0] || '';
52
+ if (blockLines.length > 1) {
53
+ var _lines;
54
+ (_lines = lines).push.apply(_lines, (0, _toConsumableArray2["default"])(blockLines.slice(1)));
55
+ }
56
+ }
57
+ var nextNode = node.nextSibling;
58
+ var hasNextBlockSibling = nextNode && BLOCK_LINE_TAGS.includes(nextNode.nodeName);
59
+ if (hasNextBlockSibling && blockLines[blockLines.length - 1] !== '') {
60
+ lines.push('');
61
+ }
62
+ return;
63
+ }
64
+ var nestedLines = _extractCodeLines(node.childNodes);
65
+ if (nestedLines.length === 0) {
66
+ return;
67
+ }
68
+ if (lines.length === 1 && lines[0] === '') {
69
+ lines = nestedLines;
70
+ return;
71
+ }
72
+ lines[lines.length - 1] += nestedLines[0] || '';
73
+ if (nestedLines.length > 1) {
74
+ var _lines2;
75
+ (_lines2 = lines).push.apply(_lines2, (0, _toConsumableArray2["default"])(nestedLines.slice(1)));
76
+ }
77
+ });
78
+ return lines;
79
+ };
80
+ var buildCodeLines = function buildCodeLines(element) {
81
+ var lines = _extractCodeLines(element.childNodes);
82
+ return lines.map(function (text) {
83
+ return {
84
+ id: _slugid["default"].nice(),
85
+ type: _constants.CODE_LINE,
86
+ children: [{
87
+ id: _slugid["default"].nice(),
88
+ text: text
89
+ }]
90
+ };
91
+ });
92
+ };
11
93
  var codeBlockRule = function codeBlockRule(element, parseChild) {
12
94
  var nodeName = element.nodeName,
13
95
  childNodes = element.childNodes;
14
96
  if (nodeName === 'PRE') {
15
- var children = Array.from(childNodes).filter(function (item) {
16
- return item.nodeName === 'CODE';
17
- });
18
- var codeChild = children[0];
19
- if (codeChild) {
20
- var lang = codeChild.getAttribute('lang');
21
- lang = (0, _helper.genCodeLangs)().find(function (item) {
22
- return item.value === lang;
23
- }) || 'plaintext';
24
- return {
25
- id: _slugid["default"].nice(),
26
- language: lang,
27
- type: _constants.CODE_BLOCK,
28
- children: parseChild(children)
29
- };
30
- } else {
31
- var _lang = 'plaintext';
32
- var content = childNodes[0].textContent;
33
- var textArr = content.split('\n').filter(Boolean);
34
- var _children = textArr.map(function (text) {
35
- return {
36
- id: _slugid["default"].nice(),
37
- type: _constants.CODE_LINE,
38
- children: [{
39
- id: _slugid["default"].nice(),
40
- text: text
41
- }]
42
- };
43
- });
44
- return {
45
- id: _slugid["default"].nice(),
46
- language: _lang,
47
- type: _constants.CODE_BLOCK,
48
- children: _children
49
- };
50
- }
97
+ var codeChild = element.querySelector('code');
98
+ var lang = codeChild === null || codeChild === void 0 ? void 0 : codeChild.getAttribute('lang');
99
+ lang = (0, _helper.genCodeLangs)().find(function (item) {
100
+ return item.value === lang;
101
+ }) || 'plaintext';
102
+ return {
103
+ id: _slugid["default"].nice(),
104
+ language: lang,
105
+ type: _constants.CODE_BLOCK,
106
+ children: buildCodeLines(codeChild || element)
107
+ };
51
108
  }
52
109
  if (nodeName === 'CODE' && element.parentElement.nodeName === 'PRE') {
53
110
  var childIsP = Array.from(childNodes).every(function (n) {
@@ -65,8 +122,8 @@ var codeBlockRule = function codeBlockRule(element, parseChild) {
65
122
  };
66
123
  });
67
124
  }
68
- var _content = element.textContent;
69
- var hasNewLine = _content.indexOf('\n') > -1;
125
+ var content = element.textContent;
126
+ var hasNewLine = content.indexOf('\n') > -1;
70
127
  if (!hasNewLine) {
71
128
  return {
72
129
  id: _slugid["default"].nice(),
@@ -77,7 +134,7 @@ var codeBlockRule = function codeBlockRule(element, parseChild) {
77
134
  }]
78
135
  };
79
136
  }
80
- var codes = _content.split('\n').filter(Boolean);
137
+ var codes = content.split('\n').filter(Boolean);
81
138
  return codes.map(function (item) {
82
139
  return {
83
140
  id: _slugid["default"].nice(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "3.0.170",
3
+ "version": "3.0.172",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -72,5 +72,5 @@
72
72
  "publishConfig": {
73
73
  "access": "public"
74
74
  },
75
- "gitHead": "9d509cd4708045eb29f2cf98f02f657e8579cd51"
75
+ "gitHead": "01eab66e5ed1a93b11abb9f92d927ad424bca4db"
76
76
  }