@adobe/helix-md2docx 1.3.8 → 1.3.11

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,24 @@
1
+ ## [1.3.11](https://github.com/adobe/helix-md2docx/compare/v1.3.10...v1.3.11) (2022-03-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * handle row and colspan ([7077d4f](https://github.com/adobe/helix-md2docx/commit/7077d4f99957741885cdc1be39d6f23fa0cb88fa)), closes [#47](https://github.com/adobe/helix-md2docx/issues/47)
7
+
8
+ ## [1.3.10](https://github.com/adobe/helix-md2docx/compare/v1.3.9...v1.3.10) (2022-03-20)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **deps:** update dependency @adobe/helix-fetch to v3.0.7 ([f211f44](https://github.com/adobe/helix-md2docx/commit/f211f4483dd6687e310e98c8b9793f65fa2f1893))
14
+
15
+ ## [1.3.9](https://github.com/adobe/helix-md2docx/compare/v1.3.8...v1.3.9) (2022-03-18)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * **deps:** update dependency @adobe/helix-docx2md to v1.0.10 ([#42](https://github.com/adobe/helix-md2docx/issues/42)) ([d58c683](https://github.com/adobe/helix-md2docx/commit/d58c683cb1f9d9fb802fa7dba1366dfb36475a1c))
21
+
1
22
  ## [1.3.8](https://github.com/adobe/helix-md2docx/compare/v1.3.7...v1.3.8) (2022-03-18)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-md2docx",
3
- "version": "1.3.8",
3
+ "version": "1.3.11",
4
4
  "description": "Helix Service that converts markdown to word documents",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -27,10 +27,10 @@
27
27
  },
28
28
  "homepage": "https://github.com/adobe/helix-md2docx#readme",
29
29
  "dependencies": {
30
- "@adobe/helix-fetch": "3.0.6",
30
+ "@adobe/helix-fetch": "3.0.7",
31
31
  "@adobe/helix-markdown-support": "3.1.2",
32
32
  "@adobe/helix-shared-process-queue": "1.1.4",
33
- "@adobe/helix-docx2md": "1.0.9",
33
+ "@adobe/helix-docx2md": "1.0.10",
34
34
  "docx": "7.3.0",
35
35
  "hast-util-is-element": "2.1.2",
36
36
  "hast-util-to-mdast": "8.3.1",
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@adobe/eslint-config-helix": "1.3.2",
46
- "@adobe/helix-mediahandler": "1.0.11",
46
+ "@adobe/helix-mediahandler": "1.0.13",
47
47
  "@semantic-release/changelog": "6.0.1",
48
48
  "@semantic-release/exec": "6.0.3",
49
49
  "@semantic-release/git": "10.0.1",
@@ -57,7 +57,7 @@
57
57
  "fs-extra": "10.0.1",
58
58
  "husky": "7.0.4",
59
59
  "junit-report-builder": "3.0.0",
60
- "lint-staged": "12.3.5",
60
+ "lint-staged": "12.3.7",
61
61
  "mocha": "9.2.2",
62
62
  "mocha-multi-reporters": "1.5.1",
63
63
  "semantic-release": "19.0.2",
@@ -45,7 +45,15 @@ export default async function tableCell(ctx, node, parent, siblings) {
45
45
  content.push(new Paragraph({ alignment, children: leaves }));
46
46
  }
47
47
 
48
- return new TableCell({
48
+ const opts = {
49
49
  children: content,
50
- });
50
+ };
51
+
52
+ if (node.data?.colSpan) {
53
+ opts.columnSpan = node.data.colSpan;
54
+ }
55
+ if (node.data?.rowSpan) {
56
+ opts.rowSpan = node.data.rowSpan;
57
+ }
58
+ return new TableCell(opts);
51
59
  }
@@ -0,0 +1,46 @@
1
+ /*
2
+ * Copyright 2022 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
+
13
+ /*
14
+ copied and adapted (adding align) from
15
+ https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/handlers/table-cell.js
16
+ */
17
+
18
+ 'use strict';
19
+
20
+ import { all } from 'hast-util-to-mdast/lib/all.js';
21
+
22
+ export default function cell(h, node) {
23
+ const wrap = h.wrapText;
24
+
25
+ // eslint-disable-next-line no-param-reassign
26
+ h.wrapText = false;
27
+
28
+ const result = h(node, 'tableCell', all(h, node));
29
+
30
+ if (node.properties?.rowSpan || node.properties?.colSpan || node.properties?.align) {
31
+ const data = result.data || (result.data = {});
32
+ if (node.properties.rowSpan) {
33
+ data.rowSpan = node.properties.rowSpan;
34
+ }
35
+ if (node.properties.colSpan) {
36
+ data.colSpan = node.properties.colSpan;
37
+ }
38
+ if (node.properties.align) {
39
+ data.align = node.properties.align;
40
+ }
41
+ }
42
+ // eslint-disable-next-line no-param-reassign
43
+ h.wrapText = wrap;
44
+
45
+ return result;
46
+ }
@@ -12,56 +12,15 @@
12
12
 
13
13
  'use strict';
14
14
 
15
- import { convertElement } from 'hast-util-is-element';
16
- import { visit } from 'unist-util-visit';
17
15
  import { all } from 'hast-util-to-mdast/lib/all.js';
18
16
 
19
- const thead = convertElement('thead');
20
- const tr = convertElement('tr');
21
- const cell = convertElement(['th', 'td']);
22
-
23
17
  /*
24
18
  copied and adapted from
25
- https://github.com/syntax-tree/hast-util-to-mdast/blob/7.1.3/lib/handlers/table.js
19
+ https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/handlers/table.js
26
20
  */
27
21
 
28
- // Infer whether the HTML table has a head and how it aligns.
29
- function inspect(node) {
30
- let headless = true;
31
- const align = [];
32
- let rowIndex = 0;
33
- let cellIndex = 0;
34
-
35
- function visitor(child) {
36
- // If there is a `thead`, assume there is a header row.
37
- if (thead(child)) {
38
- headless = false;
39
- } else if (tr(child)) {
40
- rowIndex += 1;
41
- cellIndex = 0;
42
- } else if (cell(child)) {
43
- if (align[cellIndex] === undefined) {
44
- align[cellIndex] = child.properties.align || null;
45
- }
46
-
47
- // If there is a th in the first row, assume there is a header row.
48
- if (headless && rowIndex < 2 && child.tagName === 'th') {
49
- headless = false;
50
- }
51
-
52
- cellIndex += 1;
53
- return visit.SKIP;
54
- }
55
- return visit.CONTINUE;
56
- }
57
-
58
- visit(node, 'element', visitor);
59
-
60
- return { align, headless };
61
- }
62
-
63
22
  // Ensure the cells in a row are properly structured.
64
- function toCells(children, info) {
23
+ function toCells(children) {
65
24
  const nodes = [];
66
25
  let queue;
67
26
 
@@ -93,25 +52,14 @@ function toCells(children, info) {
93
52
  node.children = node.children.concat(queue);
94
53
  }
95
54
 
96
- // add empty cells if there are more in the table
97
- for (let index = nodes.length; index < info.align.length; index += 1) {
98
- nodes.push({ type: 'tableCell', children: [] });
99
- }
100
-
101
55
  return nodes;
102
56
  }
103
57
 
104
58
  // Ensure the rows are properly structured.
105
- function toRows(children, info) {
59
+ function toRows(children) {
106
60
  const nodes = [];
107
61
  let queue;
108
62
 
109
- // Add an empty header row.
110
- // we don't need extra header rows
111
- // if (info.headless) {
112
- // nodes.push({ type: 'tableRow', children: [] });
113
- // }
114
-
115
63
  children.forEach((node) => {
116
64
  if (node.type === 'tableRow') {
117
65
  if (queue) {
@@ -133,13 +81,46 @@ function toRows(children, info) {
133
81
  }
134
82
  nodes.forEach((node) => {
135
83
  // eslint-disable-next-line no-param-reassign
136
- node.children = toCells(node.children, info);
84
+ node.children = toCells(node.children);
137
85
  });
138
86
 
139
87
  return nodes;
140
88
  }
141
89
 
142
90
  export default function table(h, node) {
143
- const info = inspect(node);
144
- return h(node, 'table', { align: info.align }, toRows(all(h, node), info));
91
+ const mdNode = h(node, 'table', toRows(all(h, node)));
92
+
93
+ // clean up table in respect to row and colspan and compute alignments
94
+ mdNode.align = [];
95
+
96
+ // compute the number of cells in each row, respecting the row and col spans.
97
+ let maxCols = 0;
98
+ const pendingRowSpans = [];
99
+ for (const row of mdNode.children) {
100
+ row.numCols = pendingRowSpans.shift() || 0;
101
+ for (const cell of row.children) {
102
+ const rowSpan = Number.parseInt(cell.data?.rowSpan || '1', 10);
103
+ const colSpan = Number.parseInt(cell.data?.colSpan || '1', 10);
104
+ if (cell.data?.align && !mdNode.align[row.numCols]) {
105
+ mdNode.align[row.numCols] = cell.data.align;
106
+ }
107
+ row.numCols += colSpan;
108
+ if (rowSpan > 1) {
109
+ for (let i = 0; i < rowSpan - 1; i += 1) {
110
+ pendingRowSpans[i] = (pendingRowSpans[i] || 0) + colSpan;
111
+ }
112
+ }
113
+ }
114
+ maxCols = Math.max(maxCols, row.numCols);
115
+ }
116
+
117
+ // add empty cells if needed
118
+ for (const row of mdNode.children) {
119
+ for (let i = row.numCols; i < maxCols; i += 1) {
120
+ row.children.push({ type: 'tableCell', children: [] });
121
+ }
122
+ delete row.numCols;
123
+ }
124
+
125
+ return mdNode;
145
126
  }
@@ -15,6 +15,7 @@ import parse from 'rehype-parse';
15
15
  import { toMdast } from 'hast-util-to-mdast';
16
16
  // import inspect from 'unist-util-inspect';
17
17
  import tableHandler from './hast-table-handler.js';
18
+ import tableCellHandler from './hast-table-cell-handler.js';
18
19
 
19
20
  /**
20
21
  * Creates simple format handler
@@ -90,6 +91,8 @@ export default function sanitizeHtml(tree) {
90
91
  sup: formatHandler('superScript'),
91
92
  table: tableHandler,
92
93
  markdown: mdHandler(mdInserts),
94
+ th: tableCellHandler,
95
+ td: tableCellHandler,
93
96
  },
94
97
  });
95
98