Fix syntax highlighting for no_run
blocks (#439)
This commit is contained in:
parent
a3c8ea3b4d
commit
0254fe1f81
1 changed files with 15 additions and 16 deletions
|
@ -63,7 +63,7 @@ fn build_page(
|
||||||
fn postprocess<'a>(content: &'a AstNode<'a>) {
|
fn postprocess<'a>(content: &'a AstNode<'a>) {
|
||||||
lower_headings(content);
|
lower_headings(content);
|
||||||
rewrite_md_links(content);
|
rewrite_md_links(content);
|
||||||
strip_hidden_code(content);
|
strip_rustdoc_idioms(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_headings<'a>(root: &'a AstNode<'a>) {
|
fn lower_headings<'a>(root: &'a AstNode<'a>) {
|
||||||
|
@ -87,25 +87,24 @@ fn rewrite_md_links<'a>(root: &'a AstNode<'a>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn strip_hidden_code<'a>(root: &'a AstNode<'a>) {
|
fn strip_rustdoc_idioms<'a>(root: &'a AstNode<'a>) {
|
||||||
for node in root.descendants() {
|
for node in root.descendants() {
|
||||||
let mut data = node.data.borrow_mut();
|
let mut data = node.data.borrow_mut();
|
||||||
if let NodeValue::CodeBlock(NodeCodeBlock { info, literal, .. }) = &mut data.value {
|
if let NodeValue::CodeBlock(NodeCodeBlock { info, literal, .. }) = &mut data.value {
|
||||||
if info.split(',').map(str::trim).all(|lang| lang != "rust") {
|
// Rustdoc uses commas, but CommonMark uses spaces
|
||||||
continue;
|
*info = info.replace(",", " ");
|
||||||
}
|
|
||||||
*literal = strip_hidden_code_inner(literal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn strip_hidden_code_inner(literal: &str) -> String {
|
// Rustdoc uses "#" to represent hidden setup code
|
||||||
let lines = literal
|
if info.split_whitespace().next() == Some("rust") {
|
||||||
|
*literal = literal
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.filter(|line| {
|
.filter(|line| {
|
||||||
let line = line.trim();
|
let line = line.trim();
|
||||||
line != "#" && !line.starts_with("# ")
|
line != "#" && !line.starts_with("# ")
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>()
|
||||||
lines.join("\n")
|
.join("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue