-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Anchors on listings #4271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Anchors on listings #4271
Conversation
Sorry for not having test locally and failling the check. |
Linting and package tests have passed. Not sure whether I should add a test for a listing without a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for doing this! I think the best move here is a slight variation on what you've done: Keep the figure id="..."
the same (with one tweak suggested on the code itself, just a minor style nit), but instead of manually coding in the links throughout, which will impose a maintenance burden on contributors to keep up to date, let’s instead update the <figcaption>
part of the output to link the headers, so we’ll end up with output like this:
<figcaption>
<a href="listing-{number}">Listing {number}</a>: {caption}
</figcaption>
That will make it easy for people to link to using that link itself, and critically means that the only changes anyone will ever need to make to it will be updating the number at <Listing number="1-1" ...>
and the text of the reference to it, not also the link to it. As silly as it might seem (why would anyone forget that?) I can tell you that my experience updating a bunch of listings lately is: it’s really easy to miss or mess up different parts of it, so the more we can get it to be right without manual checking the better that will be for us.
The other side of that is: we don’t really need the links inline in the text in the same way in the general case, though we might consider doing it in a few cases when linking across chapters. I say “might” because of the aforementioned “it’s hard to make sure you got the updates all correct” dynamic, and the link-checker will generally not help us here unless we happen to be linking to a numbered listing that we deleted along with all consecutive listings in that chapter—it’ll just end up pointing to the wrong listing. (That doesn’t happen very often, as you can probably imagine!) Net, you’ll want to do something like a git restore src --source main
to get back all the content without the links.
Thanks again! I look forward to landing this with those tweaks in place.
Great idea! I did considered keeping listings and refereces synced for easier maintenance, but I turned for some sort of service registry system and found it too complex. Are you suggesting extending the Listing component so that it not only render the listing itself but also supports rendering a reference (an <!-- create a listing -->
<Listing number="1-2" file-name="src/main.rs" caption="Some *text*, yeah?">
some snippets
</Listing>
<!-- later reference it -->
It should look similar to the code in Listing 1-2. <!-- original. can't navigate -->
It should look similar to the code in [Listing 1-2](#listing-1-2). <!-- delivered in PR, navigate to the listing -->
It should look similar to the code in <Listing number="1-2" link />. <!-- you suggested. am i right? --> When we need to change, simply search and replace all I believe we can split this into two. Firstly, what was happening and people complained here. I think the biggest problem is, text like Secondly, maintenance considerations. Adding a new reference is intuitive and including an extra target link is reasonable. Changing filename or the number of listing, may introduce additional overhead. Thorough search and confirmation before making such changes can mitigate. Implementing your suggestion would help a lot, replacing all I'll resolve the conflicts and incorporate your suggestion on |
There were 2 lint errors at the main HEAD, duplicate listing number Since there are many conflicts and I find it hard to check and apply changes one by one, I re-executed those steps. I discovered another benefit of this. After making the preceding changes, I ran the lint check, and it complained a broken link fragment: |
Ah, that’s an interesting idea, but no: the main thing I was suggesting was to make the Listing itself contain a link in its output, so that this input: <Listing number="1-2" caption="Something interesting" file-name="src/main.rs">
```rust
fn main() {
println!("Hello, world!");
}
```
</Listing> Would generate output like this: <figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
```rust
fn main() {
println!("Hello, world!");
}
```
<figcaption>
<a href="listing-1-2">Listing 1-2</a>: Something interesting
</figcaption>
</figure> Note the link here: This doesn’t fully solve the in-text linking issue that people originally called out, and that your PR, but it is a good starting point for making it easy to link to them. Then there is separately a question of how to do in-text links to those—that is, the problem you set out to solve here in the first place! My very, very strong inclination is that we should absolutely not do that manually. As one of the main people who is maintaining this, maintenance is not a secondary concern at all, nor something to be addressed later. 😅 Rather the opposite, in fact! One thing we could do, however, is create a preprocessor that takes the text and finds any instance of <a href="$1" class="listing-link">Listing $1</a> That will be pretty robust in that as long as the text is correct, the link will be as well, and it means that neither contributors nor maintainers need to think about doing something funky there; we can just write a normal reference to a listing in text and have it be auto-linked in the web version while not needing to do any work for the Net, I think this is how we should proceed:
Thanks for your patience as we iterate toward a good, easy-to-maintain solution here! |
Steps 1 and 2 are done. #4287 fixes step 3 and should be merged first, as this PR doesn't pass lint without that change. Now I finally see the value of making a self–referencing I truly admire your ability to organize thoughts and pinpoint key issues, and I completely agree that maintainability should be the top priority. The preprocessor approach is certainly a cleaner solution. Thanks for your patience and for your thoughtful, detailed review–it means a lot! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! As for the careful and detailed review—you’re very welcome, and thanks again in turn for taking the time to make this really good. Let me know if you’d like to implement the other new preprocessor; otherwise I’m happy to do it. If you do, the existing examples should be a good guide about how to do it. I’ll take responsibility for landing that upstream in rust-lang/rust either way!
update tests after new attribute id added Co-authored-by: Chris Krycho <[email protected]>
I've rebased main to adopt that lint fix and passed checks. I'd pretty much like to implement the new preprocessor, and I actually gave it a try–with some help from Copilot–and managed to get something working in #4289. One major thing blocking me at the moment is that I have no idea how to handle cross-chapter references. If you have any ideas I could look at, that would be super helpful! |
Update books ## rust-lang/book 23 commits in 81a976a237f84b8392c4ce1bd5fd076eb757a2eb..45f05367360f033f89235eacbbb54e8d73ce6b70 2025-03-21 23:23:52 UTC to 2025-03-13 14:14:37 UTC - Ch. 21: call out Chrome multiple-connections issue (rust-lang/book#4297) - Ch. 16: refactor 16-6 to using listing component (rust-lang/book#4295) - Ch. 01: Show how to work offline (rust-lang/book#4294) - Ch. 07: Clarify sentences about `pub use` (rust-lang/book#4293) - Ch. 02: Consistent ordering of `use` statements (rust-lang/book#4292) - Anchors on listings (rust-lang/book#4271) - Ch. 17: another tweak to how we phrase things about sections (rust-lang/book#4288) - Ch. 20: correct listing number (rust-lang/book#4287) - Ch. 10.3: clarify language detail (rust-lang/book#4284) - Ch. 17: minor typos and link reference (rust-lang/book#4286) - Ch. 9: correctly demonstrate privacy with module (rust-lang/book#4282) - Ch. 18: correct discussion of delegation in `Post` methods (rust-lang/book#4281) - Ch. 20: tell folks to see the Reference for more ABI info (rust-lang/book#4165) - Ch 10.1 minor clarifications (rust-lang/book#4256) - Clarified the misunderstanding b/w crates, module, items (rust-lang/book#4232) - Ferris: always show, even when it’s small (rust-lang/book#4280) - Ch. 17: mention `use std::pin::{Pin, pin};` on introduction (rust-lang/book#4279) - Persist printing error, NOT ErrorKind (rust-lang/book#4259) - Typo: "2" should be "2 seconds" (rust-lang/book#4263) - Ch. 17: fix tiny example consistency issue (rust-lang/book#4270) - Bump ring from 0.17.8 to 0.17.13 in /listings/ch17-async-await/listing-17-02 (rust-lang/book#4261) - Bump ring from 0.17.8 to 0.17.14 in /packages/trpl (rust-lang/book#4273) - 2024 Print Edition: updates to Word docs and more fixes to Markdown text (rust-lang/book#4272) ## rust-lang/reference 5 commits in dda31c85f2ef2e5d2f0f2f643c9231690a30a626..e95ebdfee02514d93f79ec92ae310a804e87f01f 2025-03-24 15:56:46 UTC to 2025-03-18 02:25:06 UTC - Fix diagnostic attribute typo (rust-lang/reference#1767) - Mention that “every address” ≠ “every pointer” (rust-lang/reference#1761) - Rework range pattern rules (rust-lang/reference#1756) - Use warning block in behavior-considered-undefined (rust-lang/reference#1759) - Add reference for asm-goto (rust-lang/reference#1693)
Rollup merge of rust-lang#138894 - rustbot:docs-update, r=ehuss Update books ## rust-lang/book 23 commits in 81a976a237f84b8392c4ce1bd5fd076eb757a2eb..45f05367360f033f89235eacbbb54e8d73ce6b70 2025-03-21 23:23:52 UTC to 2025-03-13 14:14:37 UTC - Ch. 21: call out Chrome multiple-connections issue (rust-lang/book#4297) - Ch. 16: refactor 16-6 to using listing component (rust-lang/book#4295) - Ch. 01: Show how to work offline (rust-lang/book#4294) - Ch. 07: Clarify sentences about `pub use` (rust-lang/book#4293) - Ch. 02: Consistent ordering of `use` statements (rust-lang/book#4292) - Anchors on listings (rust-lang/book#4271) - Ch. 17: another tweak to how we phrase things about sections (rust-lang/book#4288) - Ch. 20: correct listing number (rust-lang/book#4287) - Ch. 10.3: clarify language detail (rust-lang/book#4284) - Ch. 17: minor typos and link reference (rust-lang/book#4286) - Ch. 9: correctly demonstrate privacy with module (rust-lang/book#4282) - Ch. 18: correct discussion of delegation in `Post` methods (rust-lang/book#4281) - Ch. 20: tell folks to see the Reference for more ABI info (rust-lang/book#4165) - Ch 10.1 minor clarifications (rust-lang/book#4256) - Clarified the misunderstanding b/w crates, module, items (rust-lang/book#4232) - Ferris: always show, even when it’s small (rust-lang/book#4280) - Ch. 17: mention `use std::pin::{Pin, pin};` on introduction (rust-lang/book#4279) - Persist printing error, NOT ErrorKind (rust-lang/book#4259) - Typo: "2" should be "2 seconds" (rust-lang/book#4263) - Ch. 17: fix tiny example consistency issue (rust-lang/book#4270) - Bump ring from 0.17.8 to 0.17.13 in /listings/ch17-async-await/listing-17-02 (rust-lang/book#4261) - Bump ring from 0.17.8 to 0.17.14 in /packages/trpl (rust-lang/book#4273) - 2024 Print Edition: updates to Word docs and more fixes to Markdown text (rust-lang/book#4272) ## rust-lang/reference 5 commits in dda31c85f2ef2e5d2f0f2f643c9231690a30a626..e95ebdfee02514d93f79ec92ae310a804e87f01f 2025-03-24 15:56:46 UTC to 2025-03-18 02:25:06 UTC - Fix diagnostic attribute typo (rust-lang/reference#1767) - Mention that “every address” ≠ “every pointer” (rust-lang/reference#1761) - Rework range pattern rules (rust-lang/reference#1756) - Use warning block in behavior-considered-undefined (rust-lang/reference#1759) - Add reference for asm-goto (rust-lang/reference#1693)
I think I made it by the following four main steps, as the first four commits said:
add id to Listing with number attribute for cross reference
Finally make my change in listing mod into effect by
cargo install --locked --path packages/mdbook-trpl
after mod updated.Now each rendered snippet automatically has an
id
attribute likeid="listing-16-6"
for referencing.change listing reference from text to link
Replace
Listing[ \n](\d+[\d-]+)
to[Listing $1](#listing-$1)
. Change 672 results in 72 files.fix: prefix the actual file name if referenced listing is in another ch.md
Using script generated by such prompt:
Manually, fix 3 results in 2 files.
Another two commits to fix linting and package tests:
fix lint errors
Which is caused by omissions in step 3.
update tests after new attribute id added
Let me know if anything should get improved. @chriskrycho #921