I Migrated My Blog From WordPress To Hashnode

I Migrated My Blog From WordPress To Hashnode

I just finished migrating my blog from Wordpress (self hosted on DigitalOcean) to Hashnode.

How did I migrate?

I don't have too many articles on Wordpress but I don't like doing it manually so the first thing I did was look to see if there was an automated solution.

I have tried the following solutions:

  1. Solution by Tina Holly: Use NodeJS + Hashnode's Graph API

  2. Import data from Hashnode's RSS Importer

  3. Combine Hashnode's Markdown Importer + Wordpress Export feature

Unfortunately, on the first attempt at the first solution, Tina's script did not work as expected, possibly due to Hashnode's GraphAPI upgrade leading to the script being out of date.

With the second solution, you will need to increase the number of items displayed on the Wordpress feed to the maximum so that the Hashnode can read them all and import them into their system. I tried to use this feature and import articles from Wordpress as drafts, because I wanted to review & correct any errors before publishing. But unfortunately, when editing the draft articles and publishing them, Hashnode reported an error (a very general error) + reviewing each of these articles took quite a bit of time, so I decided to try again by publishing the imported articles directly, instead of draft... But, but again, the system always reported errors from the first post so I decided to ignore this solution.

I decided to try the Markdown Importer function because it seemed feasible. I'm also thinking about writing a script to convert Wordpress articles to Markdown format. But luckily I don't have to do that because I found this script.

All you need to do is:

  1. Export Wordpress articles to XML format (this feature is available in Wordpress, you do not need to install additional plugins)

  2. Run the Nodejs script above to convert articles to Markdown format

  3. Compress all markdown files in step 2 and then use Hashnode's Markdown Importer

However, there are still a few issues to note:

  • The markdown files do not have slug (because I set it up to skip generating in folder format structure => the name of the markdown file is also the slug too.

  • Downloading images will make your zip file unable to be imported, so when converting to markdown, remember to select the option not to download images to your computer. Just leave the image link in the markdown file, Hashnode will automatically convert it to their static CDN.

  • Categories & tags cannot be imported (because markdown template does not support it)

I asked ChatGPT to write a simple script to update slug in markdown file:

const fs = require('fs');
const path = require('path');
const grayMatter = require('gray-matter');
const slugify = require('slugify');

const folderPath = 'path/to/your/md/files'; // Replace with your folder path

function processMarkdownFile(filePath) {
  const fileContent = fs.readFileSync(filePath, 'utf-8');

  // Extract front matter using gray-matter
  const { data, content } = grayMatter(fileContent);
  const { title, date, categories } = data;

  // Generate slug from the title
  const slug = slugify(title, { lower: true });

  // Add the slug field
  const updatedFrontMatter = `---
title: "${title}"
date: "${date}"
slug: "${slug}"
categories: 
  ${categories.map(category => `- "${category}"`).join('\n  ')}
---`;

  // Update the file content
  const updatedFileContent = `${updatedFrontMatter}\n\n${content.trim()}`;

  // Write the updated content back to the file
  fs.writeFileSync(filePath, updatedFileContent, 'utf-8');
}

function processMarkdownFilesInFolder(folderPath) {
  const files = fs.readdirSync(folderPath);

  files.forEach(file => {
    const filePath = path.join(folderPath, file);

    if (path.extname(filePath) === '.md') {
      processMarkdownFile(filePath);
    }
  });
}

processMarkdownFilesInFolder(folderPath);

Yey! Migrated successfully 😍

And I also set up a custom domain for this new blog to https://blog.donamkhanh.com

Now I just need to edit the donamkhanh.com page into a super simple HTML page and I'm done. I've completed it but haven't pushed it to the Github page yet. Maybe let's have a free day 😅

Hello Hashnode! 🥰