Skip to content

Commit 5bcf1bf

Browse files
gaoyuanchriskrycho
authored andcommitted
add id to Listing with number attribute for cross reference
update tests after new attribute id added Co-authored-by: Chris Krycho <[email protected]>
1 parent e380269 commit 5bcf1bf

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

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

Lines changed: 7 additions & 1 deletion
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!(

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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
@@ -80,7 +80,7 @@ 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> {
@@ -117,7 +117,7 @@ Save the file and go back to your terminal window"#,
117117
result.unwrap(),
118118
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
@@ -155,7 +155,7 @@ This is the closing."#,
155155
result.unwrap(),
156156
r#"This is the opening.
157157
158-
<figure class="listing">
158+
<figure class="listing" id="listing-1-1">
159159
160160
````rust
161161
fn main() {}

0 commit comments

Comments
 (0)