@adobe/helix-markdown-support 6.2.0 → 6.2.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.2.1](https://github.com/adobe/helix-markdown-support/compare/v6.2.0...v6.2.1) (2023-07-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * no whitespace around sub/sup ([1754c65](https://github.com/adobe/helix-markdown-support/commit/1754c65ef5990c5fc76ae4fe4c6308d9ed58f930))
7
+
1
8
  # [6.2.0](https://github.com/adobe/helix-markdown-support/compare/v6.1.3...v6.2.0) (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.0",
3
+ "version": "6.2.1",
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,10 @@ export function isFormat(type) {
21
21
  || type === 'underline';
22
22
  }
23
23
 
24
+ function isSnug(type) {
25
+ return type === 'superscript' || type === 'subscript';
26
+ }
27
+
24
28
  /**
25
29
  * Sanitizes text:
26
30
  * - collapses consecutive formats
@@ -105,7 +109,7 @@ export default function sanitizeTextAndFormats(tree) {
105
109
 
106
110
  // ensure that text before format has trailing whitespace
107
111
  const prev = siblings[index - 1];
108
- if (prev?.type === 'text') {
112
+ if (prev?.type === 'text' && !isSnug(node.type)) {
109
113
  const code = prev.value.charCodeAt(prev.value.length - 1);
110
114
  if (!asciiPunctuation(code) && !markdownSpace(code) && !unicodePunctuation(code)) {
111
115
  prev.value += ' ';
@@ -114,7 +118,7 @@ export default function sanitizeTextAndFormats(tree) {
114
118
 
115
119
  // ensure that text after format has leading whitespace
116
120
  const next = siblings[index + 1];
117
- if (children.length && next?.type === 'text') {
121
+ if (children.length && next?.type === 'text' && !isSnug(node.type)) {
118
122
  const code = next.value.charCodeAt(0);
119
123
  if (!asciiPunctuation(code) && !markdownSpace(code) && !unicodePunctuation(code)) {
120
124
  next.value = ` ${next.value}`;