Skip to content

Commit

Permalink
Escape specific html tags in P elements when quoting to markdown (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
akenneth authored Aug 4, 2021
1 parent e53342a commit e205f02
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ const filters: Filters = {
}
return `\`${text}\``
},
P(el) {
const pElement = document.createElement('p')
const text = el.textContent || ''
pElement.textContent = text.replace(/<(\/?)(pre|strong|weak|em)>/g, '\\<$1$2\\>')
return pElement
},
STRONG(el) {
return `**${el.textContent || ''}**`
},
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ describe('quote-selection', function () {
<p>This should not appear as part of the quote.</p>
<div class="comment-body">
<p>This is <strong>beautifully</strong> formatted <em>text</em> that even has some <code>inline code</code>.</p>
<p>This is a simple p line</p>
<p>some escaped html tags to ignore &lt;pre&gt; &lt;strong&gt; &lt;weak&gt; &lt;em&gt; &lt;/pre&gt; &lt;/strong&gt; &lt;/weak&gt; &lt;/em&gt;</p>
<pre><code>foo(true)</code></pre>
<p><a href="http://example.com">Links</a> and <img alt=":emoji:" class="emoji" src="image.png"> are preserved.</p>
<blockquote><p>Music changes, and I'm gonna change right along with it.<br>--Aretha Franklin</p></blockquote>
Expand Down Expand Up @@ -131,6 +133,10 @@ describe('quote-selection', function () {
textarea.value.replace(/ +\n/g, '\n'),
`> This is **beautifully** formatted _text_ that even has some \`inline code\`.
>
> This is a simple p line
>
> some escaped html tags to ignore \\<pre\\> \\<strong\\> \\<weak\\> \\<em\\> \\</pre\\> \\</strong\\> \\</weak\\> \\</em\\>
>
> \`\`\`
> foo(true)
> \`\`\`
Expand Down

0 comments on commit e205f02

Please sign in to comment.