Skip to content

Instantly share code, notes, and snippets.

@jag3773
Last active June 26, 2022 14:45
Show Gist options
  • Save jag3773/5797912d1fc6616e55af88a6d10c6d41 to your computer and use it in GitHub Desktop.
Save jag3773/5797912d1fc6616e55af88a6d10c6d41 to your computer and use it in GitHub Desktop.
Scripture Reference Fixer for Obsidian
#!/usr/bin/env bash
# This turns natural references into wiki link references like this:
# Genesis 12:3 -> [[Genesis 12#12:3]]
# Psalm 119:9-11 -> [[Psalm 119#119:9-11]]
# 1 John 3 -> [[1 John 3]]
# First John 1 -> [[1 John 1]]
# Put this file into .git/hooks/ and it will run every time you
# commit into your repository. Make sure it is executable, you can run
# chmod +x .git/hooks/pre-commit
# Requires the gsed utility on Mac. For Linux, change `gsed` to `sed` below (line 63).
### BEGIN USER CONFIG ###
# You need to fill in the path to your git-backed Obsidian vault.
PATHTOVAULT=
# Where are your markdown files? Uncomment the option you want
#MARKDOWNFILES="*/*.md" # look 1 directory deep
#MARKDOWNFILES="resources/notes/*.md" # only look in the notes directory
### END USER CONFIG ###
cd "$PATHTOVAULT"
GREPREGEX='[0-9][0-9]*[ ,;.:?)]'
SEDVSREGEX='\([0-9]*\)\(:[0-9]*[-0-9]*\)/\[\[\1 \2#\2\3\]\]/g'
SEDCPREGEX='\([0-9]*\)\([ ,;.]\)/\[\[\1 \2\]\]\3/g'
IFS=$'\n'
for x in `/usr/bin/grep -l -i \
-e "[123] [JPTCSKjptcsk][a-z]* $GREPREGEX" \
-e "[Ff]irst [JPTCSKjptcsk][a-z]* $GREPREGEX" \
-e "[Ss]econd [JPTCSKjptcsk][a-z]* $GREPREGEX" \
-e "[Tt]hird [JPTCSKjptcsk][a-z]* $GREPREGEX" \
-e "[123][snr][td] [JPTCSKjptcsk][a-z]* $GREPREGEX" \
-e "Genesis $GREPREGEX" -e "Exodus $GREPREGEX" \
-e "Leviticus $GREPREGEX" -e "Numbers $GREPREGEX" \
-e "Deuteronomy $GREPREGEX" -e "Joshua $GREPREGEX" \
-e "Judges $GREPREGEX" -e "Ruth $GREPREGEX" \
-e "Ezra $GREPREGEX" -e "Nehemiah $GREPREGEX" \
-e "Esther $GREPREGEX" -e "Job $GREPREGEX" \
-e "Psalms* $GREPREGEX" -e "Proverbs $GREPREGEX" \
-e "Ecclesiastes $GREPREGEX" -e "Song of Solomon $GREPREGEX" \
-e "Isaiah $GREPREGEX" -e "Jeremiah $GREPREGEX" \
-e "Lamentations $GREPREGEX" -e "Ezekiel $GREPREGEX" \
-e "Daniel $GREPREGEX" -e "Hosea $GREPREGEX" \
-e "Joel $GREPREGEX" -e "Amos $GREPREGEX" \
-e "Obadiah $GREPREGEX" -e "Jonah $GREPREGEX" \
-e "Micah $GREPREGEX" -e "Nahum $GREPREGEX" \
-e "Habakkuk $GREPREGEX" -e "Zephaniah $GREPREGEX" \
-e "Haggai $GREPREGEX" -e "Zechariah $GREPREGEX" \
-e "Malachi $GREPREGEX" -e "Matthew $GREPREGEX" \
-e "Mark $GREPREGEX" -e "Luke $GREPREGEX" \
-e "John $GREPREGEX" -e "Acts $GREPREGEX" \
-e "Romans $GREPREGEX" -e "Galatians $GREPREGEX" \
-e "Ephesians $GREPREGEX" -e "Philippians $GREPREGEX" \
-e "Colossians $GREPREGEX" -e "Titus $GREPREGEX" \
-e "Philemon $GREPREGEX" -e "Hebrews $GREPREGEX" \
-e "James $GREPREGEX" -e "Jude $GREPREGEX" -e "Revelation $GREPREGEX" \
${MARKDOWNFILES}`; do
gsed -i \
-e "s/\([123] [JPTCSKjptcsk][a-z]*\) $SEDVSREGEX" \
-e "s/[Ff]irst \([JPTCSKjptcsk][a-z]*\) \([0-9]*\)\(:[0-9]*[-0-9]*\)/\[\[1 \1 \2#\2\3\]\]/g" \
-e "s/[Ss]econd \([JPTCSKjptcsk][a-z]*\) \([0-9]*\)\(:[0-9]*[-0-9]*\)/\[\[2 \1 \2#\2\3\]\]/g" \
-e "s/[Tt]hird \([JPTCSKjptcsk][a-z]*\) \([0-9]*\)\(:[0-9]*[-0-9]*\)/\[\[3 \1 \2#\2\3\]\]/g" \
-e "s/\([123]\)[snr][td] \([JPTCSKjptcsk][a-z]*\) \([0-9]*\)\(:[0-9]*[-0-9]*\)/\[\[\1 \2 \3#\3\4\]\]/g" \
-e "s/\(Genesis\) $SEDVSREGEX" -e "s/\(Exodus\) $SEDVSREGEX" \
-e "s/\(Leviticus\) $SEDVSREGEX" -e "s/\(Numbers\) $SEDVSREGEX" \
-e "s/\(Deuteronomy\) $SEDVSREGEX" -e "s/\(Joshua\) $SEDVSREGEX" \
-e "s/\(Judges\) $SEDVSREGEX" -e "s/\(Ruth\) $SEDVSREGEX" \
-e "s/\(Ezra\) $SEDVSREGEX" -e "s/\(Nehemiah\) $SEDVSREGEX" \
-e "s/\(Esther\) $SEDVSREGEX" -e "s/\(Job\) $SEDVSREGEX" \
-e "s/\(Psalms*\) $SEDVSREGEX" -e "s/\(Proverbs\) $SEDVSREGEX" \
-e "s/\(Ecclesiastes\) $SEDVSREGEX" -e "s/\(Song of Solomon\) $SEDVSREGEX" \
-e "s/\(Isaiah\) $SEDVSREGEX" -e "s/\(Jeremiah\) $SEDVSREGEX" \
-e "s/\(Lamentations\) $SEDVSREGEX" -e "s/\(Ezekiel\) $SEDVSREGEX" \
-e "s/\(Daniel\) $SEDVSREGEX" -e "s/\(Hosea\) $SEDVSREGEX" \
-e "s/\(Joel\) $SEDVSREGEX" -e "s/\(Amos\) $SEDVSREGEX" \
-e "s/\(Obadiah\) $SEDVSREGEX" -e "s/\(Jonah\) $SEDVSREGEX" \
-e "s/\(Micah\) $SEDVSREGEX" -e "s/\(Nahum\) $SEDVSREGEX" \
-e "s/\(Habakkuk\) $SEDVSREGEX" -e "s/\(Zephaniah\) $SEDVSREGEX" \
-e "s/\(Haggai\) $SEDVSREGEX" -e "s/\(Zechariah\) $SEDVSREGEX" \
-e "s/\(Malachi\) $SEDVSREGEX" -e "s/\(Matthew\) $SEDVSREGEX" \
-e "s/\(Mark\) $SEDVSREGEX" -e "s/\(Luke\) $SEDVSREGEX" \
-e "s/\(John\) $SEDVSREGEX" -e "s/\(Acts\) $SEDVSREGEX" \
-e "s/\(Romans\) $SEDVSREGEX" -e "s/\(Galatians\) $SEDVSREGEX" \
-e "s/\(Ephesians\) $SEDVSREGEX" -e "s/\(Philippians\) $SEDVSREGEX" \
-e "s/\(Colossians\) $SEDVSREGEX" -e "s/\(Titus\) $SEDVSREGEX" \
-e "s/\(Philemon\) $SEDVSREGEX" -e "s/\(Hebrews\) $SEDVSREGEX" \
-e "s/\(James\) $SEDVSREGEX" -e "s/\(Jude\) $SEDVSREGEX" \
-e "s/\(Revelation\) $SEDVSREGEX" \
-e "s/\([123] [JPTCSKjptcsk][a-z]*\) $SEDCPREGEX" \
-e "s/[Ff]irst \([JPTCSKjptcsk][a-z]*\) \([0-9]*\)\([ ,;.]\)/\[\[1 \1 \2\]\]/g" \
-e "s/[Ss]econd \([JPTCSKjptcsk][a-z]*\) \([0-9]*\)\([ ,;.]\)/\[\[2 \1 \2\]\]/g" \
-e "s/[Tt]hird \([JPTCSKjptcsk][a-z]*\) \([0-9]*\)\([ ,;.]\)/\[\[3 \1 \2\]\]/g" \
-e "s/\([123]\)[snr][td] \([JPTCSKjptcsk][a-z]*\) \([0-9]*\)\([ ,;.]\)/\[\[\1 \2 \3\]\]/g" \
-e "s/\(Genesis\) $SEDCPREGEX" -e "s/\(Exodus\) $SEDCPREGEX" \
-e "s/\(Leviticus\) $SEDCPREGEX" -e "s/\(Numbers\) $SEDCPREGEX" \
-e "s/\(Deuteronomy\) $SEDCPREGEX" -e "s/\(Joshua\) $SEDCPREGEX" \
-e "s/\(Judges\) $SEDCPREGEX" -e "s/\(Ruth\) $SEDCPREGEX" \
-e "s/\(Ezra\) $SEDCPREGEX" -e "s/\(Nehemiah\) $SEDCPREGEX" \
-e "s/\(Esther\) $SEDCPREGEX" -e "s/\(Job\) $SEDCPREGEX" \
-e "s/\(Psalms*\) $SEDCPREGEX" -e "s/\(Proverbs\) $SEDCPREGEX" \
-e "s/\(Ecclesiastes\) $SEDCPREGEX" -e "s/\(Song of Solomon\) $SEDCPREGEX" \
-e "s/\(Isaiah\) $SEDCPREGEX" -e "s/\(Jeremiah\) $SEDCPREGEX" \
-e "s/\(Lamentations\) $SEDCPREGEX" -e "s/\(Ezekiel\) $SEDCPREGEX" \
-e "s/\(Daniel\) $SEDCPREGEX" -e "s/\(Hosea\) $SEDCPREGEX" \
-e "s/\(Joel\) $SEDCPREGEX" -e "s/\(Amos\) $SEDCPREGEX" \
-e "s/\(Obadiah\) $SEDCPREGEX" -e "s/\(Jonah\) $SEDCPREGEX" \
-e "s/\(Micah\) $SEDCPREGEX" -e "s/\(Nahum\) $SEDCPREGEX" \
-e "s/\(Habakkuk\) $SEDCPREGEX" -e "s/\(Zephaniah\) $SEDCPREGEX" \
-e "s/\(Haggai\) $SEDCPREGEX" -e "s/\(Zechariah\) $SEDCPREGEX" \
-e "s/\(Malachi\) $SEDCPREGEX" -e "s/\(Matthew\) $SEDCPREGEX" \
-e "s/\(Mark\) $SEDCPREGEX" -e "s/\(Luke\) $SEDCPREGEX" \
-e "s/\(John\) $SEDCPREGEX" -e "s/\(Acts\) $SEDCPREGEX" \
-e "s/\(Romans\) $SEDCPREGEX" -e "s/\(Galatians\) $SEDCPREGEX" \
-e "s/\(Ephesians\) $SEDCPREGEX" -e "s/\(Philippians\) $SEDCPREGEX" \
-e "s/\(Colossians\) $SEDCPREGEX" -e "s/\(Titus\) $SEDCPREGEX" \
-e "s/\(Philemon\) $SEDCPREGEX" -e "s/\(Hebrews\) $SEDCPREGEX" \
-e "s/\(James\) $SEDCPREGEX" -e "s/\(Jude\) $SEDCPREGEX" \
-e "s/\(Revelation\) $SEDCPREGEX" \
$x
done
exit 0
@jag3773
Copy link
Author

jag3773 commented Dec 24, 2020

I changed the latest version (revision 6) to only reference */*.md which should make it more usable for others.

@jag3773
Copy link
Author

jag3773 commented Jan 16, 2021

I just added support for references like "First John 3:2" or "2nd Samuel 3" to support my speech to text engine which isn't consistent.

@jag3773
Copy link
Author

jag3773 commented Feb 17, 2021

Added support for entries like "first chronicles" which were not working previously. Also added a USER CONFIG section to make it easier to define the path to your vault and the search path for Markdown files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment