From 771ada0184ae9c0b13c363f94e06d3ca9e10b362 Mon Sep 17 00:00:00 2001 From: Max Patiiuk Date: Mon, 12 Feb 2024 06:44:50 -0800 Subject: [PATCH] Clipper: handle tables with captions (#9910) --- .../tests/html_to_md/table_with_caption.html | 15 +++++++++++++++ .../tests/html_to_md/table_with_caption.md | 6 ++++++ .../tests/html_to_md/table_with_colgroup.html | 19 +++++++++++++++++++ .../tests/html_to_md/table_with_colgroup.md | 4 ++++ packages/turndown-plugin-gfm/src/tables.js | 15 ++++++++++++++- 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 packages/app-cli/tests/html_to_md/table_with_caption.html create mode 100644 packages/app-cli/tests/html_to_md/table_with_caption.md create mode 100644 packages/app-cli/tests/html_to_md/table_with_colgroup.html create mode 100644 packages/app-cli/tests/html_to_md/table_with_colgroup.md diff --git a/packages/app-cli/tests/html_to_md/table_with_caption.html b/packages/app-cli/tests/html_to_md/table_with_caption.html new file mode 100644 index 000000000..0047f9549 --- /dev/null +++ b/packages/app-cli/tests/html_to_md/table_with_caption.html @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + +
Caption
Row 1, Column 1Row 1, Column 2Row 1, Column 3
Row 2, Column 1Row 2, Column 2Row 2, Column 3
\ No newline at end of file diff --git a/packages/app-cli/tests/html_to_md/table_with_caption.md b/packages/app-cli/tests/html_to_md/table_with_caption.md new file mode 100644 index 000000000..99e085ccf --- /dev/null +++ b/packages/app-cli/tests/html_to_md/table_with_caption.md @@ -0,0 +1,6 @@ +Caption + +| | | | +| --- | --- | --- | +| Row 1, Column 1 | Row 1, Column 2 | Row 1, Column 3 | +| Row 2, Column 1 | Row 2, Column 2 | Row 2, Column 3 | \ No newline at end of file diff --git a/packages/app-cli/tests/html_to_md/table_with_colgroup.html b/packages/app-cli/tests/html_to_md/table_with_colgroup.html new file mode 100644 index 000000000..2f0ff5d91 --- /dev/null +++ b/packages/app-cli/tests/html_to_md/table_with_colgroup.html @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + +
Row 1, Column 1Row 1, Column 2Row 1, Column 3
Row 2, Column 1Row 2, Column 2Row 2, Column 3
\ No newline at end of file diff --git a/packages/app-cli/tests/html_to_md/table_with_colgroup.md b/packages/app-cli/tests/html_to_md/table_with_colgroup.md new file mode 100644 index 000000000..befad14cf --- /dev/null +++ b/packages/app-cli/tests/html_to_md/table_with_colgroup.md @@ -0,0 +1,4 @@ +| | | | +| --- | --- | --- | +| Row 1, Column 1 | Row 1, Column 2 | Row 1, Column 3 | +| Row 2, Column 1 | Row 2, Column 2 | Row 2, Column 3 | \ No newline at end of file diff --git a/packages/turndown-plugin-gfm/src/tables.js b/packages/turndown-plugin-gfm/src/tables.js index 0df0116d1..1492211f5 100644 --- a/packages/turndown-plugin-gfm/src/tables.js +++ b/packages/turndown-plugin-gfm/src/tables.js @@ -98,10 +98,23 @@ rules.table = { } } - return '\n\n' + emptyHeader + content + '\n\n' + const captionContent = node.caption ? node.caption.textContent || '' : ''; + const caption = captionContent ? `${captionContent}\n\n` : ''; + const tableContent = `${emptyHeader}${content}`.trimStart(); + return `\n\n${caption}${tableContent}\n\n`; } } +rules.tableCaption = { + filter: ['caption'], + replacement: () => '', +}; + +rules.tableColgroup = { + filter: ['colgroup', 'col'], + replacement: () => '', +}; + rules.tableSection = { filter: ['thead', 'tbody', 'tfoot'], replacement: function (content) {