-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Variable preprocessor #760
Comments
So in-markdown templates, essentially? No I do not think there is currently a way to do this, but a preprocessor would work well. |
@mattico Yeah, exactly. Do you think it would make sense to be a core preprocessor for mdBook, or would it necessarily be a custom solution? I foresee this being a common need for things exactly like what I'm describing, but I'm much less tuned in to the needs of the average user here. |
I've never seen a need for it, but since the core audience of mdBook is programmers I think most would understand and appreciate the feature. I do think we'll want a more modular interface for configuring preprocessors at some point, so that we can add them more easily for niche use cases. |
With @Michael-F-Bryan 's PR for #658, it supports
My suggestion would be to pick something off the shelf that can do this - and what instantly comes to mind is ``{{ templating }}` - The Python Jinja format .. which is also common with "go" templates. Rust has it's own - https://github.com/djc/askama So perhaps a preprocessor that, at a minimum, provides the "config" of the preprocessor, plus ENV_VARS into the conversion. Seems simple enough. it would just need to co-exist with the |
As a related feature to having variables, a way to put conditionals in markdown would allow different outputs from the same source based on environment variables or configuration. A use case that's relevant to our company would be to bundle a help manual in a web application on multiple different embedded devices with a few different configuration options. It would be ideal to be able to conditionally render words or sections of markdown, or conditionally include different pages in the SUMMARY.md file. I think a Handlebars preprocessor would be a valid and coherent way to make this work (seeing how mdBook uses Handlebars already)? Handlebars has a form capable of conditionally rendering words or paragraphs of markdown, e.g.: |
Askama vs Handlebars... thoughts? Askama will be much faster (benchmarks), Handlebars might be more well known, but Askama isn't difficult... I think having a templating language for variables and conditionals, is important for any static site generator. |
hi All, Looking for some feedback. |
Adding to this, if you like Tera templates, see https://github.com/avitex/mdbook-tera |
Have you thought about environment variables? I feel like people could want to use that to inject git hashes when publishing a document and wanting to link back to the source on GitHub. |
Hi @Michael-F-Bryan, Yes that is not a bad idea i will have a quick look how that could be done. Regards |
Hi @Michael-F-Bryan, Have a look: https://gitlab.com/tglman/mdbook-variables/-/commit/2d628ce6ef5c243659a152ef2770ef42eeb4608f not sure though about precedence and how to enabled that, feel free to open an issue on that repository with suggestions. Bye |
Neither mdbook-variables, nor mdbook-tera have binaries available....... do I really have to install rust..? |
Hi @tglman! is there a way to use the variables preprocessor inside de SUMARRY.md? I'm trying to use variables for file paths to respect DRY pattern but mdbook doesn't seem to like it. |
This probably isn't possible. Preprocessors are only run on the chapters of a book once they've been loaded into memory, by which time the |
Hi, Yep as @Michael-F-Bryan said is not possible, and to make it possible is actually needed a bit of architecture change in mdBook itself. Bye |
thanks both of you for clarifying that. Makes me sad, though. |
Just noting this is now fixed in |
A preprocessor is transforming the JSON of the book, and
See Wilfred/difftastic@80429e9 for the full configuration I used. |
Hi all, just adding my own two cents to this thread. I also wanted to have templates included in my code as I found that I wanted a solid footer included on all pages. From that, I designed mdbook-template which is a file-based config and very similar to how the current It's by no means perfect but allows for default values and dynamic variables to be passed too 👍 |
During a discussion for the Amethyst book, we realized we wanted a way to put in doc badges dynamically dependent on the current building version of the Amethyst library.
Is there a way to read from some sort of global key value store (such as environment variables or the configuration) while rendering the markdown? I believe a preprocessor could accomplish this task. Something like
(the idea being
#[variableName]
would be the way to access a variable -- probably a terrible particular example)If there is such a way to do this, I don't think it's documented.
The text was updated successfully, but these errors were encountered: