Wrap highlight code blocks with <code> (#1026)

* Wrap highlight code blocks with <code>

* Fix tests for highlight code block
This commit is contained in:
hui.liu 2020-05-18 15:41:56 +08:00 committed by GitHub
parent fbf431d612
commit 32ca1a14ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View file

@ -231,7 +231,9 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
.unwrap_or(::syntect::highlighting::Color::WHITE); .unwrap_or(::syntect::highlighting::Color::WHITE);
background = IncludeBackground::IfDifferent(color); background = IncludeBackground::IfDifferent(color);
let snippet = start_highlighted_html_snippet(theme); let snippet = start_highlighted_html_snippet(theme);
Event::Html(snippet.0.into()) let mut html = snippet.0;
html.push_str("<code>");
Event::Html(html.into())
} }
Event::End(Tag::CodeBlock(_)) => { Event::End(Tag::CodeBlock(_)) => {
if !context.config.highlight_code { if !context.config.highlight_code {
@ -239,7 +241,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
} }
// reset highlight and close the code block // reset highlight and close the code block
highlighter = None; highlighter = None;
Event::Html("</pre>".into()) Event::Html("</code></pre>".into())
} }
Event::Start(Tag::Image(link_type, src, title)) => { Event::Start(Tag::Image(link_type, src, title)) => {
if is_colocated_asset_link(&src) { if is_colocated_asset_link(&src) {

View file

@ -39,7 +39,7 @@ fn can_highlight_code_block_no_lang() {
let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap(); let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap();
assert_eq!( assert_eq!(
res.body, res.body,
"<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">$ gutenberg server\n$ ping\n</span></pre>" "<pre style=\"background-color:#2b303b;\">\n<code><span style=\"color:#c0c5ce;\">$ gutenberg server\n$ ping\n</span></code></pre>"
); );
} }
@ -53,7 +53,7 @@ fn can_highlight_code_block_with_lang() {
let res = render_content("```python\nlist.append(1)\n```", &context).unwrap(); let res = render_content("```python\nlist.append(1)\n```", &context).unwrap();
assert_eq!( assert_eq!(
res.body, res.body,
"<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">list.</span><span style=\"color:#bf616a;\">append</span><span style=\"color:#c0c5ce;\">(</span><span style=\"color:#d08770;\">1</span><span style=\"color:#c0c5ce;\">)\n</span></pre>" "<pre style=\"background-color:#2b303b;\">\n<code><span style=\"color:#c0c5ce;\">list.</span><span style=\"color:#bf616a;\">append</span><span style=\"color:#c0c5ce;\">(</span><span style=\"color:#d08770;\">1</span><span style=\"color:#c0c5ce;\">)\n</span></code></pre>"
); );
} }
@ -68,7 +68,7 @@ fn can_higlight_code_block_with_unknown_lang() {
// defaults to plain text // defaults to plain text
assert_eq!( assert_eq!(
res.body, res.body,
"<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">list.append(1)\n</span></pre>" "<pre style=\"background-color:#2b303b;\">\n<code><span style=\"color:#c0c5ce;\">list.append(1)\n</span></code></pre>"
); );
} }