Skip to content

Commit

Permalink
Tighten up regex performance in comments.ts (#182)
Browse files Browse the repository at this point in the history
* Tighten up regex performance in comments.ts

* Tighten regex further.

* Fix broken test

* Fix escape stripping

* Further improve stripping to handle multi-backtick escaping.

* Also strip <code> elements.

* Fix formatting.

* Fix newline regex.
  • Loading branch information
Eyas authored May 17, 2022
1 parent d2bb057 commit 6470181
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/schema-dts-gen/src/ts/util/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,19 @@ function parseCommentInternal<P extends Processor>(
// without line break tags as a strong indication to use Markdown (even if other
// HTML exists). Othrewise, we simply test for the existence of any HTML tag.
function shouldParseAsHtml(s: string): boolean {
const BR = /<[Bb][Rr]\s*\/?>/g;
const NL = /\/*n/g;
const BR = /<br\s*\/?>/gi;
const NL = /\n/gi;
if (NL.test(s) && !BR.test(s)) return false;

return /<[A-Za-z][A-Za-z0-9-]*[^>]*>/g.test(s);
// Any part of the string that is _not_ escaped in (`) cannot contain HTML in
// a Markdown comment.
//
// Similarly, strip all `<code>` blocks entirely. These are somehow still
// included in Markdown comments.
const stripped = s
.replace(/<code[^<>]*>((?!<\/code>).)*<\/code\s*>/gi, '')
.replace(/(?!^)(`+)((?!\1).)+\1/g, '');
return /<[A-Za-z][A-Za-z0-9-]*(\s[^<>]*)?>/g.test(stripped);
}

interface ParseContext {
Expand Down Expand Up @@ -169,11 +177,17 @@ const universalHandlers: OnTagBuilder[] = [
const htmlHandlers: OnTagBuilder[] = [
[
'p',
{open: ({isFirstChild}) => (isFirstChild ? '' : '\n'), close: () => '\n'},
{
open: ({isFirstChild}) => (isFirstChild ? '' : '\n'),
close: () => '\n',
},
],
[
'a',
{open: ({node}) => `{@link ${node.properties!['href']} `, close: () => '}'},
{
open: ({node}) => `{@link ${node.properties!['href']} `,
close: () => '}',
},
],
['em', em],
['i', em],
Expand Down

0 comments on commit 6470181

Please sign in to comment.