Skip to content

Commit dab5f48

Browse files
feat: remove highlight span tags from code tags
1 parent 8bd3628 commit dab5f48

File tree

7 files changed

+506
-10
lines changed

7 files changed

+506
-10
lines changed

commonmark.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ var commonmark = []Rule{
288288
{
289289
Filter: []string{"code"},
290290
Replacement: func(_ string, selec *goquery.Selection, opt *Options) *string {
291-
code := getHTML(selec)
291+
code := getCodeContent(selec)
292292

293293
// Newlines in the text aren't great, since this is inline code and not a code block.
294294
// Newlines will be stripped anyway in the browser, but it won't be recognized as code
@@ -324,10 +324,7 @@ var commonmark = []Rule{
324324
language := codeElement.AttrOr("class", "")
325325
language = strings.Replace(language, "language-", "", 1)
326326

327-
code := getHTML(codeElement)
328-
if codeElement.Length() == 0 {
329-
code = getHTML(selec)
330-
}
327+
code := getCodeContent(selec)
331328

332329
fenceChar, _ := utf8.DecodeRuneInString(opt.Fence)
333330
fence := CalculateCodeFence(fenceChar, code)

testdata/TestCommonmark/pre_code/goldmark.golden

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,72 @@ The <img> tag is used to embed an image.
3434

3535
The <img/> tag is used to embed an image.
3636

37+
</code></pre>
38+
<pre><code>
39+
&lt;ul&gt;
40+
&lt;li data-converter-list-prefix=&quot;- &quot;&gt;One&lt;/li&gt;
41+
&lt;li data-converter-list-prefix=&quot;- &quot;&gt;Two&lt;/li&gt;
42+
&lt;li data-converter-list-prefix=&quot;- &quot;&gt;Three&lt;/li&gt;
43+
&lt;/ul&gt;
44+
3745
</code></pre>
3846
<pre><code>
3947
&lt;a href=&quot;#Blabla&quot; data-index=&quot;1&quot;&gt;
4048
&lt;img src=&quot;http://bla.bla/img/img.svg&quot; style=&quot;height:auto&quot; width=&quot;200px&quot;/&gt;
4149
&lt;/a&gt;
4250

4351
</code></pre>
52+
<p>We can do this like so:</p>
53+
<pre><code class="language-js">window.location.pathname.split(&quot;/&quot;);
54+
// [&quot;&quot;, &quot;blog&quot;, &quot;javascript&quot;, &quot;how-to-get-the-last-segment-of-a-url-in-javascript&quot;, &quot;&quot;]
55+
</code></pre>
56+
<hr>
57+
<pre><code class="language-js">function fn() {
58+
x = 1;
59+
return x;
60+
// eslint-disable-next-line no-unreachable
61+
x = 3;
62+
}
63+
</code></pre>
64+
<hr>
65+
<pre><code>&lt;p&gt;&lt;span&gt;Some text&lt;/span&gt;&lt;/p&gt;
66+
67+
</code></pre>
68+
<p>Copy to Clipboard</p>
69+
<hr>
70+
<h4>HTML:</h4>
71+
<pre><code>&lt;p&gt;Using CSS to change the font color is easy.&lt;/p&gt;
72+
&lt;pre&gt;
73+
body {
74+
color: red;
75+
}
76+
&lt;/pre&gt;
77+
78+
</code></pre>
79+
<p>Copy to Clipboard</p>
80+
<hr>
81+
<pre><code class="language-md">---
82+
title: &quot;Hello! This is the markdown file&quot;
83+
date: 2021-09-25
84+
tags: [&quot;react&quot;]
85+
---
86+
87+
Content of the post goes here.
88+
89+
![Image with alt text](./image.png)
90+
</code></pre>
91+
<hr>
92+
<pre><code class="language-markup">&lt;code class=&quot;language-css&quot;&gt;p { color: red }&lt;/code&gt;
93+
</code></pre>
94+
<hr>
95+
<p>The function <code>selectAll()</code> highlights all the text in the
96+
input field so the user can, for example, copy or delete the text.</p>
97+
<p>The <strong><code>&lt;pre&gt;</code></strong> <a href="http://example.com/en-US/docs/Web/HTML">HTML</a> element represents preformatted text.</p>
98+
<hr>
99+
<pre><code>
100+
This is the content:
101+
&lt;code&gt;_code_&lt;/code&gt;
102+
&lt;pre&gt;_pre_&lt;/pre&gt;
103+
Cool!
104+
105+
</code></pre>

testdata/TestCommonmark/pre_code/input.html

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,121 @@
9393
The <img> tag is used to embed an image.
9494
</code></pre>
9595

96+
<pre><code>
97+
<ul>
98+
<li>One</li>
99+
<li>Two</li>
100+
<li>Three</li>
101+
</ul>
102+
</code></pre>
103+
104+
96105
<!--TODO: make sure that "data-index" does not get added to the link-->
97106
<pre><code>
98107
<a href="#Blabla">
99108
<img src="http://bla.bla/img/img.svg" style="height:auto" width="200px"/>
100109
</a>
101110
</code></pre>
111+
112+
113+
114+
115+
<!--highlighting with javascript-->
116+
<p>We can do this like so:</p>
117+
<div class="gatsby-highlight" data-language="js"><pre class="language-js"><code class="language-js">window<span class="token punctuation">.</span>location<span class="token punctuation">.</span>pathname<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
118+
<span class="token comment">// ["", "blog", "javascript", "how-to-get-the-last-segment-of-a-url-in-javascript", ""]</span></code></pre></div>
119+
120+
121+
122+
<hr />
123+
124+
125+
126+
<div class="gatsby-highlight" data-language="js"><pre class="language-js"><code class="language-js"><span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
127+
x <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
128+
<span class="token keyword">return</span> x<span class="token punctuation">;</span>
129+
<span class="token comment">// eslint-disable-next-line no-unreachable</span>
130+
x <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
131+
<span class="token punctuation">}</span></code></pre></div>
132+
133+
134+
135+
<hr />
136+
137+
138+
139+
<div class="code-example"><pre class="brush: html notranslate"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span><span class="token punctuation">&gt;</span></span>Some text<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
140+
</code></pre><button aria-hidden="false" type="button" class="copy-icon"><span class="visually-hidden">Copy to Clipboard</span></button></div>
141+
142+
143+
144+
<hr />
145+
146+
147+
148+
<!--don't remove the pre tag from the middle of the code snippet-->
149+
<div>
150+
<h4 id="html">HTML:</h4>
151+
152+
<div class="code-example">
153+
<pre class="brush: html notranslate"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span>Using CSS to change the font color is easy.<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
154+
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>pre</span><span class="token punctuation">&gt;</span></span>
155+
body {
156+
color: red;
157+
}
158+
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>pre</span><span class="token punctuation">&gt;</span></span>
159+
</code></pre>
160+
<button aria-hidden="false" type="button" class="copy-icon"><span class="visually-hidden">Copy to Clipboard</span></button>
161+
</div>
162+
163+
</div>
164+
165+
166+
167+
<hr />
168+
169+
170+
171+
<div class="gatsby-highlight" data-language="md"><pre class="language-md"><code class="language-md"><span class="token front-matter-block"><span class="token punctuation">---</span>
172+
<span class="token font-matter yaml language-yaml">title: "Hello! This is the markdown file"
173+
date: 2021-09-25
174+
tags: ["react"]</span>
175+
<span class="token punctuation">---</span></span>
176+
177+
Content of the post goes here.
178+
179+
<span class="token url"><span class="token operator">!</span>[<span class="token content">Image with alt text</span>](<span class="token url">./image.png</span>)</span></code></pre></div>
180+
181+
182+
183+
<hr />
184+
185+
186+
187+
<pre class="language-markup" tabindex="0"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>code</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>language-css<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>p { color: red }<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>code</span><span class="token punctuation">&gt;</span></span></code></pre>
188+
189+
190+
191+
<hr />
192+
193+
194+
195+
<p>The function <code>selectAll()</code> highlights all the text in the
196+
input field so the user can, for example, copy or delete the text.</p>
197+
198+
<p>The <strong><code>&lt;pre&gt;</code></strong> <a href="/en-US/docs/Web/HTML">HTML</a> element represents preformatted text.</p>
199+
200+
201+
202+
<hr />
203+
204+
205+
206+
<pre><code>
207+
208+
This is the content:
209+
<code>_code_</code>
210+
<pre>_pre_</pre>
211+
Cool!
212+
213+
</code><pre>

testdata/TestCommonmark/pre_code/output.fenced_backtick.golden

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,101 @@ The <img/> tag is used to embed an image.
6969

7070
```
7171

72+
```
73+
74+
<ul>
75+
<li data-converter-list-prefix="- ">One</li>
76+
<li data-converter-list-prefix="- ">Two</li>
77+
<li data-converter-list-prefix="- ">Three</li>
78+
</ul>
79+
80+
```
81+
7282
```
7383

7484
<a href="#Blabla" data-index="1">
7585
<img src="http://bla.bla/img/img.svg" style="height:auto" width="200px"/>
7686
</a>
7787

88+
```
89+
90+
We can do this like so:
91+
92+
```js
93+
window.location.pathname.split("/");
94+
// ["", "blog", "javascript", "how-to-get-the-last-segment-of-a-url-in-javascript", ""]
95+
```
96+
97+
* * *
98+
99+
```js
100+
function fn() {
101+
x = 1;
102+
return x;
103+
// eslint-disable-next-line no-unreachable
104+
x = 3;
105+
}
106+
```
107+
108+
* * *
109+
110+
```
111+
<p><span>Some text</span></p>
112+
113+
```
114+
115+
Copy to Clipboard
116+
117+
* * *
118+
119+
#### HTML:
120+
121+
```
122+
<p>Using CSS to change the font color is easy.</p>
123+
<pre>
124+
body {
125+
color: red;
126+
}
127+
</pre>
128+
129+
```
130+
131+
Copy to Clipboard
132+
133+
* * *
134+
135+
```md
136+
---
137+
title: "Hello! This is the markdown file"
138+
date: 2021-09-25
139+
tags: ["react"]
140+
---
141+
142+
Content of the post goes here.
143+
144+
![Image with alt text](./image.png)
145+
```
146+
147+
* * *
148+
149+
```markup
150+
<code class="language-css">p { color: red }</code>
151+
```
152+
153+
* * *
154+
155+
The function `selectAll()` highlights all the text in the
156+
input field so the user can, for example, copy or delete the text.
157+
158+
The **`<pre>`** [HTML](http://example.com/en-US/docs/Web/HTML) element represents preformatted text.
159+
160+
* * *
161+
162+
```
163+
164+
This is the content:
165+
<code>_code_</code>
166+
<pre>_pre_</pre>
167+
Cool!
168+
78169
```

0 commit comments

Comments
 (0)