Skip to content

Commit e5492e6

Browse files
authored
Merge pull request #4271 from mianlang/main
Anchors on listings
2 parents e380269 + ba072b8 commit e5492e6

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

packages/mdbook-trpl/src/listing/mod.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,13 @@ struct Listing {
221221

222222
impl Listing {
223223
fn opening_html(&self) -> String {
224-
let figure = String::from("<figure class=\"listing\">\n");
224+
let id_attribute = self
225+
.number
226+
.as_ref()
227+
.map(|number| format!(" id=\"listing-{number}\""))
228+
.unwrap_or_default();
229+
230+
let figure = format!("<figure class=\"listing\"{id_attribute}>\n");
225231

226232
match self.file_name.as_ref() {
227233
Some(file_name) => format!(
@@ -233,16 +239,21 @@ impl Listing {
233239

234240
fn closing_html(&self, trailing: &str) -> String {
235241
match (&self.number, &self.caption) {
236-
(Some(number), Some(caption)) => format!(
237-
r#"<figcaption>Listing {number}: {caption}</figcaption>
242+
(Some(number), caption) => {
243+
let caption_text = caption
244+
.as_ref()
245+
.map(|caption| format!(": {}", caption))
246+
.unwrap_or_default();
247+
let listing_a_tag = format!(
248+
"<a href=\"#listing-{number}\">Listing {number}</a>"
249+
);
250+
format!(
251+
r#"<figcaption>{listing_a_tag}{caption_text}</figcaption>
238252
</figure>{trailing}"#
239-
),
253+
)
254+
}
240255
(None, Some(caption)) => format!(
241256
r#"<figcaption>{caption}</figcaption>
242-
</figure>{trailing}"#
243-
),
244-
(Some(number), None) => format!(
245-
r#"<figcaption>Listing {number}</figcaption>
246257
</figure>{trailing}"#
247258
),
248259
(None, None) => format!("</figure>{trailing}"),

packages/mdbook-trpl/src/listing/tests.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ fn main() {}
1818

1919
assert_eq!(
2020
&result.unwrap(),
21-
r#"<figure class="listing">
21+
r##"<figure class="listing" id="listing-1-2">
2222
<span class="file-name">Filename: src/main.rs</span>
2323
2424
````rust
2525
fn main() {}
2626
````
2727
28-
<figcaption>Listing 1-2: A write-up which <em>might</em> include inline Markdown like <code>code</code> etc.</figcaption>
29-
</figure>"#
28+
<figcaption><a href="#listing-1-2">Listing 1-2</a>: A write-up which <em>might</em> include inline Markdown like <code>code</code> etc.</figcaption>
29+
</figure>"##
3030
);
3131
}
3232

@@ -80,16 +80,16 @@ fn get_a_box_of<T>(t: T) -> Box<T> {
8080

8181
assert_eq!(
8282
&result.unwrap(),
83-
r#"<figure class="listing">
83+
r##"<figure class="listing" id="listing-34-5">
8484
8585
````rust
8686
fn get_a_box_of<T>(t: T) -> Box<T> {
8787
Box::new(T)
8888
}
8989
````
9090
91-
<figcaption>Listing 34-5: This has a <code>Box&lt;T&gt;</code> in it.</figcaption>
92-
</figure>"#
91+
<figcaption><a href="#listing-34-5">Listing 34-5</a>: This has a <code>Box&lt;T&gt;</code> in it.</figcaption>
92+
</figure>"##
9393
);
9494
}
9595

@@ -115,9 +115,9 @@ Save the file and go back to your terminal window"#,
115115
assert!(result.is_ok());
116116
assert_eq!(
117117
result.unwrap(),
118-
r#"Now open the *main.rs* file you just created and enter the code in Listing 1-1.
118+
r##"Now open the *main.rs* file you just created and enter the code in Listing 1-1.
119119
120-
<figure class="listing">
120+
<figure class="listing" id="listing-1-1">
121121
<span class="file-name">Filename: main.rs</span>
122122
123123
````rust
@@ -126,10 +126,10 @@ fn main() {
126126
}
127127
````
128128
129-
<figcaption>Listing 1-1: A program that prints <code>Hello, world!</code></figcaption>
129+
<figcaption><a href="#listing-1-1">Listing 1-1</a>: A program that prints <code>Hello, world!</code></figcaption>
130130
</figure>
131131
132-
Save the file and go back to your terminal window"#
132+
Save the file and go back to your terminal window"##
133133
);
134134
}
135135

@@ -153,18 +153,18 @@ This is the closing."#,
153153
assert!(result.is_ok());
154154
assert_eq!(
155155
result.unwrap(),
156-
r#"This is the opening.
156+
r##"This is the opening.
157157
158-
<figure class="listing">
158+
<figure class="listing" id="listing-1-1">
159159
160160
````rust
161161
fn main() {}
162162
````
163163
164-
<figcaption>Listing 1-1: This is the caption</figcaption>
164+
<figcaption><a href="#listing-1-1">Listing 1-1</a>: This is the caption</figcaption>
165165
</figure>
166166
167-
This is the closing."#
167+
This is the closing."##
168168
);
169169
}
170170

0 commit comments

Comments
 (0)