Skip to content

Commit

Permalink
Optimize regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
wljince007 committed Apr 9, 2024
1 parent c814129 commit 3aaef30
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/app-cli/tests/html_to_md/code_multiline_1.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- this if from: https://devblogs.microsoft.com/dotnet/http-3-support-in-dotnet-6/ -->
<!-- this if from: https://flaviocopes.com/bubble-sort-javascript/ -->
<div><pre class="astro-code github-dark" style="background-color: rgb(36, 41, 46); color: rgb(225, 228, 232); overflow-x: auto; --darkreader-inline-bgcolor: #1d2125; --darkreader-inline-color: #d7d4cf; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace;" tabindex="0" data-darkreader-inline-bgcolor="" data-darkreader-inline-color=""><code><span class="line"><span style="color: rgb(249, 117, 131); --darkreader-inline-color: #f96e7c;" data-darkreader-inline-color="">const</span><span style="color: rgb(179, 146, 240); --darkreader-inline-color: #ab86ee;" data-darkreader-inline-color=""> bubbleSort</span><span style="color: rgb(249, 117, 131); --darkreader-inline-color: #f96e7c;" data-darkreader-inline-color=""> =</span><span style="color: rgb(225, 228, 232); --darkreader-inline-color: #d7d4cf;" data-darkreader-inline-color=""> (</span><span style="color: rgb(255, 171, 112); --darkreader-inline-color: #ffa668;" data-darkreader-inline-color="">originalArray</span><span style="color: rgb(225, 228, 232); --darkreader-inline-color: #d7d4cf;" data-darkreader-inline-color="">) </span><span style="color: rgb(249, 117, 131); --darkreader-inline-color: #f96e7c;" data-darkreader-inline-color="">=&gt;</span><span style="color: rgb(225, 228, 232); --darkreader-inline-color: #d7d4cf;" data-darkreader-inline-color=""> {</span></span>
<span class="line"><span style="color: rgb(249, 117, 131); --darkreader-inline-color: #f96e7c;" data-darkreader-inline-color=""> let</span><span style="color: rgb(225, 228, 232); --darkreader-inline-color: #d7d4cf;" data-darkreader-inline-color=""> swapped </span><span style="color: rgb(249, 117, 131); --darkreader-inline-color: #f96e7c;" data-darkreader-inline-color="">=</span><span style="color: rgb(121, 184, 255); --darkreader-inline-color: #6ebdff;" data-darkreader-inline-color=""> false</span></span>
<span class="line"></span>
Expand Down
2 changes: 1 addition & 1 deletion packages/turndown/src/commonmark-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ rules.fencedCodeBlock = {
var fence = repeat(fenceChar, fenceSize)

// remove code block leading and trailing empty lines
code = code.replace(/^( *\t*\n)+/, '').trimEnd()
code = code.replace(/^([ \t]*\n)+/, '').trimEnd()

return (
'\n\n' + fence + language + '\n' +
Expand Down
5 changes: 3 additions & 2 deletions packages/turndown/src/turndown.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ function replacementForNode (node, previousNode) {
// Fix: Web clipper has trouble with code blocks on Joplin's website.
// See https://github.com/laurent22/joplin/pull/10126#issuecomment-2016523281 .
// if isCode, keep line breaks
//test case: packages/app-cli/tests/html_to_md/code_multiline_1.html
//test case: packages/app-cli/tests/html_to_md/code_multiline_3.html

//If the leading blank of current node or leading blank of current node including line breaks, and the leading blank of current node is equal to the leading blank of it's first child node, and the trailing blank of the current node is equal to the leading blank of it's last child node, it indicates that the leading blank and leading blank of current node is from it's child nodes, so should not be added repeatedly, this remove multiple line breaks.
Expand All @@ -256,8 +257,8 @@ function replacementForNode (node, previousNode) {
var firstChildWhitespace = node.childNodes[0].flankingWhitespace
var lastChildWhitespace = node.childNodes[node.childNodes.length-1].flankingWhitespace

if (whitespace.leading === firstChileWhitespace.leading &&
whitespace.trailing === lastChileWhitespace.trailing){
if (whitespace.leading === firstChildWhitespace.leading &&
whitespace.trailing === lastChildWhitespace.trailing){
content = content.trim()
}
}else {
Expand Down

0 comments on commit 3aaef30

Please sign in to comment.