Skip to content

Commit

Permalink
fix: update inefficient RegEx #357
Browse files Browse the repository at this point in the history
- Update below regex to make it more efficient:
\r\n|\r|\n -> \r?\n|\r(?!\n)

1. The \r\n is redundant to \r checking in current pattern,
and I think we can combine it with the \n checking by using \r?\n
2. At first, I want to also remove the standalone \r because it only for older Mac OS,
but I think we should keep it for backward compatibility, so adding | \r to the pattern.
3. Additional (?!\n) in the end is to make sure the \r is not followed by \n,
to avoid exponential backtracking on strings starting with '''
and containing many repetitions of '\r\n'. (per latest [CodeQL analysis alert](https://github.com/AlaskaAirlines/WC-Generator/security/code-scanning/3))

Changes to be committed:
	modified:   package-lock.json
	modified:   template/scripts/generateDocs.mjs
  • Loading branch information
fajar-apri-alaska committed Jan 30, 2024
1 parent 56e07cd commit ce69d1e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion template/scripts/generateDocs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function formatTemplateFileContents(content, destination) {
result = result.replace(/>(\r\n|\r|\n){2,}/g, '>\r\n'); // Remove empty lines directly after a closing html tag.
result = result.replace(/>(\r\n|\r|\n)```/g, '>\r\n\r\n```'); // Ensure an empty line before code samples.
result = result.replace(/>(\r\n|\r|\n){2,}```(\r\n|\r|\n)/g, '>\r\n```\r\n'); // Ensure no empty lines before close of code sample.
result = result.replace(/([^(\r\n|\r|\n)])(\r\n|\r|\n)+#/g, "$1\r\n\r\n#"); // Ensure empty line before header sections.
result = result.replace(/([^(\r\n|\r|\n)])(\r?\n|\r(?!\n))+#/g, "$1\r\n\r\n#"); // Ensure empty line before header sections.

/**
* Write the result to the destination file
Expand Down

0 comments on commit ce69d1e

Please sign in to comment.