A dual Batch+CSS script to convert Markdown to HTML

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jfl
Posts: 140
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

A dual Batch+CSS script to convert Markdown to HTML

#1 Post by jfl » 05 Dec 2019 17:35

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!
Last edited by jfl on 06 Dec 2019 07:50, edited 1 time in total.

Aacini
Expert
Posts: 1669
Joined: 06 Dec 2011 22:15
Location: México City, México

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

#2 Post by Aacini » 06 Dec 2019 07:14

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

aGerman
Expert
Posts: 3933
Joined: 22 Jan 2010 18:01
Location: Germany

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

#3 Post by aGerman » 06 Dec 2019 13:55

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

jfl
Posts: 140
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

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

#4 Post by jfl » 07 Dec 2019 04:52

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. :-)

aGerman
Expert
Posts: 3933
Joined: 22 Jan 2010 18:01
Location: Germany

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

#5 Post by aGerman » 07 Dec 2019 06:11

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

Post Reply