Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect row- and colspan in inline table #613

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/mdast-table-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ export const TYPE_GT_ROW = 'gtRow';
export const TYPE_GT_CELL = 'gtCell';

function toGridCell(cell, state) {
return {
const node = {
...cell,
children: state.all(cell),
type: TYPE_GT_CELL,
};
if ('rowSpan' in node.properties) {
node.rowSpan = parseInt(node.properties.rowSpan, 10);
}
if ('colSpan' in node.properties) {
node.colSpan = parseInt(node.properties.colSpan, 10);
}
return node;
}

function toGridRow(row, state) {
Expand Down
10 changes: 8 additions & 2 deletions test/fixtures/block-with-table.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
</thead>
<tbody>
<tr>
<td>Table cell 1</td>
<td rowspan="2">Table cell 1</td>
<td>Table cell 2</td>
</tr>
<tr>
<td>Table cell 2</td>
</tr>
<tr>
<td colspan="2">Table cell 3-4</td>
</tr>
<tr>
<td>Table cell 3</td>
<td>
Expand Down Expand Up @@ -77,4 +83,4 @@ <h2> table with no rows</h2>
<footer></footer>
</body>

</html>
</html>
4 changes: 4 additions & 0 deletions test/fixtures/block-with-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ index
| | | Table head cell 1 | Table head cell 2 | |
| | +===================+==============================+ |
| | | Table cell 1 | Table cell 2 | |
| | | +------------------------------+ |
| | | | Table cell 2 | |
| | +--------------------------------------------------+ |
| | | Table cell 3-4 | |
| | +-------------------+------------------------------+ |
| | | Table cell 3 | **Table** cell 4 | |
| | | | | |
Expand Down
95 changes: 95 additions & 0 deletions test/roundtrip/roundtrip-fixtures/rowspan.input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<body>
<header></header>
<main>
<div>
<div class="columns">
<div>
<div data-valign="middle">
<h4 id="table-4-preparing-to-calculate-our-test-statistic"><strong>Table 4:
Preparing to calculate our test statistic</strong></h4>
<table>
<tbody>
<tr>
<td>Type of Movie</td>
<td>Snack</td>
<td>No Snacks</td>
</tr>
<tr>
<td rowspan="2">Action</td>
<td>Actual: 50<br><strong>Expected: 64.58</strong></td>
<td>Actual: 75<br><strong>Expected: 60.42</strong></td>
</tr>
<tr>
<td>
<p>Difference: 50 – 64.58 = -14.58</p>
<p>Squared Difference: 212.67</p>
<p>Divide by Expected: 212.67/64.58 = 3.29</p>
</td>
<td>
<p>Difference: 75 – 60.42 = 14.58</p>
<p>Squared Difference: 212.67</p>
<p>Divide by Expected: 212.67/60.42 = 3.52</p>
</td>
</tr>
<tr>
<td rowspan="2">Comedy</td>
<td>Actual: 125<br><strong>Expected 155</strong></td>
<td>Actual 175<br><strong>Expected 145</strong></td>
</tr>
<tr>
<td>
<p>Difference: 125 – 155 = -30</p>
<p>Squared Difference: 900</p>
<p>Divide by Expected: 900/155 = 5.81</p>
</td>
<td>
<p>Difference: 175 – 145 = 30</p>
<p>Squared Difference: 900</p>
<p>Divide by Expected: 900/145 = 6.21</p>
</td>
</tr>
<tr>
<td rowspan="2">Family</td>
<td>Actual: 90<br><strong>Expected: 62</strong></td>
<td>Actual: 30<br><strong>Expected 58</strong></td>
</tr>
<tr>
<td>
<p>Difference: 90 – 62 = 28</p>
<p>Squared Difference: 784</p>
<p>Divide by Expected: 784/62 = 12.65</p>
</td>
<td>
<p>Difference: 30 – 58 = -28</p>
<p>Squared Difference: 784</p>
<p>Divide by Expected: 784/58 = 13.52</p>
</td>
</tr>
<tr>
<td rowspan="2">Horror</td>
<td>Actual: 45<br><strong>Expected 28.42</strong></td>
<td>Actual: 10<br><strong>Expected 26.58</strong></td>
</tr>
<tr>
<td>
<p>Difference: 45 – 28.42 = 16.58</p>
<p>Squared Difference: 275.01</p>
<p>Divide by Expected: 275.01/28.42 = 9.68</p>
</td>
<td>
<p>Difference: 10 – 26.58 = -16.58</p>
<p>Squared Difference: 275.01</p>
<p>Divide by Expected: 275.01/26.58 = 10.35</p>
</td>
</tr>
</tbody>
</table>
<p>Lastly, to get our test statistic, we add the numbers in the final row for
each cell:</p>
</div>
</div>
</div>
</div>
</main>
<footer></footer>
</body>
110 changes: 110 additions & 0 deletions test/roundtrip/roundtrip-fixtures/rowspan.output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!DOCTYPE html>
<html>
<head>
<title>Table 4: Preparing to calculate our test statistic</title>
<link rel="canonical" href="https://undefined/">
<meta name="description" content="Lastly, to get our test statistic, we add the numbers in the final row for each cell:">
<meta property="og:title" content="Table 4: Preparing to calculate our test statistic">
<meta property="og:description" content="Lastly, to get our test statistic, we add the numbers in the final row for each cell:">
<meta property="og:url" content="https://undefined/">
<meta property="og:image" content="https://undefined/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta property="og:image:secure_url" content="https://undefined/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Table 4: Preparing to calculate our test statistic">
<meta name="twitter:description" content="Lastly, to get our test statistic, we add the numbers in the final row for each cell:">
<meta name="twitter:image" content="https://undefined/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
</head>
<body>
<header></header>
<main>
<div>
<div class="columns">
<div>
<div data-valign="middle">
<h4 id="table-4-preparing-to-calculate-our-test-statistic"><strong>Table 4: Preparing to calculate our test statistic</strong></h4>
<table>
<tbody>
<tr>
<td>Type of Movie</td>
<td>Snack</td>
<td>No Snacks</td>
</tr>
<tr>
<td rowspan="2">Action</td>
<td>Actual: 50<br><strong>Expected: 64.58</strong></td>
<td>Actual: 75<br><strong>Expected: 60.42</strong></td>
</tr>
<tr>
<td>
<p>Difference: 50 – 64.58 = -14.58</p>
<p>Squared Difference: 212.67</p>
<p>Divide by Expected: 212.67/64.58 = 3.29</p>
</td>
<td>
<p>Difference: 75 – 60.42 = 14.58</p>
<p>Squared Difference: 212.67</p>
<p>Divide by Expected: 212.67/60.42 = 3.52</p>
</td>
</tr>
<tr>
<td rowspan="2">Comedy</td>
<td>Actual: 125<br><strong>Expected 155</strong></td>
<td>Actual 175<br><strong>Expected 145</strong></td>
</tr>
<tr>
<td>
<p>Difference: 125 – 155 = -30</p>
<p>Squared Difference: 900</p>
<p>Divide by Expected: 900/155 = 5.81</p>
</td>
<td>
<p>Difference: 175 – 145 = 30</p>
<p>Squared Difference: 900</p>
<p>Divide by Expected: 900/145 = 6.21</p>
</td>
</tr>
<tr>
<td rowspan="2">Family</td>
<td>Actual: 90<br><strong>Expected: 62</strong></td>
<td>Actual: 30<br><strong>Expected 58</strong></td>
</tr>
<tr>
<td>
<p>Difference: 90 – 62 = 28</p>
<p>Squared Difference: 784</p>
<p>Divide by Expected: 784/62 = 12.65</p>
</td>
<td>
<p>Difference: 30 – 58 = -28</p>
<p>Squared Difference: 784</p>
<p>Divide by Expected: 784/58 = 13.52</p>
</td>
</tr>
<tr>
<td rowspan="2">Horror</td>
<td>Actual: 45<br><strong>Expected 28.42</strong></td>
<td>Actual: 10<br><strong>Expected 26.58</strong></td>
</tr>
<tr>
<td>
<p>Difference: 45 – 28.42 = 16.58</p>
<p>Squared Difference: 275.01</p>
<p>Divide by Expected: 275.01/28.42 = 9.68</p>
</td>
<td>
<p>Difference: 10 – 26.58 = -16.58</p>
<p>Squared Difference: 275.01</p>
<p>Divide by Expected: 275.01/26.58 = 10.35</p>
</td>
</tr>
</tbody>
</table>
<p>Lastly, to get our test statistic, we add the numbers in the final row for each cell:</p>
</div>
</div>
</div>
</div>
</main>
<footer></footer>
</body>
</html>
1 change: 1 addition & 0 deletions test/roundtrip/roundtrip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const specs = [
'some-text',
'all-sections',
'microdata',
'rowspan',
];

describe('Roundtrip tests', () => {
Expand Down
Loading