From a4d4d033a20764e74b115d39315ea0293522accc Mon Sep 17 00:00:00 2001 From: Mitchell Burton Date: Sun, 21 Jan 2024 14:06:19 -0800 Subject: [PATCH] support md-based titles, allow for modified env roots --- .github/workflows/static.yml | 3 +-- README.md | 5 ++++- convert.sh | 32 ++++++++++++++++++++++++++------ env | 2 ++ markdown/index.md | 5 ++++- markdown/subpage/index.md | 4 ++++ templates/base.html | 2 +- 7 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 env create mode 100644 markdown/subpage/index.md diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index ae9746b..bd2f367 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -37,8 +37,7 @@ jobs: run: | sudo apt update sudo apt install markdown - echo ${{ steps.deployment.outputs.page_url }} - SITE_ROOT="/simple-site" ./convert.sh + ./convert.sh - name: Upload artifact uses: actions/upload-pages-artifact@v3 with: diff --git a/README.md b/README.md index d797e38..9916bf9 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,10 @@ For my actual website source see [this repo][1]. 2. Make sure github pages is turned on and the source is set to "Github Actions" 3. Put your markdown files in `./markdown`, make sure they end with `.md` 4. Replace the `favicon.ico` of my face with your own, or delete it -5. Commit them and push +5. Change `SITE_ROOT` in `./env` to match where your site will be served +6. Commit and push + +**Note**: If your markdown file starts with a H1 header, That header will also be used as the title of the page. ## Custom CSS and Javascript diff --git a/convert.sh b/convert.sh index 57a6640..6c3e690 100755 --- a/convert.sh +++ b/convert.sh @@ -1,21 +1,41 @@ #!/usr/bin/env sh -# A simple-site converted. Wraps the html produced from markdown in a few tags +# A simple-site convertor. Wraps the html produced from markdown in a few tags # to make it web-servable. +. ./env + FILES=$(find ./markdown -type f -name '*.md') rm -rf ./_site -mkdir -p ./_site -for FILE in $FILES; do - HTML_PATH=$(echo "$FILE" | sed -e 's/\.md$/\.html/' | sed -e 's/^\.\/markdown/\.\/_site/') +convert_file() { + HTML_PATH=$(echo "$1" | sed -e 's/\.md$/\.html/' | sed -e 's/^\.\/markdown/\.\/_site/') + + mkdir -p "$(dirname "$HTML_PATH")" + + FIRST_LINE=$(head -n 1 "$1") + + if echo "$FIRST_LINE" | grep -q '^# '; then + PAGE_TITLE=$(echo "$FIRST_LINE" | cut -c3-) + else + PAGE_TITLE=$SITE_TITLE + fi + echo "Creating $HTML_PATH" - sed -e "s:{{SITE_ROOT}}:$SITE_ROOT:g" ./templates/base.html > "$HTML_PATH" - markdown --html4tags "$FILE" >> "$HTML_PATH" + sed -e "s:{{SITE_ROOT}}:$SITE_ROOT:g" ./templates/base.html | \ + sed -e "s:{{PAGE_TITLE}}:$PAGE_TITLE:g" \ + > "$HTML_PATH" + markdown --html4tags "$1" >> "$HTML_PATH" cat ./templates/close.html >> "$HTML_PATH" +} + +for FILE in $FILES; do + convert_file "$FILE" & done +wait + mkdir -p ./_site/css mkdir -p ./_site/js cp ./css/base.css ./_site/css/base.css diff --git a/env b/env new file mode 100644 index 0000000..4ee9549 --- /dev/null +++ b/env @@ -0,0 +1,2 @@ +export SITE_ROOT="/simple-site" +export SITE_TITLE="Simple Site" diff --git a/markdown/index.md b/markdown/index.md index be0b863..1ba40bd 100644 --- a/markdown/index.md +++ b/markdown/index.md @@ -1,4 +1,7 @@ -# Homepage +# Simple Site Welcome to the homepage! +There's a subpage [here][1]. + + [1]: subpage "A test subpage" diff --git a/markdown/subpage/index.md b/markdown/subpage/index.md new file mode 100644 index 0000000..1f1be76 --- /dev/null +++ b/markdown/subpage/index.md @@ -0,0 +1,4 @@ +# Subpage + +This is a subpage. You can go [back to home](../ "Back home"). + diff --git a/templates/base.html b/templates/base.html index ea36322..27993d1 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,7 +2,7 @@ - Simple Site + {{PAGE_TITLE}}