Skip to main content

Command Palette

Search for a command to run...

hashnode-sync: Sync your local markdown files with hashnode

Sync hashnode with your local files easily

Updated
4 min read
hashnode-sync: Sync your local markdown files with hashnode

Intro

Hashnode Sync is the easiest way to get started with your hashnode blog. You only need these things:
1. A Hashnode Account and Personal Access Token from https://hashnode.com/settings/developer
2. A folder on your PC with your written blogs as markdown files.

The markdown files will have some metadata with them that corresponds with the post that you want to publish. We will put that information on the frontmatter of the markdown file in YAML format.

Example Format:

<!--- There is YAML between the 2 pairs of dashes -->
<!--- This is used to specify metadata similar -->
<!--- the ones in your post's settings tab -->
---
title: Hey There!
subtitle: How's it going.
status: PUBLISH
---

This is a blog too.

The status key is important here: setting it to `PUBLISH` will publish the blog. Any subsequent changes to it will also be updated.

However, if you set it to `DELETE` the blog will be deleted.

I could've set up the default value to be PUBLISH however I figured we'd want a status that could schedule our blog posts.

Setup

Run the command:

npm i -g hashnode-sync

To get set up with hashnode-sync. Go to any folder of your choosing with your markdown files and run the command:

hashnode-sync

You will be prompted with a few questions to set up your folder, Personal Access Token and default blog/publication.

After that, you're set up! You can now write markdown files with the correct frontmatter and easily sync them to Hashnode. Here's an example file:

Make sure to include the `status` key on the frontmatter in your markdown files. This is a signal to us that this blog needs to be published.

Limitations

There might be a few edge cases I haven't covered with regards to the validation of the inputs to the GraphQL API and some of the metadata are still not supported.

How this works

We're keeping track of every file in `hashnode-syncrc.js`. Over there, we are storing the hash of the content of the markdowns and comparing them again when we run the script.

If the hashes change, we can say that the file has changed and we can then make API calls to Hashnode.

Let's go through the process again. In our folder, we have a structure like this:

/blogs
    - this-is-md.md
    - my-markdown-blog.md
    - not-markdown.pdf
    - hashnode-syncrc.json

When we run our command hashnode-sync will take all the markdown files only and parse their content for metadata.

Any file that we haven't kept track of in our hashnodesync-rc.jsonfile will be created as a new post in the Hashnode blog that we set. We have to make sure to set the status key to PUBLISH to make sure the blog is posted. We're using the `publishPostMutation` to publish posts.

A sha256 hash will be generated from this content that we will use to compare with the file if we edit it at a later time.

If we detect changes in our file, we will look at the status key. If it's set as DELETE we will make a `deletePostMutation` request to the Hashnode GraphQL API.

If not we will make a request to `updatePostMutation` to update our blog post using the id we stored in our json file.

Easy no?

Struggles

I haven't worked with GraphQL extensively and this was a chance to flex those muscles. And boy were they flexed. Learning GraphQL and following the API docs was really difficult but I'm glad I got through it and could finish this 💪.

Interesting Finds

If you look into my GitHub repo, you'll find a file called `sync-json.js`. Over here we're making use of a Proxy object to sync whatever changes we make to the object we get from parsing the json config file.

This was also the first time I published a script on npm and used it as a standalone command which was interesting. Maybe I'll post about it another time.

Video Demo

Here's a video demo of `hashnode-sync` in action. I forgot to mention the setup part in this but it's mentioned above.
https://www.loom.com/share/be7ba5427f4f439fbfeba910003c384b?sid=61ff9b1d-b01b-4507-9317-ac17583bde5d

Links

GitHub Repo: https://github.com/Aryan3212/hashnode-sync