brrd / abricotine

Markdown editor with inline preview
GNU General Public License v3.0
2.63k stars 156 forks source link

Don’t include MathJax or highlight.js in HTML export if a file doesn’t have math or code #114

Closed nloveladyallen closed 8 years ago

brrd commented 8 years ago

Hmm... it means parsing the whole document once again to check only math and code blocks (with usual potential errors) and providing if/else statements in templating syntax + two more specific variables... This sounds really tricky in my opinion.

This is exactly the reason why I would like to provide post-save hooks (see my comments in #14) to let user write his own specific postprocessor according to his needs.

nloveladyallen commented 8 years ago

I don't know if it's also too complicated, but this should work:

if (document.querySelector("script[type=\"math/tex; mode=display\"]") !== null) {
    var mathjax = document.createElement("script");
    mathjax.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML";
    document.head.appendChild(mathjax);
}
if (document.getElementsByTagName("code").length !== 0) {
    var highlight = document.createElement("script");
    var highlightcss = document.createElement("link");
    highlight.src = "http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/highlight.min.js";
    highlightcss.rel = "stylesheet";
    highlightcss.href = "http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/styles/default.min.css";
    highlight.onload = function() {hljs.initHighlighting();};
    document.head.appendChild(highlight);
    document.head.appendChild(highlightcss);
}
brrd commented 8 years ago

OK, I didn't get you wanted to do this directly in the export template. I'm not really convinced of the utility of this extra code... but why not.

You can commit this in defaults and in https://github.com/brrd/Abricotine-templates (I would probably merge this repo into Abricotine main repo in a future release)

darahak commented 8 years ago

Is there a good reason to keep pulling these scripts from CDNs instead of adding them as npm dependencies?

I would expect a text editor to completely work offline.

nloveladyallen commented 8 years ago

@darahak You mean loading the script from inside the app bundle or config dir? That would be a cross-platform nightmare, not to mention breaking if the user tries to open the file on a computer without Abricotine.

darahak commented 8 years ago

I mean bundling these scripts with the HTML export, after copying them from the app directory.

Anyway I don't even know all the use cases for HTML export, so it might not be relevant.

I just thought an online dependency could be an issue.

nloveladyallen commented 8 years ago

@darahak See #115

brrd commented 8 years ago

@nloveladyallen Thank you for contributing 👍

@darahak I created #128 which could be a way to fix this.