Skip to content
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

Embed highlight.js files #8

Open
AndrewRadev opened this issue Feb 26, 2021 · 0 comments
Open

Embed highlight.js files #8

AndrewRadev opened this issue Feb 26, 2021 · 0 comments
Labels
easy-ish Should be relatively straightforward rust

Comments

@AndrewRadev
Copy link
Owner

AndrewRadev commented Feb 26, 2021

This one might be done quite easily by using include_str... but it would be a lot of repetitive lines, since highlight.js has tons of separate files with different language support. It could all be packed into one file and exported, but it would be a large file, with lots of stuff that would never be used.

Currently, the code knows which code languages are being used on the markdown page, so there's no issue in including just the right files. So an "easy" way to solve this issue is something like:

  • Create a folder in res with an import of highlight.js files.
  • Create a hashmap, maybe with lazy_static or once_cell with include_str lines for these.
  • Package them up in a struct with a sensible interface to write these script files like we do here:

    quickmd/src/assets.rs

    Lines 88 to 93 in 9e09b02

    fs::write(output_path.join("main.js"), MAIN_JS).
    unwrap_or_else(|e| warn!("{}", e));
    fs::write(output_path.join("main.css"), MAIN_CSS).
    unwrap_or_else(|e| warn!("{}", e));
    fs::write(output_path.join("github.css"), GITHUB_CSS).
    unwrap_or_else(|e| warn!("{}", e));

An interesting, more complicated idea, might be to make this reusable, maybe even pull this concept into a separate crate. Something like an include_dir! proc macro that recursively reads the whole contents of a directory, and does the whole lazy-static hashmap setup above, it just doesn't require somebody to manually go over the individual files in the source. The hashmap that include_dir! spits out could be keyed by a Path. The contents might even be compressed like include_flate does, but that's a separate thing.

@AndrewRadev AndrewRadev added easy-ish Should be relatively straightforward rust labels Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy-ish Should be relatively straightforward rust
Projects
None yet
Development

No branches or pull requests

1 participant