Skip to content

Instantly share code, notes, and snippets.

@abhishekkr
Last active October 22, 2021 10:58
Show Gist options
  • Save abhishekkr/f3ebf2ce6058c13bf619 to your computer and use it in GitHub Desktop.
Save abhishekkr/f3ebf2ce6058c13bf619 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Download HTML converted from provided Markdown, using GitHub API v3
##
md2html(){
if [[ $# -ne 2 ]]; then
echo "ERROR.\nSYNTAX: Markdown_To_HTML <markdown-filepath> <dest-html-filepath>"
return
fi
unset _markdown_filepath
unset _html_filepath
unset _github_json_filepath
unset _markdown_for_github
_markdown_filepath=$1
_html_filepath=$2
_github_json_filepath="${_markdown_filepath}.json"
sed -i 's/$/\\n/g' $_markdown_filepath
sed -i 's/"/\\"/g' $_markdown_filepath
echo "{ \"text\" : \"" > $_github_json_filepath
cat $_markdown_filepath >> $_github_json_filepath
echo "\" }" >> $_github_json_filepath
cat $_github_json_filepath | curl -sLk -X POST -d@- https://api.github.com/markdown > $_html_filepath
rm $_github_json_filepath
echo "Successful conversion of ${_markdown_filepath} to ${_html_filepath}."
}
@smarx
Copy link

smarx commented Aug 25, 2014

GitHub has a "raw" version of this endpoint just to make this sort of thing easier. So this whole thing could be replaced with the following alias:

$ alias md='curl https://api.github.com/markdown/raw -s -XPOST -H Content-Type:text/plain -d @-'

Usage:

$ echo '**Hello, World!**' | md
<p><strong>Hello, World!</strong></p>

Of course, to use it more like this script, you could read and write files:

$ md < test.md > test.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment