dots


dots / config / .local / bin
xxwhirlpool  ·  2026-03-12

md2html

 1#!/usr/bin/env bash
 2#
 3# convert markdown to HTML
 4# can read from STDIN or file
 5#
 6# this is a *very* opinionated script, using:
 7# gum, bat, perl, openssl, and xclip
 8# pandoc is mandatory!!!
 9# edit to suit your system
10
11# functions
12fromstdin() {
13	# declare some vars
14	MDFILE=$(mktemp --suffix .md)
15	HTMLFILE="${MDFILE%.*}.html"
16	HTMLFILE_FINAL="${HTMLFILE%.*}_final.html"
17	# prompt user to paste markdown formatted text, then write it to a temp file
18	gum write --header="paste MD-formatted text below" > "$MDFILE"
19	# the conversion!
20	pandoc --no-highlight --wrap=none "$MDFILE" -o "$HTMLFILE"
21	# swap unicode/HTML entities to their actual characters
22	perl -C -MHTML::Entities -pe 'decode_entities($_);' < "$HTMLFILE" > "$HTMLFILE_FINAL"
23	# show final file
24	batcat --paging=never "$HTMLFILE_FINAL"
25	# copy file to clipboard too
26	xclip -sel clip "$HTMLFILE_FINAL"
27	# cleanup
28	rm "$HTMLFILE"
29}
30
31fromfile() {
32	# declare some vars
33	MDFILE=$(gum file)
34	HTMLFILE="${MDFILE%.*}.html"
35	HTMLFILE_FINAL="${HTMLFILE%.*}_final.html"
36	# the conversion!
37	pandoc --no-highlight --wrap=none "$MDFILE" -o "$HTMLFILE"
38	# swap unicode/HTML entities to their actual characters
39	perl -C -MHTML::Entities -pe 'decode_entities($_);' < "$HTMLFILE" > "$HTMLFILE_FINAL"
40	# show final file
41	batcat --paging=never "$HTMLFILE_FINAL"
42	# copy file to clipboard too
43	xclip -sel clip "$HTMLFILE_FINAL"
44	# cleanup
45	rm "$HTMLFILE"
46}
47
48# execute it
49choice() {
50	choose=$(gum choose "paste" "file")
51	case "$choose" in
52		*paste) fromstdin ;;
53		*file) fromfile ;;
54		*) exit ;;
55	esac
56}
57
58choice