# Migration from Hugo to Ghost

**URL:** <https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674>\
**Category:** Integrations & API\
**Created:** [July 31, 2022, 2:36am UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674 "2022-07-31T02:36:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![diegoeis](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diegoeis/32/18763_2.png) [@diegoeis](https://forum.ghost.org/u/diegoeis)\
**Post date:** [July 31, 2022, 2:36am UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/1 "2022-07-31T02:36:15Z")

</div>

Hello people!

Is there any way to migrate from hugo to ghost?  
I just found plugins for jekyll.

---

<div class="post-metadata">

**Author:** ![Aliya\_Lee](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/aliya_lee/32/18725_2.png) [@Aliya\_Lee](https://forum.ghost.org/u/Aliya_Lee)\
**Post date:** [July 31, 2022, 1:18pm UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/2 "2022-07-31T13:18:14Z")

</div>

Check this: [Migrating from Hugo to Ghost](https://www.conradk.com/2019/12/30/migrating-from-hugo-to-ghost.html)

---

<div class="post-metadata">

**Author:** ![diegoeis](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diegoeis/32/18763_2.png) [@diegoeis](https://forum.ghost.org/u/diegoeis)\
**Post date:** [July 31, 2022, 2:35pm UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/3 "2022-07-31T14:35:03Z")

</div>

I’m trying to use this code, but when I run, I got this error:

```auto
ode migra.js 
/Users/diego.eis/Sites/migra.js:57
    const image = imageMatches[1]
                              ^

TypeError: Cannot read properties of null (reading '1')
    at createPostDataFromFileContent (/Users/diego.eis/Sites/migra.js:57:31)
    at /Users/diego.eis/Sites/migra.js:75:41
    at Array.map (<anonymous>)
    at Object.<anonymous> (/Users/diego.eis/Sites/migra.js:75:6)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

```

I changed the pattern of YAML instead use +++, I use in my markdowns —, so, I changed this in the Regexp of JS. But, this is the only change.

I got this error even when I don’t change the JS.  
 ![Screen Shot 2022-07-31 at 11.32.44](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/d/2/d2b55a1879f7b7e41f8f9476506ecd5dcba508a3.jpeg)

Thanks for the answer.

---

<div class="post-metadata">

**Author:** ![Aliya\_Lee](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/aliya_lee/32/18725_2.png) [@Aliya\_Lee](https://forum.ghost.org/u/Aliya_Lee)\
**Post date:** [July 31, 2022, 4:34pm UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/4 "2022-07-31T16:34:08Z")

</div>

It’s also hard to understand for me. Maybe someone will help with it.

---

<div class="post-metadata">

**Author:** ![diegoeis](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diegoeis/32/18763_2.png) [@diegoeis](https://forum.ghost.org/u/diegoeis)\
**Post date:** [August 3, 2022, 1:37am UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/5 "2022-08-03T01:37:53Z")

</div>

Update:  
I found this script  
[saltfactory/node-jekyll-to-ghost: jekyll to ghost exporter (github.com)](https://github.com/saltfactory/node-jekyll-to-ghost)

What I did: install jekyll, copy all posts to \_posts folder of jekyll and run the script.  
Apparently this works fine, but, when I tried to import the json in ghost, I got an error:

```auto
Please install Ghost 1.0, import the file and then update your blog to the latest Ghost version. Visit https://ghost.org/docs/update/ or ask for help in our https://forum.ghost.org.: Detected unsupported file structure.

```

I’m trying to install this oldest version of Ghost, but I need to downgrade all my stack.

Any update, I change this thread.

---

<div class="post-metadata">

**Author:** ![diegoeis](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diegoeis/32/18763_2.png) [@diegoeis](https://forum.ghost.org/u/diegoeis)\
**Post date:** [August 3, 2022, 2:38am UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/6 "2022-08-03T02:38:54Z")

</div>

Tried to get the old JSON and change to modern pattern, inserting DB object…  
After that, I tried import the json again, and a got the error:

```auto
Value in [posts.title] cannot be blank.

```

Dont know what it is, but I think can be related to mobiledoc.

Tried follow the instructions here ( [Importing content from other platforms to Ghost](https://ghost.org/docs/migration/content/)), but I got this error.

```bash
node:internal/modules/cjs/loader:959
  throw err;
  ^

Error: Cannot find module '@tryghost/mg-mediascraper'
Require stack:
- /opt/homebrew/lib/node_modules/@tryghost/migrate/sources/ghost.js
- /opt/homebrew/lib/node_modules/@tryghost/migrate/commands/ghost.js
- /opt/homebrew/lib/node_modules/@tryghost/migrate/node_modules/sywac/api.js
- /opt/homebrew/lib/node_modules/@tryghost/migrate/node_modules/@tryghost/pretty-cli/lib/pretty-cli.js
- /opt/homebrew/lib/node_modules/@tryghost/migrate/node_modules/@tryghost/pretty-cli/index.js
- /opt/homebrew/lib/node_modules/@tryghost/migrate/bin/cli.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1022:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/opt/homebrew/lib/node_modules/@tryghost/migrate/sources/ghost.js:4:24)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1022:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/opt/homebrew/lib/node_modules/@tryghost/migrate/commands/ghost.js:1:15)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Module._load (node:internal/modules/cjs/loader:839:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/opt/homebrew/lib/node_modules/@tryghost/migrate/sources/ghost.js',
    '/opt/homebrew/lib/node_modules/@tryghost/migrate/commands/ghost.js',
    '/opt/homebrew/lib/node_modules/@tryghost/migrate/node_modules/sywac/api.js',
    '/opt/homebrew/lib/node_modules/@tryghost/migrate/node_modules/@tryghost/pretty-cli/lib/pretty-cli.js',
    '/opt/homebrew/lib/node_modules/@tryghost/migrate/node_modules/@tryghost/pretty-cli/index.js',
    '/opt/homebrew/lib/node_modules/@tryghost/migrate/bin/cli.js'
  ]
}

Node.js v18.7.0

```

---

<div class="post-metadata">

**Author:** ![diegoeis](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diegoeis/32/18763_2.png) [@diegoeis](https://forum.ghost.org/u/diegoeis)\
**Post date:** [August 3, 2022, 2:44am UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/7 "2022-08-03T02:44:10Z")

</div>

Here are some of the scripts I tried, but I got many errors… I think all area outdated, since the latest update was 4,5,6 years ago.

- [kshaffer/jekylltoghost: Convert Jekyll posts (MarkDown files with YAML headers) to a Ghost-compatible JSON file. (github.com)](https://github.com/kshaffer/jekylltoghost)
- [saltfactory/node-jekyll-to-ghost: jekyll to ghost exporter (github.com)](https://github.com/saltfactory/node-jekyll-to-ghost)
- [mattvh/Jekyll-to-Ghost: Takes your Markdown posts from Jekyll and packages them up into a JSON file that Ghost (http://ghost.org) can import (github.com)](https://github.com/mattvh/Jekyll-to-Ghost)
- [weblancaster/nodejs-jekyll-to-ghost: Takes your Markdown posts from Jekyll and packages them up into a JSON file that Ghost (http://ghost.org) can import. (github.com)](https://github.com/weblancaster/nodejs-jekyll-to-ghost)

---

<div class="post-metadata">

**Author:** ![diegoeis](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diegoeis/32/18763_2.png) [@diegoeis](https://forum.ghost.org/u/diegoeis)\
**Post date:** [August 4, 2022, 11:42pm UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/8 "2022-08-04T23:42:20Z")

</div>

Well, I don’t know how to write all what I did to this work, but I finally made this.

I modified this script - [https://github.com/saltfactory/node-jekyll-to-ghost](https://github.com/saltfactory/node-jekyll-to-ghost) to help.

Because the script is very old, I needed to solve many small problems of patters and sanitization to get the correct final json. Every time I tried to import the json to ghost, I discovery some errors pattern. I solved that one by one.

This post ([Ghost and Markdown: Getting everything consistent on the backend](https://polymath.net/2020/12/ghost-markdown-backend/)) describe in details some of my problems. The main problem was solve the content that was not being imported to ghost. This was resolved by individually handling each of the errors when importing the json into the platform. A lot of work and a lot of small different problems.

In this post, I read the problem of `\n` that we need to substitute to `\\n`.

Some good links to read:

- [Migrated to Ghost](https://www.jwon.me/migrated-to-ghost/)
- [Troy Hunt: It's a new blog!](https://www.troyhunt.com/its-a-new-blog/)

If you are facing the problem of content not showing in the ghost, the a-ha moment for me was treat the patter problem in that line in the script:

```javascript
mobiledoc: "{\"version\":\"0.3.1\",\"atoms\":[],\"cards\":[[\"markdown\",{\"markdown\":\"" + matter.content.replaceAll("\"","_").replaceAll("\\---","---") + "\"}]],\"markups\":[],\"sections\":[[10,0],[1,\"p\",[]]]}",

```

Here I tried to replace and remove the parts that was breaking the import `matter.content.replaceAll("\"","_").replaceAll("\\---","---")`

My final script follow. I hope this can help someone in the feature.

```javascript
'use strict';

const fs = require('fs');
const path = require('path');
const grayMatter = require('gray-matter');
const uuid = require('node-uuid');
const markdown = require('markdown').markdown;
const extend = require('util')._extend;

let tagList = [];
let matterList = [];

let ghostData = {
  meta: {
    exported_on: Date.now(),
    version: "5.7.0"
  },
  data: {
    posts: [],
    tags: [],
    posts_tags: []
  }
};

function getMatter(filePath) {
  let fileContent = String(fs.readFileSync(filePath));
  return grayMatter(fileContent);
}

function createSlug(filename) {
  return filename.substring(0, filename.lastIndexOf('.'));
  // return Path.parse(filename).name  
  // const filename = Path.parse('/home/user/avatar.png').name  
}

function createDate(filename) {
  return Date.parse(filename.substring(0, 10));
}

function addUniqueTags(tags, index) {
  let postIndex = index+1;

  let addUniqueTag = (tag) => {
    if (tagList.indexOf(tag) === -1){
      tagList.push(tag);
    }

    createPostsTags(postIndex, tagList.indexOf(tag)+1);

  };

  if (typeof tags === 'string') {
    addUniqueTag(tags);
  } else if(typeof tags === 'object') {
    tags.map(tag => addUniqueTag(tag));
  }

}

function createGhostPost(index, matter, filename) {
  let importUserId = 1;
  let post = {
    featured: 0,
    page: 0,
    status: 'published',
    language: 'pt_BR',
    meta_description: matter.data.excerpt,
    author_id: matter.data.author,
    created_by: importUserId,
    updated_by: importUserId,
    published_by: importUserId,
  };

  post = extend(post, {
    id: index + 1,
    uuid: uuid.v4(),
    title: matter.data.title ? matter.data.title : createSlug(filename).replaceAll("-"," ").toLowerCase(),
    slug: createSlug(filename),
    // markdown: matter.content,
    // html: markdown.toHTML(matter.content),
    mobiledoc: "{\"version\":\"0.3.1\",\"atoms\":[],\"cards\":[[\"markdown\",{\"markdown\":\"" + matter.content.replaceAll("\"","_").replaceAll("\\---","---") + "\"}]],\"markups\":[],\"sections\":[[10,0],[1,\"p\",[]]]}",
    image: matter.data.images ? matter.data.images.title : null,
    meta_title: matter.data.title,
    created_at: createDate(filename),
    updated_at: createDate(filename),
    published_at: matter.data.date
    // publised_at: createDate(filename)
  });

  return post;
}

function createGhostTag(index, tag) {
  return {
    id: index + 1,
    name: tag,
    slug: tag,
    description: ""
  };
}

function createPostsTags(postId, tagId){
  ghostData.data.posts_tags.push({post_id:postId, tag_id:tagId});
}

function convert(src, dest){
  fs.readdir(src, (err, filenames) => {
    filenames.forEach((filename, index) => {
      let filePath = path.join(src, filename);
      let matter = getMatter(filePath);
      matterList.push(matter);

      let ghostPost = createGhostPost(index, matter, filename);
      ghostData.data.posts.push(ghostPost);
      addUniqueTags(matter.data.tags, index);
    });

    tagList.forEach((tag, i) => {
      let ghostTag = createGhostTag(i, tag);
      ghostData.data.tags.push(ghostTag);
    });

    createPostsTags();
    fs.writeFile(dest, JSON.stringify(ghostData), err => err ? console.error(err) : process.exit(0));
  });
}

if ( process.argv.length < 4) {
  console.error('You need to specify a path to Jekyll posts.');
} else {
  let src = process.argv[2];
  let dest = process.argv[3];

  convert(src, dest);
}

```

---

<div class="post-metadata">

**Author:** ![ndbroadbent2](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ndbroadbent2/32/29573_2.png) [@ndbroadbent2](https://forum.ghost.org/u/ndbroadbent2)\
**Post date:** [April 19, 2024, 5:08am UTC](https://forum.ghost.org/t/migration-from-hugo-to-ghost/31674/9 "2024-04-19T05:08:58Z")

</div>

Awesome, thanks so much @diegoeis!

I was still struggling with some missing content, so I tweaked your JS script a little bit to handle newlines and quotes. This seemed to work for me (with a few other issues that I needed to fix manually.)

```auto
"use strict";

const fs = require("fs");
const path = require("path");
const grayMatter = require("gray-matter");
const uuid = require("node-uuid");
const markdown = require("markdown").markdown;
const extend = require("util")._extend;

let tagList = [];
let matterList = [];

let ghostData = {
  meta: {
    exported_on: Date.now(),
    version: "5.7.0",
  },
  data: {
    posts: [],
    tags: [],
    posts_tags: [],
  },
};

function getMatter(filePath) {
  let fileContent = String(fs.readFileSync(filePath));
  return grayMatter(fileContent);
}

function createSlug(filename) {
  return filename.substring(0, filename.lastIndexOf("."));
  // return Path.parse(filename).name
  // const filename = Path.parse('/home/user/avatar.png').name
}

function createDate(filename) {
  return Date.parse(filename.substring(0, 10));
}

function addUniqueTags(tags, index) {
  let postIndex = index + 1;

  let addUniqueTag = (tag) => {
    if (tagList.indexOf(tag) === -1) {
      tagList.push(tag);
    }

    createPostsTags(postIndex, tagList.indexOf(tag) + 1);
  };

  if (typeof tags === "string") {
    addUniqueTag(tags);
  } else if (typeof tags === "object") {
    tags.map((tag) => addUniqueTag(tag));
  }
}

function createGhostPost(index, matter, filename) {
  let importUserId = 1;
  let post = {
    featured: 0,
    page: 0,
    status: "published",
    language: "pt_BR",
    meta_description: matter.data.excerpt,
    author_id: matter.data.author,
    created_by: importUserId,
    updated_by: importUserId,
    published_by: importUserId,
  };

  post = extend(post, {
    id: index + 1,
    uuid: uuid.v4(),
    title: matter.data.title
      ? matter.data.title
      : createSlug(filename).replaceAll("-", " ").toLowerCase(),
    slug: createSlug(filename),
    // markdown: matter.content,
    // html: markdown.toHTML(matter.content),
    mobiledoc:
      '{"version":"0.3.1","atoms":[],"cards":[["markdown",{"markdown":"' +
      matter.content
        .replaceAll('"', '\\"')
        .replaceAll("\n", "\\n")
        .replaceAll("\\---", "---") +
      '"}]],"markups":[],"sections":[[10,0],[1,"p",[]]]}',
    image: matter.data.images ? matter.data.images.title : null,
    meta_title: matter.data.title,
    created_at: createDate(filename),
    updated_at: createDate(filename),
    published_at: matter.data.date,
    // publised_at: createDate(filename)
  });

  return post;
}

function createGhostTag(index, tag) {
  return {
    id: index + 1,
    name: tag,
    slug: tag,
    description: "",
  };
}

function createPostsTags(postId, tagId) {
  ghostData.data.posts_tags.push({ post_id: postId, tag_id: tagId });
}

function convert(src, dest) {
  fs.readdir(src, (err, filenames) => {
    // const filename = filenames[0];
    // const index = 0;
    filenames.forEach((filename, index) => {
      let filePath = path.join(src, filename);
      let matter = getMatter(filePath);
      matterList.push(matter);

      let ghostPost = createGhostPost(index, matter, filename);
      ghostData.data.posts.push(ghostPost);
      addUniqueTags(matter.data.tags, index);
    });

    tagList.forEach((tag, i) => {
      let ghostTag = createGhostTag(i, tag);
      ghostData.data.tags.push(ghostTag);
    });

    createPostsTags();
    fs.writeFile(dest, JSON.stringify(ghostData), (err) =>
      err ? console.error(err) : process.exit(0)
    );
  });
}

if (process.argv.length < 4) {
  console.error("You need to specify a path to Jekyll posts.");
} else {
  let src = process.argv[2];
  let dest = process.argv[3];

  convert(src, dest);
}

```
