@adobe/helix-markdown-support 6.0.1 → 6.0.2

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.0.2](https://github.com/adobe/helix-markdown-support/compare/v6.0.1...v6.0.2) (2023-01-20)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add root phrasing helper ([#166](https://github.com/adobe/helix-markdown-support/issues/166)) ([4a875b5](https://github.com/adobe/helix-markdown-support/commit/4a875b545e191ad7a10e749256c1955972c5a08e))
7
+
1
8
  ## [6.0.1](https://github.com/adobe/helix-markdown-support/compare/v6.0.0...v6.0.1) (2023-01-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.0.1",
3
+ "version": "6.0.2",
4
4
  "description": "Helix Markdown Support",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -27,6 +27,7 @@
27
27
  "dependencies": {
28
28
  "hast-util-to-html": "8.0.4",
29
29
  "js-yaml": "4.1.0",
30
+ "mdast-util-phrasing": "3.0.0",
30
31
  "mdast-util-to-hast": "12.2.5",
31
32
  "micromark-util-character": "1.1.0",
32
33
  "micromark-util-symbol": "1.0.1",
package/src/index.js CHANGED
@@ -16,6 +16,7 @@ export { default as sanitizeHeading } from './mdast-sanitize-heading.js';
16
16
  export { default as suppressSpaceCode } from './mdast-suppress-spacecode.js';
17
17
  export { default as sanitizeFormats } from './mdast-sanitize-formats.js';
18
18
  export { default as fixCodeFlow } from './mdast-fix-code-flow.js';
19
+ export { default as fixRootPhrasing } from './mdast-fix-root-phrasing.js';
19
20
  export { default as sanitizeLinks } from './mdast-sanitize-links.js';
20
21
  export { default as sanitizeText } from './mdast-sanitize-text.js';
21
22
  export { default as sanitizeTextAndFormats } from './mdast-sanitize-text-and-formats.js';
@@ -0,0 +1,45 @@
1
+ /*
2
+ * Copyright 2021 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 { phrasing } from 'mdast-util-phrasing';
13
+
14
+ function wrap(children, from, to) {
15
+ const para = {
16
+ type: 'paragraph',
17
+ };
18
+ para.children = children.splice(from, to - from, para);
19
+ }
20
+
21
+ /**
22
+ * ensures that the root node has no phrasing children.
23
+ *
24
+ * @param {object} tree
25
+ * @returns {object} The modified (original) tree.
26
+ */
27
+ export default function fixRootPhrasing(tree) {
28
+ const { children } = tree;
29
+ let firstPhrasing = -1;
30
+ for (let i = 0; i < children.length; i += 1) {
31
+ if (phrasing(children[i])) {
32
+ if (firstPhrasing < 0) {
33
+ firstPhrasing = i;
34
+ }
35
+ } else if (firstPhrasing >= 0) {
36
+ wrap(children, firstPhrasing, i);
37
+ i = firstPhrasing;
38
+ firstPhrasing = -1;
39
+ }
40
+ }
41
+ if (firstPhrasing >= 0) {
42
+ wrap(children, firstPhrasing, children.length);
43
+ }
44
+ return tree;
45
+ }