Skip to content

Instantly share code, notes, and snippets.

@lebarde
Last active April 30, 2018 04:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lebarde/3b49a8ff8ea9940829559c3e439bee9c to your computer and use it in GitHub Desktop.
Save lebarde/3b49a8ff8ea9940829559c3e439bee9c to your computer and use it in GitHub Desktop.
Convert Markdown to HTML and leave the YAML verbatim (to use in Hugo)
#!/bin/sh
# This script converts a .md Markdown file into a HTML file.
# It takes advantage of Pandoc for that, but keeps the front
# matter verbatim in order to process it into Hugo.
# tmp file:
TMP=`mktemp`
BASE=`basename $1 .md`
DIR=`dirname $1`
HTML="${DIR}/${BASE}.html"
# 1) Get the frontmatter into the final html file
sed -n '1 { /^---/ { :a N; /\n---/! ba; p} }' $1 > ${HTML}
# 2) Get the rest of the Markdown content in a temp file
sed '1 { /^---/ { :a N; /\n---/! ba; d} }' $1 >> ${TMP}
# 3) Convert Markdown into HTML using pandoc and append it to html file
pandoc -f markdown-yaml_metadata_block -t html --parse-raw ${TMP} >> ${HTML}
# 4) Remove the draft line
grep -v "draft:\s*true" ${HTML} > ${TMP}
mv ${TMP} ${HTML}
# LAST: The temporary file has been moved into $HTML,
# there is no need to delete it.
#rm ${TMP}
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment