Skip to content

Commit

Permalink
Workaround GitHub rendering bug with inline code in tables
Browse files Browse the repository at this point in the history
A table cell containing `^1 || ^2` would only be rendered as `^1` by
GitHub as the pipes break out of the table cell.
  • Loading branch information
cs278 committed Aug 7, 2023
1 parent e926cf9 commit 7ae8c84
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
30 changes: 27 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

36 changes: 32 additions & 4 deletions src/markdownRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ export function render(
: "Moved[^Moved]";
}

row[0] = `\`${entry.name}\``;
row[0] = renderInlineCodeInsideTableCell(entry.name);
row[1] = entry.section === Section.Prod ? "Prod" : "Dev";
row[2] = operationText;
row[3] =
entry.operation & Operation.Added ? "" : `\`${entry.base}\``;
entry.operation & Operation.Added ? "" : renderInlineCodeInsideTableCell(entry.base);
row[4] =
entry.operation & Operation.Removed ? "" : `\`${entry.head}\``;

entry.operation & Operation.Removed ? "" : renderInlineCodeInsideTableCell(entry.head);
``
message.push("| " + row.join(" | ") + " |");
});
}
Expand Down Expand Up @@ -143,3 +143,31 @@ export function render(

return message.join("\n");
}

/**
* Workaround problem with GHFM whereby an inline code block inside a table cell
* cannot contain a pipe without breaking the table.
*
* https://stackoverflow.com/a/17320389
*/
function renderInlineCodeInsideTableCell(content: string|null): string {
if (content === null) {
return '';
}

if (content.indexOf('|') >= 0) {
content = htmlEscape(content).replace(/\|/g, '|');

return `<code>${content}</code>`;
}

return `\`${content}\``;
}

function htmlEscape(content: string): string {
return content
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}

0 comments on commit 7ae8c84

Please sign in to comment.