Skip to content
Snippets Groups Projects

md-to-html

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Dan Thomson

    This is a quick and dirty script to help convert Markdown files into HTML by using Gitlab's markdown API.

    md-to-html.sh 2.37 KiB
    #!/bin/bash
    
    usage() {
            echo "usage: $0 [options] <project_path> <file1> [<file2> <file3> ...]" >&2
            echo " <project_path>: The path for your project in Gitlab. Eg. " >&2
            echo "   /server-docs/gfs"
            echo " <file1> [<file2> <file3> ...]: The list of markdown files to" >&2
            echo "   convert. Note that these files should end with '.md' so that" >&2
            echo "   this script knows how to name their .html counterparts" >&2
            echo "" >&2
            echo "  options:" >&2
            echo "    -h:     Print this help message" >&2
            echo "" >&2
            echo "    -t <T>: Use <T> as your Gitlab Private Token" >&2
            echo "            default: empty"
            echo "            This value may also be passed with the GITLAB_TOKEN" >&2
            echo "            environment variable (in fact, it's recommended)" >&2
            echo "    -o <O>: Output directory for .html files" >&2
            echo "            default: $(pwd)" >&2
    }
    
    OUTPUT_DIR=${OUTPUT_DIR-$(pwd)}
    GITLAB_TOKEN=${GITLAB_TOKEN-}
    
    while getopts "ht:" opt; do
            case $opt in
                    h)
                            usage
                            exit 0
                            ;;
                    t)
                            GITLAB_TOKEN="${OPTARG}"
                            ;;
                    o)
                            OUTPUT_DIR="${OPTARG}"
                            ;;
                    *)
                            echo "Invalid option: -$OPTARG" >&2
                            exit 1
                    ;;
            esac
    done
    shift "$((OPTIND-1))"
    
    TEMPDIR=""
    
    trap "{ rm -rf \"${TEMPDIR}\"; }" EXIT
    TEMPDIR="$(mktemp -d)"
    
    mkdir -p "${OUTPUT_DIR}"
    
    for f in $@; do
            DATA=$(sed 's/\"/\\"/g' $f | sed ':a;N;$!ba;s/\n/\\n/g')
            echo "{\"text\":\"$DATA\", \"gfm\":true, \"project\":\"server-docs/gfs\"}" > "${TEMPDIR}/${f/.md/-request.json}"
            if [ -z "${GITLAB_TOKEN}" ]; then
                    curl --header "Content-Type: application/json" --data "@${TEMPDIR}/${f/.md/-request.json}" https://gitlab.triumf.ca/api/v4/markdown | jq -r .html > "${TEMPDIR}/${f/.md/.html}"
            else
                    curl --header "Private-Token: ${GITLAB_TOKEN}" --header "Content-Type: application/json" --data "@${TEMPDIR}/${f/.md/-request.json}" https://gitlab.triumf.ca/api/v4/markdown | jq -r .html > "${TEMPDIR}/${f/.md/.html}"
            fi
    done
    
    mv ${TEMPDIR}/*.html "${OUTPUT_DIR}"
    rm -rf "${TEMPDIR}"
    trap - EXIT
    
    
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment