Created
April 5, 2022 16:42
-
-
Save meleu/f9667e76a2744d46686702edeb3cc77c to your computer and use it in GitHub Desktop.
dg-publish.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# dg-publish.sh | |
################### | |
# | |
# This script adds a "dg-publish: true" to all *.md files that don't have | |
# a "dg-publish: " defined in it's frontmatter. | |
# | |
# Consequences of this: | |
# - non versioned files won't be affected (e.g.: gitignored files) | |
# - files without any frontmatter will have one with "dg-publish: true" | |
# - files with a "dg-publish: " (any value) in its frontmatter won't be touched | |
# | |
# WARNING: understand what this script does before using it!!! | |
# | |
# This script was created to be used in my Obsidian vault and to publish my | |
# digital garden using this awesome plugin: | |
# https://github.com/oleeskild/obsidian-digital-garden | |
# | |
# | |
# meleu - https://meleu.dev/ | |
set -euo pipefail | |
listMdFiles() { | |
git ls-files --exclude='*.md' --ignored --cached | |
} | |
includePublishFrontmatter() { | |
local mdFile="$1" | |
# if file doesn't have a frontmatter, add an empty one | |
[[ $(head -1 "${mdFile}") == '---' ]] \ | |
|| sed --in-place '1i ---\n---' "${mdFile}" | |
# if file doesn't have a "dg-publish:" in the frontmatter | |
# add one defaulting to 'true' | |
sed '1,/---/!d' "${mdFile}" \ | |
| grep --quiet 'dg-publish: ' \ | |
|| sed --in-place '2i dg-publish: true' "${mdFile}" | |
} | |
main() { | |
local mdFile | |
local mdFiles=() | |
mapfile -t mdFiles < <(listMdFiles) | |
for mdFile in "${mdFiles[@]}"; do | |
includePublishFrontmatter "${mdFile}" | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment