@adobe/helix-markdown-support 6.1.3 → 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,17 @@
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
+
8
+ # [6.2.0](https://github.com/adobe/helix-markdown-support/compare/v6.1.3...v6.2.0) (2023-07-18)
9
+
10
+
11
+ ### Features
12
+
13
+ * support extended formats ([b1561dd](https://github.com/adobe/helix-markdown-support/commit/b1561ddfc13ddd8aae22a6f396d2d956afcd0a09))
14
+
1
15
  ## [6.1.3](https://github.com/adobe/helix-markdown-support/compare/v6.1.2...v6.1.3) (2023-06-27)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-markdown-support",
3
- "version": "6.1.3",
3
+ "version": "6.2.1",
4
4
  "description": "Helix Markdown Support",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -41,7 +41,7 @@
41
41
  "micromark-util-character": "1.2.0",
42
42
  "micromark-util-combine-extensions": "1.1.0",
43
43
  "micromark-util-symbol": "1.1.0",
44
- "unist-util-find": "1.0.3",
44
+ "unist-util-find": "1.0.4",
45
45
  "unist-util-visit": "4.1.2"
46
46
  },
47
47
  "mocha": {
@@ -59,14 +59,14 @@
59
59
  },
60
60
  "devDependencies": {
61
61
  "@adobe/eslint-config-helix": "2.0.2",
62
- "@adobe/remark-gridtables": "1.0.3",
62
+ "@adobe/remark-gridtables": "1.0.4",
63
63
  "@semantic-release/changelog": "6.0.3",
64
64
  "@semantic-release/git": "10.0.1",
65
65
  "c8": "8.0.0",
66
- "eslint": "8.43.0",
66
+ "eslint": "8.45.0",
67
67
  "husky": "8.0.3",
68
68
  "junit-report-builder": "3.0.1",
69
- "lint-staged": "13.2.2",
69
+ "lint-staged": "13.2.3",
70
70
  "mdast-builder": "1.1.1",
71
71
  "mocha": "10.2.0",
72
72
  "mocha-multi-reporters": "1.5.1",
@@ -76,7 +76,7 @@
76
76
  "remark-parse": "10.0.2",
77
77
  "remark-rehype": "10.1.0",
78
78
  "remark-stringify": "10.0.3",
79
- "semantic-release": "21.0.5",
79
+ "semantic-release": "21.0.7",
80
80
  "unified": "10.1.2",
81
81
  "unist-util-inspect": "7.0.2"
82
82
  },
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
+ }
@@ -10,6 +10,7 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
  import { visit, CONTINUE } from 'unist-util-visit';
13
+ import { isFormat } from './mdast-sanitize-text-and-formats.js';
13
14
 
14
15
  /**
15
16
  * Sanitizes text:
@@ -23,7 +24,7 @@ export default function sanitizeFormats(tree) {
23
24
  visit(tree, (node, index, parent) => {
24
25
  const { children: siblings = [] } = parent || {};
25
26
  const { children } = node;
26
- if (node.type === 'strong' || node.type === 'emphasis' || node.type === 'delete') {
27
+ if (isFormat(node.type)) {
27
28
  // remove empty nodes
28
29
  if (!children || !children.length) {
29
30
  siblings.splice(index, 1);
@@ -12,8 +12,17 @@
12
12
  import { visit, CONTINUE } from 'unist-util-visit';
13
13
  import { asciiPunctuation, markdownSpace, unicodePunctuation } from 'micromark-util-character';
14
14
 
15
- function isFormat(type) {
16
- return type === 'strong' || type === 'emphasis' || type === 'delete';
15
+ export function isFormat(type) {
16
+ return type === 'strong'
17
+ || type === 'emphasis'
18
+ || type === 'delete'
19
+ || type === 'superscript'
20
+ || type === 'subscript'
21
+ || type === 'underline';
22
+ }
23
+
24
+ function isSnug(type) {
25
+ return type === 'superscript' || type === 'subscript';
17
26
  }
18
27
 
19
28
  /**
@@ -100,7 +109,7 @@ export default function sanitizeTextAndFormats(tree) {
100
109
 
101
110
  // ensure that text before format has trailing whitespace
102
111
  const prev = siblings[index - 1];
103
- if (prev?.type === 'text') {
112
+ if (prev?.type === 'text' && !isSnug(node.type)) {
104
113
  const code = prev.value.charCodeAt(prev.value.length - 1);
105
114
  if (!asciiPunctuation(code) && !markdownSpace(code) && !unicodePunctuation(code)) {
106
115
  prev.value += ' ';
@@ -109,7 +118,7 @@ export default function sanitizeTextAndFormats(tree) {
109
118
 
110
119
  // ensure that text after format has leading whitespace
111
120
  const next = siblings[index + 1];
112
- if (children.length && next?.type === 'text') {
121
+ if (children.length && next?.type === 'text' && !isSnug(node.type)) {
113
122
  const code = next.value.charCodeAt(0);
114
123
  if (!asciiPunctuation(code) && !markdownSpace(code) && !unicodePunctuation(code)) {
115
124
  next.value = ` ${next.value}`;