Skip to content

Create a listing link preprocessor to address the in-text linking issue #4289

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mianlang
Copy link
Contributor

@mianlang mianlang commented Mar 21, 2025

Following #4271.

It works by handling the pattern Listing\s(\d+-\d+) and replacing it with <a href="#listing-$1" class="listing-link">.

However, cross-chapter references aren't handled yet–I'm not quite sure how to approach that part at the moment.

And cross-chapter references are handled by a HashMap dictionary.

@mianlang mianlang force-pushed the preprocessor-listing-link branch from 2cec050 to 507a418 Compare March 21, 2025 06:09
@mianlang mianlang mentioned this pull request Mar 21, 2025
@mianlang
Copy link
Contributor Author

One possible approach: first collect all listing numbers along with their corresponding filenames and store them in a dictionary. Then, during the replacement phase, look up the referenced filename in that dictionary using the listing number, take the current filename into account, and construct the correct href.

@mianlang mianlang force-pushed the preprocessor-listing-link branch from 507a418 to d69523a Compare March 21, 2025 14:41
@mianlang mianlang force-pushed the preprocessor-listing-link branch from f784838 to 1eb6da3 Compare March 21, 2025 16:49
@mianlang mianlang changed the title Create a listing link preprocessor Create a listing link preprocessor to address the in-text linking issue Mar 21, 2025
@mianlang
Copy link
Contributor Author

mianlang commented Mar 21, 2025

I got it working – it should now address the in-text linking issue (which is quite exciting from my point of view, though it doesn't affect the print version). Let me know if anything needs to be adjusted to move forward, such as code style, module naming, code organization, or tests – which I suppose there should be quite a bit of.

Just to share one thing I ran into during the process: I'm not sure how to efficiently debug mod.rs. My current loop is: make a change, run cargo install --locked --path packages/mdbook-trpl, rebuild with mdbook build, then check the output HTML. I imagine there must be a more idiomatic or streamlined way.

A fix on broken links was also included, since otherwise the lint would fail. #4295 fixes that, and I'll rebase main after it's merged. This also shows that in the future, if someone forgets to update an outdated listing reference, linkcheck.sh will complain.

Note: cases like references to multiple listings (e.g. "Listings 10-16, 10-17, and 10-23" in Ch. 10) aren't handled yet. Also, this doesn't fully address #921, as neither the referencing of figures nor the figure-with-number component itself has been implemented.

@mianlang mianlang marked this pull request as ready for review March 21, 2025 17:44
@chriskrycho
Copy link
Contributor

Excellent! I’ll try to take a look early next week or over the weekend, though I am also shifting my focus elsewhere and taking some time off for the next four weeks, so I may be quite slow to respond and iterate for that period of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants