hexojs / hexo-renderer-pandoc

A pandoc-markdown-flavor renderer for hexo.
https://npmjs.com/hexo-renderer-pandoc
MIT License
118 stars 33 forks source link

Is it possible to render external package? #40

Closed fomiuna closed 3 years ago

fomiuna commented 3 years ago

Hello! I'm considering writing some pseudocode in my markdown articles. I just found a reply https://tex.stackexchange.com/questions/452552/algorithm-pseudocode-in-markdown to solve this by adding \usepackage in front matter. But it's .md converting to .pdf. So is there any way to do this in the hexo page using hexo-render-pandoc? Thanks for your help!

Ritsuka314 commented 3 years ago

It is in principle possible, but likely a very involved process.


First of all, don't expect pandoc's capability on this. It can't. Skip to the next section if you don't care why I claim this.

Here is the mechanism: Hexo passes your .md post to this plugin, which passes it to pandoc. Pandoc's markdown reader will read the .md post into its internal universal representation, where the \begin{algorithm}...\end{algorithm} part will be represented as a RawBlock, which is used for all non-universal document structure (pandoc considers structures such as headers, paragraphs, etc. as universal document structures, i.e., ones that are present in most, if not all, formats, i.e., md, html, latex).

When the output format is pdf, as is in the stackexchange post, pandoc will emit a latex file and then send it to, for example, pdflatex to generate the pdf output. In this case, rawblocks containing valid latex code can just be emitted as-is, as pdflatex knows how to deal with them. Pandoc itself does not understand latex: even though it has a latex reader, pretty much it does is wrapping whatever it does not understand in rawblocks and let pdflatex deal with them.

But when the output format is html as is in our case, pandoc is only able to process rawblocks that contain latex math, see https://github.com/jgm/pandoc/blob/master/src/Text/Pandoc/Writers/HTML.hs#L810, which will eventually be wrapped into some suitable format and processed by mathjax or katex. All other rawblocks are simply discarded, including the one containing our \begin{algorithm}...\end{algorithm}. In our case this is because pandoc does not know how to deal with it when outputting to html, it no longer has the help of pdflatex.


I can propose one way to achieve your goal, but mind you it is very involved.

You may write a pandoc filter that scans pandoc's internal representation for rawblocks that contains latex code. Then you can have your filter send those code to pdflatex to compile these pieces individually as standalone documents, and finally include them as images in your main post.