Skip to content

Commit e205f02

Browse files
authored
Escape specific html tags in P elements when quoting to markdown (#23)
1 parent e53342a commit e205f02

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/markdown.ts

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ const filters: Filters = {
7676
}
7777
return `\`${text}\``
7878
},
79+
P(el) {
80+
const pElement = document.createElement('p')
81+
const text = el.textContent || ''
82+
pElement.textContent = text.replace(/<(\/?)(pre|strong|weak|em)>/g, '\\<$1$2\\>')
83+
return pElement
84+
},
7985
STRONG(el) {
8086
return `**${el.textContent || ''}**`
8187
},

test/test.js

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ describe('quote-selection', function () {
102102
<p>This should not appear as part of the quote.</p>
103103
<div class="comment-body">
104104
<p>This is <strong>beautifully</strong> formatted <em>text</em> that even has some <code>inline code</code>.</p>
105+
<p>This is a simple p line</p>
106+
<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>
105107
<pre><code>foo(true)</code></pre>
106108
<p><a href="http://example.com">Links</a> and <img alt=":emoji:" class="emoji" src="image.png"> are preserved.</p>
107109
<blockquote><p>Music changes, and I'm gonna change right along with it.<br>--Aretha Franklin</p></blockquote>
@@ -131,6 +133,10 @@ describe('quote-selection', function () {
131133
textarea.value.replace(/ +\n/g, '\n'),
132134
`> This is **beautifully** formatted _text_ that even has some \`inline code\`.
133135
>
136+
> This is a simple p line
137+
>
138+
> some escaped html tags to ignore \\<pre\\> \\<strong\\> \\<weak\\> \\<em\\> \\</pre\\> \\</strong\\> \\</weak\\> \\</em\\>
139+
>
134140
> \`\`\`
135141
> foo(true)
136142
> \`\`\`

0 commit comments

Comments
 (0)