@adobe/helix-markdown-support 6.2.0 → 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,17 @@
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
+
8
+ ## [6.2.1](https://github.com/adobe/helix-markdown-support/compare/v6.2.0...v6.2.1) (2023-07-18)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * no whitespace around sub/sup ([1754c65](https://github.com/adobe/helix-markdown-support/commit/1754c65ef5990c5fc76ae4fe4c6308d9ed58f930))
14
+
1
15
  # [6.2.0](https://github.com/adobe/helix-markdown-support/compare/v6.1.3...v6.2.0) (2023-07-18)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-markdown-support",
3
- "version": "6.2.0",
3
+ "version": "6.3.0",
4
4
  "description": "Helix Markdown Support",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -23,3 +23,4 @@ export { default as sanitizeTextAndFormats } from './mdast-sanitize-text-and-for
23
23
  export { default as imageReferences } from './mdast-image-references.js';
24
24
  export { default as dereference } from './mdast-dereference.js';
25
25
  export { default as remarkGfmNoLink } from './remark-gfm-nolink.js';
26
+ export { default as renderHtmlFormats } from './mdast-render-html-formats.js';
@@ -0,0 +1,58 @@
1
+ /*
2
+ * Copyright 2023 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { visit, CONTINUE } from 'unist-util-visit';
13
+
14
+ const FORMATS = {
15
+ subscript: {
16
+ open: '<sub>',
17
+ close: '</sub>',
18
+ },
19
+ superscript: {
20
+ open: '<sup>',
21
+ close: '</sup>',
22
+ },
23
+ underline: {
24
+ open: '<u>',
25
+ close: '</u>',
26
+ },
27
+ };
28
+
29
+ /**
30
+ * Renders the special html formats
31
+ *
32
+ * @param {object} tree
33
+ * @returns {object} The modified (original) tree.
34
+ */
35
+ export default function renderHtmlFormats(tree) {
36
+ visit(tree, (node, index, parent) => {
37
+ const { children: siblings = [] } = parent || {};
38
+ const fmt = FORMATS[node.type];
39
+ if (fmt) {
40
+ siblings.splice(
41
+ index,
42
+ 1,
43
+ {
44
+ type: 'html',
45
+ value: fmt.open,
46
+ },
47
+ ...node.children,
48
+ {
49
+ type: 'html',
50
+ value: fmt.close,
51
+ },
52
+ );
53
+ return index + node.children.length;
54
+ }
55
+ return CONTINUE;
56
+ });
57
+ return tree;
58
+ }
@@ -21,6 +21,59 @@ export function isFormat(type) {
21
21
  || type === 'underline';
22
22
  }
23
23
 
24
+ function isSnug(type) {
25
+ return type === 'superscript' || type === 'subscript';
26
+ }
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
+
24
77
  /**
25
78
  * Sanitizes text:
26
79
  * - collapses consecutive formats
@@ -105,7 +158,7 @@ export default function sanitizeTextAndFormats(tree) {
105
158
 
106
159
  // ensure that text before format has trailing whitespace
107
160
  const prev = siblings[index - 1];
108
- if (prev?.type === 'text') {
161
+ if (prev?.type === 'text' && !isSnug(node.type)) {
109
162
  const code = prev.value.charCodeAt(prev.value.length - 1);
110
163
  if (!asciiPunctuation(code) && !markdownSpace(code) && !unicodePunctuation(code)) {
111
164
  prev.value += ' ';
@@ -114,7 +167,7 @@ export default function sanitizeTextAndFormats(tree) {
114
167
 
115
168
  // ensure that text after format has leading whitespace
116
169
  const next = siblings[index + 1];
117
- if (children.length && next?.type === 'text') {
170
+ if (children.length && next?.type === 'text' && !isSnug(node.type)) {
118
171
  const code = next.value.charCodeAt(0);
119
172
  if (!asciiPunctuation(code) && !markdownSpace(code) && !unicodePunctuation(code)) {
120
173
  next.value = ` ${next.value}`;
@@ -195,6 +248,6 @@ export default function sanitizeTextAndFormats(tree) {
195
248
  return false;
196
249
  }
197
250
  prune(tree);
198
-
251
+ sort(tree);
199
252
  return tree;
200
253
  }