lzanini / mdbook-katex

A preprocessor for mdBook, rendering LaTex equations to HTML at build time.
MIT License
205 stars 34 forks source link

Fix #11 #13

Closed kknives closed 3 years ago

kknives commented 3 years ago

Issue

11 was caused by the program using the relative path defined in book.toml:

[preprocessor.katex]
macros = "macros.txt"

This will not work if mdBook is called from outside the book directory as @str4d reported.

Solution

This PR fixes #11 by using the PreprocessorContext's root field, defined here as:

The location of the book directory on disk.

so that the macros file is now located by an absolute path:

book_root.join(PathBuf::from(macros_value))

Refactoring

In order to make a test case for this issue, I had to refactor the part of load_macros that locates the macros file into its own helper function: get_macro_path and then test it instead of load_macros.

This was because, I was not able to mock a PreprocessorContext (no public constructor) that load_macros needs:

fn load_macros(ctx: &PreprocessorContext) -> HashMap<String, String>

Feel free to ask me any questions regarding the changes in this PR

lzanini commented 3 years ago

Thanks for your contributions, and for the detailed explanation ! Merging into master.

kknives commented 3 years ago

@lzanini Thank you for merging my PRs! I would like to help out and refactor more of the code, if you don't mind.

lzanini commented 3 years ago

@kknives I don't mind at all, as long as you document the changes as you did for this PR, you're more than welcome to do so !