Skip to content

Commit ba072b8

Browse files
committed
change listing text to <a> tag in figcaption to provide link
1 parent 5bcf1bf commit ba072b8

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,21 @@ impl Listing {
239239

240240
fn closing_html(&self, trailing: &str) -> String {
241241
match (&self.number, &self.caption) {
242-
(Some(number), Some(caption)) => format!(
243-
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>
244252
</figure>{trailing}"#
245-
),
253+
)
254+
}
246255
(None, Some(caption)) => format!(
247256
r#"<figcaption>{caption}</figcaption>
248-
</figure>{trailing}"#
249-
),
250-
(Some(number), None) => format!(
251-
r#"<figcaption>Listing {number}</figcaption>
252257
</figure>{trailing}"#
253258
),
254259
(None, None) => format!("</figure>{trailing}"),

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

Lines changed: 12 additions & 12 deletions
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" id="listing-1-2">
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" id="listing-34-5">
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,7 +115,7 @@ 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
120120
<figure class="listing" id="listing-1-1">
121121
<span class="file-name">Filename: main.rs</span>
@@ -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
158158
<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)