@adobe/helix-markdown-support 6.2.1 → 6.3.0

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.
@@ -2,7 +2,7 @@ version: 2.1
2
2
  executors:
3
3
  node18:
4
4
  docker:
5
- - image: cimg/node:18.16
5
+ - image: cimg/node:18.17
6
6
 
7
7
  orbs:
8
8
  codecov: codecov/codecov@3.2.5
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [6.3.0](https://github.com/adobe/helix-markdown-support/compare/v6.2.1...v6.3.0) (2023-07-27)
2
+
3
+
4
+ ### Features
5
+
6
+ * sort formats ([#206](https://github.com/adobe/helix-markdown-support/issues/206)) ([b734a35](https://github.com/adobe/helix-markdown-support/commit/b734a359e764ac4630b7b2418c0e71e342ad52b6))
7
+
1
8
  ## [6.2.1](https://github.com/adobe/helix-markdown-support/compare/v6.2.0...v6.2.1) (2023-07-18)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-markdown-support",
3
- "version": "6.2.1",
3
+ "version": "6.3.0",
4
4
  "description": "Helix Markdown Support",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -25,6 +25,55 @@ function isSnug(type) {
25
25
  return type === 'superscript' || type === 'subscript';
26
26
  }
27
27
 
28
+ const FLOW_SORT_ORDER = [
29
+ 'delete',
30
+ 'strong',
31
+ 'emphasis',
32
+ 'link',
33
+ 'underline',
34
+ 'subscript',
35
+ 'superscript',
36
+ ];
37
+
38
+ export function sort(tree) {
39
+ if (!tree.children) {
40
+ return;
41
+ }
42
+ for (let i = 0; i < tree.children.length; i += 1) {
43
+ let node = tree.children[i];
44
+ let key = FLOW_SORT_ORDER.indexOf(node.type);
45
+ if (key >= 0) {
46
+ // find the longest chain of formats
47
+ const chain = [];
48
+ while (key >= 0) {
49
+ chain.push({ node, key });
50
+ node = node.children?.length === 1 ? node.children[0] : null;
51
+ key = node ? FLOW_SORT_ORDER.indexOf(node?.type) : -1;
52
+ }
53
+ if (chain.length > 1) {
54
+ // remember children of last node in chain
55
+ const lastChildren = chain[chain.length - 1].node.children;
56
+
57
+ // sort chain
58
+ chain.sort((n0, n1) => (n0.key - n1.key));
59
+
60
+ // relink chain
61
+ for (let j = 0; j < chain.length - 1; j += 1) {
62
+ chain[j].node.children = [chain[j + 1].node];
63
+ }
64
+ chain[chain.length - 1].node.children = lastChildren;
65
+
66
+ // eslint-disable-next-line no-param-reassign
67
+ tree.children[i] = chain[0].node;
68
+ }
69
+ // continue on last node
70
+ sort(chain[chain.length - 1].node);
71
+ } else {
72
+ sort(node);
73
+ }
74
+ }
75
+ }
76
+
28
77
  /**
29
78
  * Sanitizes text:
30
79
  * - collapses consecutive formats
@@ -199,6 +248,6 @@ export default function sanitizeTextAndFormats(tree) {
199
248
  return false;
200
249
  }
201
250
  prune(tree);
202
-
251
+ sort(tree);
203
252
  return tree;
204
253
  }