Page 1 of 1

A dual Batch+CSS script to convert Markdown to HTML

Posted: 05 Dec 2019 17:35
by jfl
I've published on my github SysToolsLib library a new batch file called md2h.bat.
This batch converts a Markdown text file into an HTML file, and displays it in a Web browser.
This is useful if you're editing Markdown files, and want to know what they will look like once pushed into GitHub.

For that md2h.bat uses a number of interesting techniques:
  • It is a dual Batch+CSS script.
    This adds to a long line of dual language batch scripts published on this forum. :-) For example see this thread.
    It's done in a rather classic way, by hiding the batch section in a CSS comment.
    The lucky find is that the initial @echo off directive is interpreted by CSS as a style directive, for an <@echo> tag having a child <off> tag, etc.
  • It uses GitHub's own REST MarkDown API to generate the HTML.
    This is interesting because there are many different Markdown dialects, with subtle differences.
    By using GitHub's own APIs, we can be sure that the generated HTML will look exactly the same once pushed into GitHub.
  • As GitHub's MarkDown API does not generate any style sheet, I'm reusing one published in this gist.
  • Currently it uses curl.exe for doing the REST API calls.
    curl.exe is distributed as part of the standard Windows 10 updates since early 2018.
    If you don't have it, you can download it from the Official curl download page for Windows.
    I considered using JavaScript code for doing the same. This would have allowed md2h.bat to work on all versions of Windows without an external dependency on curl...
    But so far I've not found a way to do a triple Batch+CSS+JavaScript script! ;-)
Example of use:

Code: Select all

md2h README.md
Enjoy!

Re: A dual Batch+CSS script to convert Markdown to HTML

Posted: 06 Dec 2019 07:14
by Aacini
This remembers me my old TextToHtml.bat program that converts a BBCode-style text file into a HTML complete page although it is not an hybrid program, but a pure Batch file...

Antonio

Re: A dual Batch+CSS script to convert Markdown to HTML

Posted: 06 Dec 2019 13:55
by aGerman
Works beautifully.
Although I'm wondering why GitHub extensions are not supported if their markdown API is used. E.g. instead of [x] I would have expected to see a checkbox.

Steffen

Re: A dual Batch+CSS script to convert Markdown to HTML

Posted: 07 Dec 2019 04:52
by jfl
There is another API supporting their "gfm" extensions. See their Markdown Rest API page.
I chose to use the API using just the "raw" markdown, because this is the one used for all *.md files in code repositories.
(This raw markdown already includes some extensions versus plain markdown, such as tables.)
My understanding is that the gfm extensions, like the checkbox you mention, are available only for comments in issues or pull requests.
For these, there was less of a need, because the web input fields have a preview tab, allowing to check immediately if what you've typed looks as intended.

Still, we could add an option to the batch, to select one API or the other.
I'll consider favorably any pull request providing that feature. :-)

Re: A dual Batch+CSS script to convert Markdown to HTML

Posted: 07 Dec 2019 06:11
by aGerman
Oh, I see. I already read the linked site but didn't understand the difference in the first place. And honestly, I still do not know what to pass in order to use gfm :oops: Not that important though. Anyway, nice work!

Steffen