Skip to content

Commit b79e874

Browse files
Merge #1070
1070: Re-enable syntax highlighting. r=carols10cents Syntax highlighting seems to be working as intended now. I tested by publishing a create locally with a `README.md` including a couple of code samples, see below for a screenshot. Thanks for the help, and please let me know if you have any questions. Closes #1008 <img width="963" alt="screen shot 2017-09-21 at 1 07 28 pm" src="https://user-images.githubusercontent.com/5225538/30719271-d79969e8-9ef0-11e7-878d-064a62a4dbeb.png">
2 parents ba37a57 + 164f0d8 commit b79e874

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Diff for: src/render.rs

+43
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl<'a> MarkdownRenderer<'a> {
5454
.collect();
5555
let tag_attributes = [
5656
("a", ["href", "target"].iter().cloned().collect()),
57+
("code", ["class"].iter().cloned().collect()),
5758
(
5859
"img",
5960
["width", "height", "src", "alt", "align"]
@@ -68,11 +69,37 @@ impl<'a> MarkdownRenderer<'a> {
6869
].iter()
6970
.cloned()
7071
.collect();
72+
let allowed_classes = [
73+
(
74+
"code",
75+
[
76+
"language-bash",
77+
"language-clike",
78+
"language-glsl",
79+
"language-go",
80+
"language-ini",
81+
"language-javascript",
82+
"language-json",
83+
"language-markup",
84+
"language-protobuf",
85+
"language-ruby",
86+
"language-rust",
87+
"language-scss",
88+
"language-sql",
89+
"yaml",
90+
].iter()
91+
.cloned()
92+
.collect(),
93+
),
94+
].iter()
95+
.cloned()
96+
.collect();
7197
let html_sanitizer = Ammonia {
7298
link_rel: Some("nofollow noopener noreferrer"),
7399
keep_cleaned_elements: true,
74100
tags: tags,
75101
tag_attributes: tag_attributes,
102+
allowed_classes: allowed_classes,
76103
..Ammonia::default()
77104
};
78105
MarkdownRenderer { html_sanitizer: html_sanitizer }
@@ -173,4 +200,20 @@ mod tests {
173200
let result = markdown_to_html(text).unwrap();
174201
assert_eq!(result, "<p>wb’</p>\n");
175202
}
203+
204+
#[test]
205+
fn code_block_with_syntax_highlighting() {
206+
let code_block = r#"```rust \
207+
println!("Hello World"); \
208+
```"#;
209+
let result = markdown_to_html(code_block).unwrap();
210+
assert!(result.contains("<code class=\"language-rust\">"));
211+
}
212+
213+
#[test]
214+
fn text_with_forbidden_class_attribute() {
215+
let text = "<p class='bad-class'>Hello World!</p>";
216+
let result = markdown_to_html(text).unwrap();
217+
assert_eq!(result, "<p>Hello World!</p>\n");
218+
}
176219
}

0 commit comments

Comments
 (0)