fix(table): tables with a preceding footnote are now rendered correctly

This commit is contained in:
shawnfunke 2022-06-22 21:15:48 +02:00
parent e4c87cf5d5
commit 6c76e79453
No known key found for this signature in database
GPG Key ID: E8BEB308C26D719B
7 changed files with 62 additions and 8 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -6,13 +6,13 @@
showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, globals) { showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, globals) {
'use strict'; 'use strict';
const regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*<?([^>\s]+)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm, const regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*<?([^>\s]+)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*((?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0)))/gm,
base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*<?(data:.+?\/.+?;base64,[A-Za-z\d+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm; base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*<?(data:.+?\/.+?;base64,[A-Za-z\d+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*((?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[)))/gm;
// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
text += '¨0'; text += '¨0';
let replaceFunc = function (wholeMatch, linkId, url, width, height, blankLines, title) { let replaceFunc = function (wholeMatch, linkId, url, width, height, rest, blankLines, title) {
// if there aren't two instances of linkId it must not be a reference link so back out // if there aren't two instances of linkId it must not be a reference link so back out
linkId = linkId.toLowerCase(); linkId = linkId.toLowerCase();
@ -29,14 +29,45 @@ showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, glo
} }
if (blankLines) { if (blankLines) {
// Oops, found blank lines, so it's not a title. // oops, found blank lines, so it's not a title, put back the
// Put back the parenthetical statement we stole. // parenthetical statement we stole
return blankLines + title; //
// if a input such as below is provided the rest of the string needs to be
// returned, in this case it would match the headers of the table, but
// they need to be returned as original, otherwise the rendering will not
// work, see issue #929 for details
//
// https://github.com/showdownjs/showdown/issues/929
//
// === input
// [^1]: https://example.com =200x200
//
// | Column1 | Column2 |
// | --- | --- |
// | key | value |
// ===
//
// === whole match
// [^1]: https://example.com =200x200
//
// | Column1 | Column2 |
// ===
//
// === rest
//
// | Column1 | Column2 |
// ===
//
// in this case it should omit the URL and return the rest of the string
// since due to the blank line we know it isn't a title, the new lines are
// important as changing these will also result in the subsequent table
// breaking
return rest;
} else { } else {
if (title) { if (title) {
globals.gTitles[linkId] = title.replace(/["']/g, '&quot;'); globals.gTitles[linkId] = title.replace(/["']/g, '&quot;');
} }
if (options.parseImgDimensions && width && height) { if (options.parseImgDimensions && width && height) {
globals.gDimensions[linkId] = { globals.gDimensions[linkId] = {
width: width, width: width,
@ -44,7 +75,8 @@ showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, glo
}; };
} }
} }
// Completely remove the definition from the text
// completely remove the definition from the text
return ''; return '';
}; };

View File

@ -0,0 +1,15 @@
<p>Footnote <a href="https://example.com">^1</a></p>
<table>
<thead>
<tr>
<th>C1</th>
<th>C2</th>
</tr>
</thead>
<tbody>
<tr>
<td>k1</td>
<td>v2</td>
</tr>
</tbody>
</table>

View File

@ -0,0 +1,7 @@
Footnote [^1]
[^1]: https://example.com
| C1 | C2 |
| --- | --- |
| k1 | v2 |