From afbccecfdad97c5f2a2a11e8be3998c8e8937489 Mon Sep 17 00:00:00 2001 From: Michael Clayton Date: Fri, 28 Jan 2022 13:11:33 -0500 Subject: [PATCH] Fix markup for fenced code with linenos (#1746) * fix fenced code blocks with linenos and minify_html * fix test cases involving linenos --- components/rendering/src/codeblock/mod.rs | 6 +- .../rendering/tests/codeblock_linenos.rs | 6 +- .../tests/codeblock_shortcode_mix.rs | 90 +++++++++---------- 3 files changed, 53 insertions(+), 49 deletions(-) diff --git a/components/rendering/src/codeblock/mod.rs b/components/rendering/src/codeblock/mod.rs index 1e8d1eb09b..df1c60492e 100644 --- a/components/rendering/src/codeblock/mod.rs +++ b/components/rendering/src/codeblock/mod.rs @@ -162,6 +162,10 @@ impl<'config> CodeBlock<'config> { let highlighted_line = self.highlighter.highlight_line(line); maybe_mark(&mut buffer, &highlighted_line); + + if self.line_numbers { + buffer.push_str(""); + } } if let Some(rest) = self.highlighter.finalize() { @@ -169,7 +173,7 @@ impl<'config> CodeBlock<'config> { } if self.line_numbers { - buffer.push_str(""); + buffer.push_str(""); } buffer diff --git a/components/rendering/tests/codeblock_linenos.rs b/components/rendering/tests/codeblock_linenos.rs index 4315fba8e2..94e367dbde 100644 --- a/components/rendering/tests/codeblock_linenos.rs +++ b/components/rendering/tests/codeblock_linenos.rs @@ -32,7 +32,7 @@ bar .unwrap(); assert_eq!( res.body, - "
1foo\n
2bar\n
\n" + "
1foo\n
2bar\n
\n" ); } @@ -62,7 +62,7 @@ bar .unwrap(); assert_eq!( res.body, - "
40foo\n
41bar\n
\n" + "
40foo\n
41bar\n
\n" ); } @@ -92,6 +92,6 @@ bar .unwrap(); assert_eq!( res.body, - "
1foo\n
2bar\n
\n" + "
1foo\n
2bar\n
\n" ); } diff --git a/components/rendering/tests/codeblock_shortcode_mix.rs b/components/rendering/tests/codeblock_shortcode_mix.rs index e1781913b3..53a7da4fac 100644 --- a/components/rendering/tests/codeblock_shortcode_mix.rs +++ b/components/rendering/tests/codeblock_shortcode_mix.rs @@ -63,7 +63,7 @@ fn can_render_shortcode_in_codeblock() { .unwrap(); assert_eq!( res.body, - "
1<div id="custom-attr">\n
2<div >\n
3 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>\n
4</div>\n
5\n
6</div>\n
\n" + "
1<div id="custom-attr">\n
2<div >\n
3 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>\n
4</div>\n
5\n
6</div>\n
\n" ); } @@ -130,18 +130,18 @@ text 3 .unwrap(); assert_eq!( res.body, -r#"
1<span>text 1</span> -
2<div > -
3 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> -
4</div> -
5 -
6<span>text 2</span> -
7<div class="gist"> -
8 <script src="https:&#x2F;&#x2F;gist.github.com&#x2F;Keats&#x2F;e5fb6aad409f28721c0ba14161644c57.js"></script> -
9</div> -
10 -
11<span>text 3</span> -
+ r#"
1<span>text 1</span> +
2<div > +
3 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> +
4</div> +
5 +
6<span>text 2</span> +
7<div class="gist"> +
8 <script src="https:&#x2F;&#x2F;gist.github.com&#x2F;Keats&#x2F;e5fb6aad409f28721c0ba14161644c57.js"></script> +
9</div> +
10 +
11<span>text 3</span> +
"# ); } @@ -175,7 +175,7 @@ fn is_highlighting_linenos_still_working() { .unwrap(); assert_eq!( res.body, - "
1<div id="custom-attr">\n
2<div >\n
3 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>\n
4</div>\n
5\n
6</div>\n
\n" + "
1<div id="custom-attr">\n
2<div >\n
3 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>\n
4</div>\n
5\n
6</div>\n
\n" ); // multiple shortcode mixed with syntax and line numbers let res = render_content( @@ -193,18 +193,18 @@ fn is_highlighting_linenos_still_working() { .unwrap(); assert_eq!( res.body, -r#"
1<span>text 1</span> -
2<div > -
3 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> -
4</div> -
5 -
6<span>text 2</span> -
7<div class="gist"> -
8 <script src="https:&#x2F;&#x2F;gist.github.com&#x2F;Keats&#x2F;e5fb6aad409f28721c0ba14161644c57.js"></script> -
9</div> -
10 -
11<span>text 3</span> -
+ r#"
1<span>text 1</span> +
2<div > +
3 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> +
4</div> +
5 +
6<span>text 2</span> +
7<div class="gist"> +
8 <script src="https:&#x2F;&#x2F;gist.github.com&#x2F;Keats&#x2F;e5fb6aad409f28721c0ba14161644c57.js"></script> +
9</div> +
10 +
11<span>text 3</span> +
"# ); } @@ -247,25 +247,25 @@ A quote .unwrap(); assert_eq!( res.body, -r#"
1<a href="javascript:void(0);">{{ before(texts="1") }}</a> -
2Normally people would not write something & like <> this: -
3<div id="custom-attr"> -
4An inline <div class="youtube"> -
5 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> -
6</div> -
7 shortcode -
8</div> -
9Plain text in-between -
10{% quote(author="Vincent") %} -
11A quote -
12{% end %} -
13<div class="gist"> -
14 <script src="https:&#x2F;&#x2F;gist.github.com&#x2F;Keats&#x2F;e5fb6aad409f28721c0ba14161644c57.js"></script> -
15</div> -
16 -
17{# A Tera comment, you should see it #} -
18<!-- end text goes here --> -
+ r#"
1<a href="javascript:void(0);">{{ before(texts="1") }}</a> +
2Normally people would not write something & like <> this: +
3<div id="custom-attr"> +
4An inline <div class="youtube"> +
5 <iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> +
6</div> +
7 shortcode +
8</div> +
9Plain text in-between +
10{% quote(author="Vincent") %} +
11A quote +
12{% end %} +
13<div class="gist"> +
14 <script src="https:&#x2F;&#x2F;gist.github.com&#x2F;Keats&#x2F;e5fb6aad409f28721c0ba14161644c57.js"></script> +
15</div> +
16 +
17{# A Tera comment, you should see it #} +
18<!-- end text goes here --> +
"# ); } \ No newline at end of file