Automating the process for moving notes from my Obsidian vault to my Astro site
Lately, I’ve been writing the drafts of the notes on this site in my Obsidian vault. When I want to publish them, I would copy the contents over to the src directory of my Astro site code, use the VSCode Grammarly extension to proof read, add some more frontmatter and push to GitHub.
I was doing this manually, and it was making publishing a note a whole thing. In the interest of publishing more frequently, I thought I would try and automate parts of the process of getting an obsidian note in to the github repo.
I wrote a Node.js script that reads all the notes in my vault, checking for the ones with frontmatter that include a slug
and note
status value of publish
. It then checks the contents of the note for internal links (aka wikilinks) that link to other published notes, and replaces them with markdown links. If the note isn’t published, link syntax is removed. This way I can link freely between notes in my own vault without worrying about whether a note is published or not.
After processing the notes, the script copies them to the src
location in this site’s repo. Then I can just use the git diff to make sure everything looks like it should before pushing.
At first I tried to get fancy and convert the notes in to a markdown syntax tree with mdast-util-from-markdown and do the processing with that. But I ended up just using a series of indexOf
checks, split
calls and a little regex to achieve what I need.
The first version of the script just copied all the notes and images over when you ran it. Then I updated it to use chokidar to watch for changes in the vault, and sync any notes that were updated.
I’ve shared the script in this GitHub Repository.
Comments
Versun
December 25, 2022 at 4:18 AM
Thanks for the script, it works for me!
THANKS!!
Rach Smith OP
December 29, 2022 at 6:43 AM
I'm glad you found it helpful!
Mike
February 23, 2024 at 8:54 PM
Hey! so glad this post is still up. I've used and built out this script to help run my blog... great stuff and thank you!
Leave a Comment
💯 Thanks for submitting your comment! It will appear here after it has been approved.