@adobe/helix-markdown-support 6.3.0 → 6.3.1

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [6.3.1](https://github.com/adobe/helix-markdown-support/compare/v6.3.0...v6.3.1) (2023-08-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * properly handle whitespace after sort ([#211](https://github.com/adobe/helix-markdown-support/issues/211)) ([03bf02a](https://github.com/adobe/helix-markdown-support/commit/03bf02a249f594e03c5d523d876e0b6522ad6ed5))
7
+
1
8
  # [6.3.0](https://github.com/adobe/helix-markdown-support/compare/v6.2.1...v6.3.0) (2023-07-27)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-markdown-support",
3
- "version": "6.3.0",
3
+ "version": "6.3.1",
4
4
  "description": "Helix Markdown Support",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -58,20 +58,20 @@
58
58
  "node": ">=14"
59
59
  },
60
60
  "devDependencies": {
61
- "@adobe/eslint-config-helix": "2.0.2",
61
+ "@adobe/eslint-config-helix": "2.0.3",
62
62
  "@adobe/remark-gridtables": "1.0.4",
63
63
  "@semantic-release/changelog": "6.0.3",
64
64
  "@semantic-release/git": "10.0.1",
65
- "c8": "8.0.0",
66
- "eslint": "8.45.0",
65
+ "c8": "8.0.1",
66
+ "eslint": "8.47.0",
67
67
  "husky": "8.0.3",
68
68
  "junit-report-builder": "3.0.1",
69
- "lint-staged": "13.2.3",
69
+ "lint-staged": "13.3.0",
70
70
  "mdast-builder": "1.1.1",
71
71
  "mocha": "10.2.0",
72
72
  "mocha-multi-reporters": "1.5.1",
73
73
  "rehype-format": "4.0.1",
74
- "rehype-stringify": "9.0.3",
74
+ "rehype-stringify": "9.0.4",
75
75
  "remark-gfm": "3.0.1",
76
76
  "remark-parse": "10.0.2",
77
77
  "remark-rehype": "10.1.0",
@@ -74,22 +74,7 @@ export function sort(tree) {
74
74
  }
75
75
  }
76
76
 
77
- /**
78
- * Sanitizes text:
79
- * - collapses consecutive formats
80
- * - collapses consecutive text blocks
81
- * - trims ends of texts before break
82
- * - trims ends of texts at the end
83
- * - moves leading and trailing whitespaces out of formats
84
- * - ensures spaces after formats
85
- * - removes trailing breaks in containers
86
- * see https://github.com/micromark/micromark/issues/118#issuecomment-1238225086
87
- * - removes empty text blocks, formats, paragraphs
88
- *
89
- * @param {object} tree
90
- * @returns {object} The modified (original) tree.
91
- */
92
- export default function sanitizeTextAndFormats(tree) {
77
+ function collapse(tree) {
93
78
  visit(tree, (node, index, parent) => {
94
79
  const { children: siblings = [] } = parent || {};
95
80
  const { children = [] } = node;
@@ -105,7 +90,17 @@ export default function sanitizeTextAndFormats(tree) {
105
90
  siblings.splice(index, 1);
106
91
  return index - 1;
107
92
  }
93
+ }
94
+ return CONTINUE;
95
+ });
96
+ }
97
+
98
+ function whitespace(tree) {
99
+ visit(tree, (node, index, parent) => {
100
+ const { children: siblings = [] } = parent || {};
101
+ const { children = [] } = node;
108
102
 
103
+ if (isFormat(node.type)) {
109
104
  // check if last text block has trailing whitespace
110
105
  const last = children[children.length - 1];
111
106
  if (last?.type === 'text') {
@@ -176,7 +171,9 @@ export default function sanitizeTextAndFormats(tree) {
176
171
  }
177
172
  return CONTINUE;
178
173
  });
174
+ }
179
175
 
176
+ function cleanup(tree) {
180
177
  visit(tree, (node, index, parent) => {
181
178
  const { children: siblings = [] } = parent || {};
182
179
 
@@ -226,28 +223,56 @@ export default function sanitizeTextAndFormats(tree) {
226
223
 
227
224
  return CONTINUE;
228
225
  });
226
+ }
229
227
 
230
- // remove text, formats and paragraphs
231
- function prune(node) {
232
- const { children, type } = node;
233
- if (type === 'text') {
234
- return !node.value;
235
- }
236
- if (!children) {
237
- return false;
238
- }
239
- for (let i = 0; i < children.length; i += 1) {
240
- if (prune(children[i])) {
241
- children.splice(i, 1);
242
- i -= 1;
243
- }
244
- }
245
- if (type === 'paragraph' || isFormat(type)) {
246
- return children.length === 0;
247
- }
228
+ /**
229
+ * remove empty text, formats, paragraphs
230
+ * @param node
231
+ * @return {boolean}
232
+ */
233
+ function prune(node) {
234
+ const { children, type } = node;
235
+ if (type === 'text') {
236
+ return !node.value;
237
+ }
238
+ if (!children) {
248
239
  return false;
249
240
  }
241
+ for (let i = 0; i < children.length; i += 1) {
242
+ if (prune(children[i])) {
243
+ children.splice(i, 1);
244
+ i -= 1;
245
+ }
246
+ }
247
+ if (type === 'paragraph' || isFormat(type)) {
248
+ return children.length === 0;
249
+ }
250
+ return false;
251
+ }
252
+
253
+ /**
254
+ * Sanitizes text:
255
+ * - collapses consecutive formats
256
+ * - collapses consecutive text blocks
257
+ * - trims ends of texts before break
258
+ * - trims ends of texts at the end
259
+ * - moves leading and trailing whitespaces out of formats
260
+ * - ensures spaces after formats
261
+ * - removes trailing breaks in containers
262
+ * see https://github.com/micromark/micromark/issues/118#issuecomment-1238225086
263
+ * - removes empty text blocks, formats, paragraphs
264
+ *
265
+ * @param {object} tree
266
+ * @returns {object} The modified (original) tree.
267
+ */
268
+ export default function sanitizeTextAndFormats(tree) {
269
+ collapse(tree);
250
270
  prune(tree);
251
271
  sort(tree);
272
+ // collapse again, because sorting the nodes might have produce new collapsable siblings
273
+ collapse(tree);
274
+ whitespace(tree);
275
+ cleanup(tree);
276
+ prune(tree);
252
277
  return tree;
253
278
  }