Skip to content

Commit dd00582

Browse files
authored
Rollup merge of #105539 - GuillaumeGomez:hashtag-prepended-lines-non-rust, r=notriddle
rustdoc: Only hide lines starting with `#` in rust code blocks Fixes #105527. So before approving, this is a big question: in rust code blocks, in a line starts with a `#`, we hide it in the output. However, should we do the same for non-rust code blocks too? I think it's a bit problematic to do it because `#` can be used for many things but I prefer to check first with everyone (might also be worth updating documentation too). cc ``@rust-lang/rustdoc`` r? ``@notriddle``
2 parents 2daa3bc + bc63c0e commit dd00582

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/librustdoc/html/markdown.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
246246
_ => {}
247247
}
248248
}
249-
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
250-
let text = lines.intersperse("\n".into()).collect::<String>();
251249

252250
let parse_result = match kind {
253251
CodeBlockKind::Fenced(ref lang) => {
@@ -260,7 +258,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
260258
<pre class=\"language-{}\"><code>{}</code></pre>\
261259
</div>",
262260
lang,
263-
Escape(&text),
261+
Escape(&origtext),
264262
)
265263
.into(),
266264
));
@@ -270,6 +268,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
270268
CodeBlockKind::Indented => Default::default(),
271269
};
272270

271+
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
272+
let text = lines.intersperse("\n".into()).collect::<String>();
273+
273274
compile_fail = parse_result.compile_fail;
274275
should_panic = parse_result.should_panic;
275276
ignore = parse_result.ignore;

src/librustdoc/html/markdown/tests.rs

+37
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,40 @@ fn test_find_testable_code_line() {
309309
t("```rust\n```\n```rust\n```", &[1, 3]);
310310
t("```rust\n```\n ```rust\n```", &[1, 3]);
311311
}
312+
313+
#[test]
314+
fn test_ascii_with_prepending_hashtag() {
315+
fn t(input: &str, expect: &str) {
316+
let mut map = IdMap::new();
317+
let output = Markdown {
318+
content: input,
319+
links: &[],
320+
ids: &mut map,
321+
error_codes: ErrorCodes::Yes,
322+
edition: DEFAULT_EDITION,
323+
playground: &None,
324+
heading_offset: HeadingOffset::H2,
325+
}
326+
.into_string();
327+
assert_eq!(output, expect, "original: {}", input);
328+
}
329+
330+
t(
331+
r#"```ascii
332+
#..#.####.#....#.....##..
333+
#..#.#....#....#....#..#.
334+
####.###..#....#....#..#.
335+
#..#.#....#....#....#..#.
336+
#..#.#....#....#....#..#.
337+
#..#.####.####.####..##..
338+
```"#,
339+
"<div class=\"example-wrap\"><pre class=\"language-ascii\"><code>\
340+
#..#.####.#....#.....##..
341+
#..#.#....#....#....#..#.
342+
####.###..#....#....#..#.
343+
#..#.#....#....#....#..#.
344+
#..#.#....#....#....#..#.
345+
#..#.####.####.####..##..
346+
</code></pre></div>",
347+
);
348+
}

0 commit comments

Comments
 (0)