Skip to content

Instantly share code, notes, and snippets.

@ScottJWalter
Last active September 13, 2024 21:56
Show Gist options
  • Save ScottJWalter/e813aa1f6534492e65dd6287b8d14f18 to your computer and use it in GitHub Desktop.
Save ScottJWalter/e813aa1f6534492e65dd6287b8d14f18 to your computer and use it in GitHub Desktop.
Convert HTML to Markdown

Convert HTML to Markdown

This is a simple bash script to convert one or more HTML files to markdown format. I created it to quickly convert a Medium export for import into my Obsidian vault.

Prerequisites

Usage

mkmd path/*.html

Variables

MKMD_SH="https://gist.github.com/ScottJWalter/e813aa1f6534492e65dd6287b8d14f18/raw/d192d2f0c9da5dc070d7b2c979ab8421d6e8223d/mkmd.sh"

With curl:

bash <(curl -sL ${MKMD_SH}) args...

With wget:

bash <(wget -nv -O - ${MKMD_SH}) args...
#!/bin/sh
# -----------------------------------------------------------------------------
#
generate_md() {
if [ ! -f "$1" ]; then
echo " *** file not found: $1"
exit 1
fi
if [ ! -r "$1" ]; then
echo " *** file not readable: $1"
exit 1
fi
# get html file's metadata
f_name=$(basename $1)
f_basename="${f_name%.*}"
f_create=$(stat -c %w "$1")
f_modified=$(stat -c %y "$1")
f_changed=$(stat -c %z "$1")
# if creation (birth) date is blank, use 'change' date
if [ "$f_create" = "-" ]; then
f_create=$f_changed
fi
echo " Processing $f_basename ..."
cat > $f_basename.md << EOF
---
title:
- $f_basename
created: $f_create
updated: $f_modified
tags:
- medium
---
$(html2md -i $1)
EOF
rm $1
echo " .. $f_basename.md created."
}
# =============================================================================
# MAIN
# =============================================================================
#
if [ $# -eq 0 ]; then
echo "usage: $(basename $0) <html file>"
return 0
fi
for f in $@; do
generate_md "$f"
done
echo "Done!"
#
# =============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment