Skip to content

Commit

Permalink
fix(pacmak): xmldom error when generating packages
Browse files Browse the repository at this point in the history
In some cases, an `xmldom` error would occur when trying to render C#
XML-style documentation blocks, where inline html within a Markdown
document is not valid as far as the `xmldom` parser is considered. This
introduces a safer fall-back strategy.
  • Loading branch information
RomainMuller committed Mar 16, 2021
1 parent e58344b commit e6aefba
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/jsii-rosetta/lib/markdown/xml-comment-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,24 @@ export class CSharpXmlCommentRenderer extends MarkdownRenderer {
*/
public html_inline(node: cm.Node, _context: RendererContext) {
const html = node.literal ?? '';
const doc = new DOMParser().parseFromString(html, 'text/html');
return new XMLSerializer().serializeToString(doc);
try {
const doc = new DOMParser().parseFromString(html, 'text/html');
return new XMLSerializer().serializeToString(doc);
} catch {
// Could not parse - we'll escape unsafe XML entities here...
return html.replace(/[<>&]/g, (char: string) => {
switch (char) {
case '&':
return '&amp;';
case '<':
return '&lt;';
case '>':
return '&gt;';
default:
return char;
}
});
}
}

/**
Expand All @@ -89,9 +105,7 @@ export class CSharpXmlCommentRenderer extends MarkdownRenderer {
* If we don't do this, the parser will reject the whole XML block once it seens an unclosed
* <img> tag.
*/
public html_block(node: cm.Node, _context: RendererContext) {
const html = node.literal ?? '';
const doc = new DOMParser().parseFromString(html, 'text/html');
return new XMLSerializer().serializeToString(doc);
public html_block(node: cm.Node, context: RendererContext) {
return this.html_inline(node, context);
}
}

0 comments on commit e6aefba

Please sign in to comment.