-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Translate relative links to other .md files during rendering (fixes #588) #589
Conversation
I think this really belongs as a |
I'll have a look implementing it as a preprocessor. I suspected this might be an issue with other backends as well. Does the preprocessor infrastructure have access to a collection of md files which are valid link destinations? |
So looking at the existing pre-processor support it seems like we have to do the following:
Is this correct? Another thought I had is that different backends might require different handling of cross linking. So this feature might be better suited to be implemented closer to the backends themselves. PDFs as an example, (I think) would want to generate anchors, for which it would be easier if the link destination remained intact. Or similarly if you'd want to build a single-page document. |
When we invoke a preprocessor we give it a representation of the entire book loaded into memory, this gives them an idea of what objects they need to update. I believe they also have access to the book's root directory to enable things like the
That's pretty much exactly what I had in mind!
I guess there's no reason why a The main purpose for |
@Michael-F-Bryan so I just pushed something which is work in progress. Instead of tying the link translation into the This hook could be provided from anywhere, including pre-processors if you want it to. I'm honestly not sure if it makes sense to be part of a Preprocessor as it looks here, because it kind of undermines the So the hook has a generic interface ( I'm investigating this because I'm not too keen about the whole markdown roundtripping that would have to happen if this were to be implemented as a pre-processor. So I'm hoping this could be a more desirable alternative. It sounded like your larger objection was tying render-specific code into the markdown rendering. What do you think? |
Also: If you're interested in this approach I'll figure out how to change the |
So final experiment (promise). I've converted most internal path representations to This has the following benefits:
This also permitted me to easily implement relative path comparisons, so warnings are now printed based on the existing chapters. I've added a test page to the example book, which contains bad links, these now print the following warnings:
|
I just had another look at this and I think there may have been a misunderstanding. I originally thought this would just be a case of changing any At the moment, Additionally, we shouldn't prevent a book from linking outside itself (e.g. links like I'm really sorry about the miscommunication @udoprog! It may be beneficial to break this out into two PRs, one which just changes |
@Michael-F-Bryan The goal is to be backwards compatible. With the last change I pushed there is no difference in how the rust book second edition is rendered, this claims that the resulting html is identical:
We do emit the following warning which we can choose to more selectively ignore, but 3 of them are actually links that should be fixed after this change:
EDIT: this removes these warnings: 5bc7c0d
This still works. I think. Do you have a test that you can try it on?
This still works for two reasons, only relative links are filtered, and only links which point to an existing document will be translated, see: Note that I'd be happy to split them up though, in fact, the commits already are split up :). But the |
@Michael-F-Bryan I've cleaned up and opened #597 separately which contains the conversion to RelativePath. If that looks good, I'd rather have that merged before this to help out with validating link destinations without doing a filesystem check. |
@Michael-F-Bryan if you'd like, I can remove the need to convert the project to use RelativePath for this PR. The last commit is almost standalone. The code might look a bit interesting and not catch all corner cases, but that will remove the dependency. Alternatively we can live with the |
I've now modified this to no longer depend on #597. |
Any updates on this? Or project is dead? |
yes the project is active but this PR would have diverged a lot since it has been inactive for a while. |
@Dylan-DPC I'd be happy to fix it in case there's still interest. |
After some digging I realized a fix for this was already merged in #603 |
On second look, I'm not sure links to other resources (images, static files) are made currently made relative. Has someone tested this? |
I'm trying to avoid as many is_file checks as possible by analysing the destination when possible.
Adds two dependencies:
url
, to conveniently parse URLs to determine if they are relative or not.relative-path
(A crate of mine), parses only forward slash paths and permits convenient decomposition into components that can be translated.This also makes sure that there are no platform-dependent path manipulations.
I can try to avoid using
relative-path
if wanted, buturl
is a bit harder.