Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't escape markdown characters in code #281

Merged
merged 1 commit into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/utils/turndown-rules/code-block-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ const getIntendNumber = (node: any): number => {
return intendNumber;
}

export const unescapeMarkdown = (s: string): string => s.replace(/\\(.)/g, '$1');

export const codeBlockRule = {
filter: filterByNodeName('DIV'),
replacement: (content: string, node: any) => {
const nodeProxy = getAttributeProxy(node);
const intend = getIntendNumber(node);
content = `${'\t'.repeat(intend)}${content}`;
if (isCodeBlock(node)) {
// turndown has already escaped markdown chars (and all '\') in content;
// reverse that to avoid extraneous backslashes in code block.
content = unescapeMarkdown(content);
return `${markdownBlock}${content}${markdownBlock}`;
}

Expand All @@ -47,4 +52,4 @@ export const codeBlockRule = {

return node.isBlock ? `\n${content}\n` : content;
},
};
};
2 changes: 2 additions & 0 deletions src/utils/turndown-rules/monospace-code-block-rule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { filterByNodeName } from './filter-by-nodename';
import { getAttributeProxy } from './get-attribute-proxy';
import { unescapeMarkdown } from './code-block-rule';

const markdownBlock = '\n```\n';

Expand Down Expand Up @@ -64,6 +65,7 @@ export const monospaceCodeBlockRule = {
return content;
}

content = unescapeMarkdown(content);
return content.trim() ? `${markdownBlock}${content}${markdownBlock}` : content;
}

Expand Down
2 changes: 1 addition & 1 deletion test/data/test-noteWithCodeBlock.enex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
<en-export export-date="20201019T211051Z" application="Evernote" version="Evernote Mac 7.14 (458265)">
<note><title>Note with code block</title><content><![CDATA[<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div>Some text before the code block</div><div><br /></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div># This program prints Hello, world in Python</div><div><div>print('Hello, world!')<br /></div></div></div><div><br /></div><div><br /></div><div>Some code after the code block 1</div><div><br /></div><div>One more longer code block</div><div><br /></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>// some Rust code...</div><div>fn main() {</div><div>    for n in 1..=100 {<br /></div><div>        if n % 15 == 0 {</div><div>            println!("fizzbuzz");</div><div>        } else if n % 3 == 0 {</div><div>            println!("fizz");</div><div>        } else if n % 5 == 0 {</div><div>            println!("buzz");</div><div>        } else {</div><div>            println!("{}", n);</div><div>        }</div><div>    }</div><div>}</div></div><div><br /></div><div><br /></div></en-note>]]></content><created>20201019T194241Z</created><updated>20201019T210141Z</updated><note-attributes><author>Rodrigo</author><source>desktop.mac</source><reminder-order>0</reminder-order></note-attributes></note>
<note><title>Note with code block</title><content><![CDATA[<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div>Some text before the code block</div><div><br /></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div># This program prints *Hello, world* in _Python_</div><div><div>print('Hello, world!\n')<br /></div></div></div><div><br /></div><div><br /></div><div>Some code after the code block 1</div><div><br /></div><div>One more longer code block</div><div><br /></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>// some Rust code...</div><div>fn main() {</div><div>    for n in 1..=100 {<br /></div><div>        if n % 15 == 0 {</div><div>            println!("fizzbuzz");</div><div>        } else if n % 3 == 0 {</div><div>            println!("fizz");</div><div>        } else if n % 5 == 0 {</div><div>            println!("buzz");</div><div>        } else {</div><div>            println!("{}", n);</div><div>        }</div><div>    }</div><div>}</div></div><div><br /></div><div><br /></div></en-note>]]></content><created>20201019T194241Z</created><updated>20201019T210141Z</updated><note-attributes><author>Rodrigo</author><source>desktop.mac</source><reminder-order>0</reminder-order></note-attributes></note>
</en-export>
4 changes: 2 additions & 2 deletions test/data/test-noteWithCodeBlock.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Some text before the code block

```
\# This program prints Hello, world in Python
# This program prints *Hello, world* in _Python_

print('Hello, world!')
print('Hello, world!\n')


```
Expand Down