@kerebron/extension-odt 0.5.3 → 0.5.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.
Files changed (43) hide show
  1. package/esm/ExtensionOdt.d.ts.map +1 -1
  2. package/esm/ExtensionOdt.js +12 -7
  3. package/esm/ExtensionOdt.js.map +1 -1
  4. package/esm/OdtParser.d.ts +36 -2
  5. package/esm/OdtParser.d.ts.map +1 -1
  6. package/esm/OdtParser.js +20 -20
  7. package/esm/OdtParser.js.map +1 -1
  8. package/esm/node_handlers/basic_node_handlers.d.ts +1 -0
  9. package/esm/node_handlers/basic_node_handlers.d.ts.map +1 -1
  10. package/esm/node_handlers/basic_node_handlers.js +76 -6
  11. package/esm/node_handlers/basic_node_handlers.js.map +1 -1
  12. package/esm/node_handlers/list_node_handlers.d.ts.map +1 -1
  13. package/esm/node_handlers/list_node_handlers.js +21 -11
  14. package/esm/node_handlers/list_node_handlers.js.map +1 -1
  15. package/esm/postprocess/convertCodeParagraphsToCodeBlocks.d.ts.map +1 -1
  16. package/esm/postprocess/convertCodeParagraphsToCodeBlocks.js +19 -8
  17. package/esm/postprocess/convertCodeParagraphsToCodeBlocks.js.map +1 -1
  18. package/esm/postprocess/convertMathMl.js +1 -1
  19. package/esm/postprocess/convertMathMl.js.map +1 -1
  20. package/esm/postprocess/fixListsLevels.d.ts +3 -0
  21. package/esm/postprocess/fixListsLevels.d.ts.map +1 -0
  22. package/esm/postprocess/fixListsLevels.js +62 -0
  23. package/esm/postprocess/fixListsLevels.js.map +1 -0
  24. package/esm/postprocess/mergeCodeBlocks.d.ts.map +1 -1
  25. package/esm/postprocess/mergeCodeBlocks.js +15 -4
  26. package/esm/postprocess/mergeCodeBlocks.js.map +1 -1
  27. package/esm/postprocess/postProcess.d.ts.map +1 -1
  28. package/esm/postprocess/postProcess.js +3 -2
  29. package/esm/postprocess/postProcess.js.map +1 -1
  30. package/esm/postprocess/removeUnusedBookmarks.d.ts.map +1 -1
  31. package/esm/postprocess/removeUnusedBookmarks.js +0 -1
  32. package/esm/postprocess/removeUnusedBookmarks.js.map +1 -1
  33. package/package.json +3 -3
  34. package/src/ExtensionOdt.ts +13 -12
  35. package/src/OdtParser.ts +74 -26
  36. package/src/node_handlers/basic_node_handlers.ts +80 -6
  37. package/src/node_handlers/list_node_handlers.ts +42 -18
  38. package/src/postprocess/convertCodeParagraphsToCodeBlocks.ts +21 -8
  39. package/src/postprocess/convertMathMl.ts +1 -1
  40. package/src/postprocess/fixListsLevels.ts +93 -0
  41. package/src/postprocess/mergeCodeBlocks.ts +20 -4
  42. package/src/postprocess/postProcess.ts +3 -1
  43. package/src/postprocess/removeUnusedBookmarks.ts +0 -1
@@ -19,6 +19,15 @@ export const convertCodeParagraphsToCodeBlocks: Command = (
19
19
  let codeSize = 0;
20
20
  for (let childNo = 0; childNo < node.childCount; childNo++) {
21
21
  const child = node.child(childNo);
22
+ const monospaced = child.marks.some((mark) =>
23
+ mark.type === markCodeType
24
+ );
25
+
26
+ if (child.type.name === 'node_bookmark') {
27
+ codeText += '\n';
28
+ codeSize += child.nodeSize;
29
+ continue;
30
+ }
22
31
 
23
32
  if (child.type.name === 'br') {
24
33
  codeText += '\n';
@@ -26,7 +35,7 @@ export const convertCodeParagraphsToCodeBlocks: Command = (
26
35
  continue;
27
36
  }
28
37
 
29
- if (child.marks.some((mark) => mark.type === markCodeType)) {
38
+ if (monospaced) {
30
39
  codeText += child.text || child.textBetween(0, child.content.size);
31
40
  codeSize += child.nodeSize;
32
41
  continue;
@@ -39,18 +48,22 @@ export const convertCodeParagraphsToCodeBlocks: Command = (
39
48
  const startPos = tr.mapping.map(pos);
40
49
  const endPos = tr.mapping.map(pos + 1 + codeSize);
41
50
 
42
- const textNode = schema.text(codeText);
43
- const codeBlock = schema.nodes.code_block.createAndFill(null, [
44
- textNode,
45
- ]);
51
+ if (codeText) {
52
+ const textNode = schema.text(codeText);
53
+ const codeBlock = schema.nodes.code_block.createAndFill(null, [
54
+ textNode,
55
+ ]);
46
56
 
47
- if (codeBlock) {
48
- tr = tr.replaceRangeWith(startPos, endPos, codeBlock);
57
+ if (codeBlock) {
58
+ tr.replaceRangeWith(startPos, endPos, codeBlock);
59
+ }
60
+ } else {
61
+ tr.replace(startPos, endPos);
49
62
  }
50
63
  }
51
64
 
52
65
  if (codeSize > 0 && codeSize + 2 === node.nodeSize) {
53
- // tr = tr.deleteRange(tr.mapping.map(pos), tr.mapping.map(pos))
66
+ // tr.deleteRange(tr.mapping.map(pos), tr.mapping.map(pos))
54
67
  }
55
68
  }
56
69
  });
@@ -51,5 +51,5 @@ export const convertMathMl: Command = (state, dispatch): boolean => {
51
51
  dispatch(tr);
52
52
  }
53
53
 
54
- return true;
54
+ return tr.docChanged;
55
55
  };
@@ -0,0 +1,93 @@
1
+ import { Fragment, Node } from 'prosemirror-model';
2
+ import { Command } from 'prosemirror-state';
3
+
4
+ const ANY_LIST = ['ordered_list', 'bullet_list'];
5
+
6
+ type LevelMargins = Array<number>;
7
+
8
+ // Related tests:
9
+ // test ./example-document.md
10
+ export const fixListsLevels: Command = (state, dispatch): boolean => {
11
+ const doc: Node = state.doc;
12
+ const schema = state.schema;
13
+ const bulletListType = schema.nodes.bullet_list;
14
+ const listItemType = schema.nodes.list_item;
15
+ const paragraphType = schema.nodes.paragraph;
16
+
17
+ const tr = state.tr;
18
+
19
+ let level = 0;
20
+
21
+ let prevList: LevelMargins = [];
22
+ let curList: LevelMargins = [];
23
+ let fakeLevelGenerated = 0;
24
+
25
+ function walk(
26
+ node: Node,
27
+ pos = 0,
28
+ depth = 0,
29
+ ) {
30
+ if (!ANY_LIST.includes(node.type.name)) {
31
+ node.forEach((child, offset) => {
32
+ walk(child, pos + offset + 1, depth + 1);
33
+ });
34
+ } else {
35
+ const marginLeft = node.attrs['odtMarginLeft'] || 0;
36
+ if (level === 0) {
37
+ curList = [];
38
+ fakeLevelGenerated = 0;
39
+ if (prevList.length >= level) {
40
+ for (let i = 0; i < prevList.length; i++) {
41
+ if (
42
+ fakeLevelGenerated < prevList.length &&
43
+ marginLeft > prevList[fakeLevelGenerated]
44
+ ) {
45
+ curList[level + fakeLevelGenerated] =
46
+ prevList[fakeLevelGenerated];
47
+ fakeLevelGenerated++;
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ curList[level + fakeLevelGenerated] = marginLeft;
54
+
55
+ level++;
56
+
57
+ node.forEach((child, offset) => {
58
+ walk(child, pos + offset + 1, depth + 1);
59
+ });
60
+
61
+ level--;
62
+
63
+ if (level === 0 && !node.attrs.toc) {
64
+ prevList = curList;
65
+ for (let i = 0; i < fakeLevelGenerated; i++) {
66
+ const wrapper = bulletListType.create(
67
+ { type: 'none' },
68
+ listItemType.create(
69
+ null,
70
+ Fragment.from([
71
+ paragraphType.create(),
72
+ node,
73
+ ]),
74
+ ),
75
+ );
76
+ tr.replaceWith(
77
+ tr.mapping.map(pos),
78
+ tr.mapping.map(pos + node.nodeSize),
79
+ wrapper,
80
+ );
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ walk(doc);
87
+
88
+ if (dispatch) {
89
+ dispatch(tr);
90
+ }
91
+
92
+ return tr.docChanged;
93
+ };
@@ -40,7 +40,6 @@ export const mergeCodeBlocks: Command = (state, dispatch): boolean => {
40
40
  break;
41
41
  }
42
42
 
43
- const currentPos = nextPos;
44
43
  nextPos += next.nodeSize;
45
44
 
46
45
  if (next.type === paraBlockType && next.childCount === 0) {
@@ -49,7 +48,10 @@ export const mergeCodeBlocks: Command = (state, dispatch): boolean => {
49
48
  continue;
50
49
  }
51
50
 
52
- if (next.type === codeBlockType && node.attrs.lang === next.attrs.lang) {
51
+ if (
52
+ next.type === codeBlockType && node.attrs.lang === next.attrs.lang &&
53
+ node.attrs.lang !== 'mathml'
54
+ ) {
53
55
  const text = next.text || next.textBetween(0, next.content.size);
54
56
  codeTexts.push(text.endsWith('\n') ? text : text + '\n');
55
57
 
@@ -64,9 +66,23 @@ export const mergeCodeBlocks: Command = (state, dispatch): boolean => {
64
66
  const startPos = tr.mapping.map(offset);
65
67
  const endPos = tr.mapping.map(offset + codeSize);
66
68
 
67
- const codeText = codeTexts.join('').replace(/\n+$/gm, '\n');
69
+ let codeText = codeTexts.join('').replace(/\n+$/gm, '\n');
70
+
71
+ const lines = codeText.split('\n');
72
+ let emptyCount = 0;
73
+ for (emptyCount = 0; emptyCount < lines.length; emptyCount++) {
74
+ if (lines[emptyCount].trim()) {
75
+ break;
76
+ }
77
+ }
78
+ lines.splice(0, emptyCount);
79
+
80
+ codeText = lines.join('\n');
81
+
68
82
  const textNode = schema.text(codeText);
69
- const codeBlock = schema.nodes.code_block.createAndFill(null, [textNode]);
83
+ const codeBlock = schema.nodes.code_block.createAndFill({
84
+ lang: node.attrs.lang,
85
+ }, [textNode]);
70
86
 
71
87
  if (codeBlock) {
72
88
  tr = tr.replaceRangeWith(startPos, endPos, codeBlock);
@@ -6,6 +6,7 @@ import { removeUnusedBookmarks } from './removeUnusedBookmarks.js';
6
6
  import { fixContinuedLists } from './fixContinuedLists.js';
7
7
  import { convertMathMl } from './convertMathMl.js';
8
8
  import { mergeCodeBlocks } from './mergeCodeBlocks.js';
9
+ import { fixListsLevels } from './fixListsLevels.js';
9
10
 
10
11
  export interface PostProcessConfig {
11
12
  doc: Node;
@@ -16,8 +17,9 @@ export function getDefaultsPostProcessFilters(
16
17
  { doc, filesMap }: PostProcessConfig,
17
18
  ): Array<Command> {
18
19
  return [
19
- removeUnusedBookmarks,
20
+ // removeUnusedBookmarks,
20
21
  convertCodeParagraphsToCodeBlocks,
22
+ fixListsLevels,
21
23
  fixContinuedLists,
22
24
  convertMathMl,
23
25
  mergeCodeBlocks,
@@ -14,7 +14,6 @@ export const removeUnusedBookmarks: Command = (state, dispatch): boolean => {
14
14
  depth = 0,
15
15
  ) {
16
16
  if (node.type === bookmarkNodeType) {
17
- console.log('del');
18
17
  tr = tr.delete(
19
18
  tr.mapping.map(pos - 1),
20
19
  tr.mapping.map(pos + node.nodeSize - 1),