@adobe/helix-markdown-support 4.0.0 → 4.0.3
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 +21 -0
- package/package.json +1 -1
- package/src/gridtable/mdast2hast-handler.js +19 -1
- package/src/gridtable/to-markdown.js +35 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
## [4.0.3](https://github.com/adobe/helix-markdown-support/compare/v4.0.2...v4.0.3) (2022-09-05)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* more improvements ([7d348e0](https://github.com/adobe/helix-markdown-support/commit/7d348e0cfdde11ab04f26c40fa7f9739787b9030))
|
|
7
|
+
|
|
8
|
+
## [4.0.2](https://github.com/adobe/helix-markdown-support/compare/v4.0.1...v4.0.2) (2022-09-03)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* empty cell with v-align throws ([#132](https://github.com/adobe/helix-markdown-support/issues/132)) ([14510b2](https://github.com/adobe/helix-markdown-support/commit/14510b27fbed96c2845527f3cb4c7e7896e1bf9c))
|
|
14
|
+
|
|
15
|
+
## [4.0.1](https://github.com/adobe/helix-markdown-support/compare/v4.0.0...v4.0.1) (2022-09-02)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* clean cell contents ([#131](https://github.com/adobe/helix-markdown-support/issues/131)) ([f422e97](https://github.com/adobe/helix-markdown-support/commit/f422e97ac0094f0c3448eba88c68476c8f3415cd))
|
|
21
|
+
|
|
1
22
|
# [4.0.0](https://github.com/adobe/helix-markdown-support/compare/v3.1.8...v4.0.0) (2022-09-02)
|
|
2
23
|
|
|
3
24
|
|
package/package.json
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
import { all } from 'mdast-util-to-hast';
|
|
13
|
+
import { CONTINUE, SKIP, visit } from 'unist-util-visit';
|
|
13
14
|
import {
|
|
14
15
|
TYPE_BODY, TYPE_CELL, TYPE_FOOTER, TYPE_HEADER, TYPE_ROW,
|
|
15
16
|
} from './types.js';
|
|
@@ -33,7 +34,24 @@ function handleRow(h, node, cellElementName) {
|
|
|
33
34
|
props[p] = child[p];
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
+
// if cell contains only 1 single paragraph, unwrap it
|
|
38
|
+
if (child.children?.length === 1 && child.children[0].type === 'paragraph') {
|
|
39
|
+
child.children = child.children[0].children;
|
|
40
|
+
}
|
|
41
|
+
const cell = h(child, cellElementName, props, all(h, child));
|
|
42
|
+
cells.push(cell);
|
|
43
|
+
|
|
44
|
+
// clean text elements
|
|
45
|
+
visit(cell, (n) => {
|
|
46
|
+
if (n.tagName === 'code') {
|
|
47
|
+
return SKIP;
|
|
48
|
+
}
|
|
49
|
+
if (n.type === 'text') {
|
|
50
|
+
// eslint-disable-next-line no-param-reassign
|
|
51
|
+
n.value = n.value.replace(/\r?\n/mg, ' ');
|
|
52
|
+
}
|
|
53
|
+
return CONTINUE;
|
|
54
|
+
});
|
|
37
55
|
}
|
|
38
56
|
}
|
|
39
57
|
|
|
@@ -41,7 +41,7 @@ function spanWidth(cols, idx, cell) {
|
|
|
41
41
|
export function lineWrapTextHandler(node, parent, context, safeOptions) {
|
|
42
42
|
const textNode = {
|
|
43
43
|
...node,
|
|
44
|
-
value: node.value.replace(
|
|
44
|
+
value: node.value.replace(/[ \t\v\r\n]/g, ' '),
|
|
45
45
|
};
|
|
46
46
|
let value = textHandler(textNode, parent, context, safeOptions);
|
|
47
47
|
const { lineWidth } = context.options;
|
|
@@ -123,11 +123,9 @@ class Table {
|
|
|
123
123
|
row = row || this.lastRow;
|
|
124
124
|
row.cells.push(cell);
|
|
125
125
|
for (let i = 1; i < cell.colSpan; i += 1) {
|
|
126
|
-
row.cells.push({
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (cell.colSpan > 1) {
|
|
130
|
-
row.cells[row.cells.length - 1].align = cell.align;
|
|
126
|
+
row.cells.push({
|
|
127
|
+
align: cell.align,
|
|
128
|
+
});
|
|
131
129
|
}
|
|
132
130
|
}
|
|
133
131
|
|
|
@@ -166,6 +164,7 @@ class Table {
|
|
|
166
164
|
toMarkdown(context) {
|
|
167
165
|
// populate the matrix with the rowspans and compute max width
|
|
168
166
|
// (the empty cells for the colspans are already created during insert).
|
|
167
|
+
let realNumCols = 0;
|
|
169
168
|
const cols = [];
|
|
170
169
|
for (let y = 0; y < this.rows.length; y += 1) {
|
|
171
170
|
const row = this.rows[y];
|
|
@@ -173,11 +172,14 @@ class Table {
|
|
|
173
172
|
let col = cols[x];
|
|
174
173
|
if (!col) {
|
|
175
174
|
col = {
|
|
176
|
-
width:
|
|
175
|
+
width: 3,
|
|
177
176
|
};
|
|
178
177
|
cols[x] = col;
|
|
179
178
|
}
|
|
180
179
|
const cell = row.cells[x];
|
|
180
|
+
if (cell.tree) {
|
|
181
|
+
realNumCols = Math.max(realNumCols, x + 1);
|
|
182
|
+
}
|
|
181
183
|
if (cell.rowSpan > 1) {
|
|
182
184
|
// insert colspan amount of null cells below
|
|
183
185
|
for (let i = 1; i < cell.rowSpan; i += 1) {
|
|
@@ -190,6 +192,25 @@ class Table {
|
|
|
190
192
|
}
|
|
191
193
|
}
|
|
192
194
|
}
|
|
195
|
+
|
|
196
|
+
// now trim tailing colspans
|
|
197
|
+
if (cols.length > realNumCols) {
|
|
198
|
+
cols.length = realNumCols;
|
|
199
|
+
for (const { cells } of this.rows) {
|
|
200
|
+
if (cells.length > realNumCols) {
|
|
201
|
+
cells.length = realNumCols;
|
|
202
|
+
// find trailing colspan
|
|
203
|
+
let x = cells.length - 1;
|
|
204
|
+
while (x >= 0 && !cells[x].tree) {
|
|
205
|
+
x -= 1;
|
|
206
|
+
}
|
|
207
|
+
if (x >= 0) {
|
|
208
|
+
cells[x].colSpan = realNumCols - x;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
193
214
|
const numCols = cols.length;
|
|
194
215
|
|
|
195
216
|
// add empty cells if needed
|
|
@@ -220,6 +241,10 @@ class Table {
|
|
|
220
241
|
const col = cols[x + idx];
|
|
221
242
|
col.width = Math.max(col.width, avgColWidth);
|
|
222
243
|
}
|
|
244
|
+
// if valign, the col needs to be at least 4 (3 + delim) wide
|
|
245
|
+
if (cell.valign) {
|
|
246
|
+
cols[x].width = Math.max(4, cols[x].width);
|
|
247
|
+
}
|
|
223
248
|
}
|
|
224
249
|
}
|
|
225
250
|
}
|
|
@@ -381,17 +406,18 @@ class Table {
|
|
|
381
406
|
}
|
|
382
407
|
|
|
383
408
|
// add last grid line
|
|
409
|
+
const d = this.rows.length === this.headerSize ? '=' : '-'; // special case: only header
|
|
384
410
|
const grid = [];
|
|
385
411
|
const lastRow = this.rows[this.rows.length - 1];
|
|
386
412
|
for (let x = 0; x < cols.length; x += 1) {
|
|
387
413
|
const col = cols[x];
|
|
388
414
|
// if the cell above was a colspan, and we are on the last line, don't draw the `+`
|
|
389
415
|
const aboveCell = lastRow.cells[x];
|
|
390
|
-
let c = aboveCell.tree || aboveCell.linked ? gtVLineEnds :
|
|
416
|
+
let c = aboveCell.tree || aboveCell.linked ? gtVLineEnds : d;
|
|
391
417
|
if (x === 0) {
|
|
392
418
|
c = '+';
|
|
393
419
|
}
|
|
394
|
-
grid.push(`${c}${
|
|
420
|
+
grid.push(`${c}${d.repeat(col.width - 1)}`);
|
|
395
421
|
}
|
|
396
422
|
lines.push(`${grid.join('')}+`);
|
|
397
423
|
|