Hi,
I wrote a simple python script for creating a table of content based on the headers. Finished product gets copied to the clipboard.
The plan is to create a simple hotkey for activating it or something of that sort…
This is actually my first completed project since i started learning to code, so im pretty stoked it worked!
If you have any feedback or improvements it would be much appreciated!
After pasting the result back into Obsidan it looks like this:
code:
import re
import pyperclip
fileName = 'Lists in Python'
vaultFolder = '/Users/macbook-air/SynologyDrive/The Cortex/000 Notater/'
filePath = vaultFolder + fileName + '.md'
# Leser filen
with open(filePath) as file_object:
content = file_object.read()
# Removes all the text between the ``` character - Codeblocks in particular
regex = r"```.*?```"
txt = str(content)
txt = re.sub(regex, '', txt, 0, re.DOTALL)
removeComma = txt.replace(",",";-;")
cleanTitles = removeComma.replace("\n",",")
cleanTitlesList = cleanTitles.split(",")
finishedList = []
for i in cleanTitlesList:
noComma= i.replace(";-;", ",")
finishedList.append(noComma)
tab = " "
print("- [[" + fileName + "]]")
fullTxt = "- [[" + fileName + "]]\n"
for i in finishedList:
if i[:2] == "# ":
fullTxt += (tab * 0) + "- [[" + fileName + '#' + i[2:] + '|' + i[2:] + "]]\n"
if i[:3] == "## ":
fullTxt += (tab * 1) + "- [[" + fileName + '#' + i[3:] + '|' + i[3:] + "]]\n"
if i[:4] == "### ":
fullTxt += (tab * 2) + "- [[" + fileName + '#' + i[4:] + '|' + i[4:] + "]]\n"
if i[:5] == "#### ":
fullTxt += (tab * 3) + "- [[" + fileName + '#' + i[5:] + '|' + i[5:] + "]]\n"
if i[:6] == "##### ":
fullTxt += (tab * 4) + "- [[" + fileName + '#' + i[6:] + '|' + i[6:] + "]]\n"
if i[:7] == "###### ":
fullTxt += (tab * 5) + "- [[" + fileName + '#' + i[7:] + '|' + i[7:] + "]]\n"
pyperclip.copy(fullTxt)```