Skip to content

Commit 65b58d3

Browse files
committed
WIP Closes rust-lang#1008 - Adding syntax highlighting.
- Added `code` tag and corresponding `class` attribute to the whitelisted `tag_attributes`. - Added `allowed-classes` for the `code` tag.
1 parent 264dd1a commit 65b58d3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/render.rs

Lines changed: 38 additions & 0 deletions
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,15 @@ 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#"```ruby \
207+
def cat \
208+
puts meow \
209+
end \
210+
```"#;
211+
let result = markdown_to_html(code_block).unwrap();
212+
assert_eq!(result, "cat")
213+
}
176214
}

0 commit comments

Comments
 (0)