Content Storage, Database vs Markdown

How does Ghost store data?

I’ve been looking around the docs but can’t find anything on this. Are posts and pages kept in a database or as markdown files? If markdown files aren’t the default, is there anyway to change it so this data is kept in markdown files?

All data is stored in a MySQL (default and recommended in production) or SQLite database. The main content for posts/pages is stored in a format called mobiledoc as a row on the posts table, there is no option to change any of that.

Well, that’s discouraging. That would mean there’s no way to keep your site in git, right?

Ghost doesn’t natively support version control. However, since mobiledoc is a text-based format, if you really want it, you can create an internal integration that will commit changes to git. The workflow would be something like:

post.created -> webhook -> update posts/{id}.mobiledoc -> commit (update post {post.title} ({post.author})

I’m not sure where the mobiledoc->markdown transformer is if there is one.

Do you know of any guides for this? The raw documentation in that link is above my understanding.

I don’t, as I’ve never done it or seen it done in the wild. You would essentially be writing a little app which stores post data in another location. Here’s an example of a webhook controller in node.js:

const path = require('path');
const execa = require('execa');
const fs = require('fs').promises;

module.exports = function webhook(req, res) {
	// You can use a webhook service to see the exact schema of a webhook body
	const {post: postObject} = req.body;

	if (!postObject || !postObject.current) {
		return;
	}

	const {current: post} = postObject;

	const location = path.resolve(__dirname, `./repo/posts/${current.id}.post`);
	// I'm not sure if the mobiledoc field is included, and if it's included,
	// I'm not sure what the property would look like. So here's the entire post.
	await fs.writeFile(location, JSON.stringify(post));

	try {
		await execa(`git commit -m "Update post ${post.slug}"`);
		await execa(`git push`);
	} catch (error) {
		console.error(error);
		return res.status(500).json({
			error: 'Failed committing'
		});
	}

	res.end('ok');
};

I’m interested to know why you want to store your content in git? Is it for creating backups, surely your hosting provider can do that?

The main reason is portability. If I want to move to a different system, having the content in markdown files makes that easy. Another reason is that backing-up and restoring a database is far more complicated and time consuming than a git revert.

I figured being a fellow Jekyll lover, you’d prefer having your content in git.

As much as I like the portability of Markdown, it’s fallen behind while modern HTML has stormed ahead. Jekyll is a great tool, but lacks the dedicated writing experience and more modern HTML elements that Ghost can provide. Being able to log into something and begin writing articles quickly and easily is a very underrated privilege :slight_smile:

If you’re still wanting content portability with git then maybe you should be looking at Netlify CMS or Forestry. They do require more configuration, but they do let you edit Markdown from a git repository.

Composing articles with image galleries and whatnot are far easier in tools like Ghost, which is why I’ve been looking for an alternative for my clients. But I didn’t figure anyone was composing the text inside the system. I’ve been blogging for ten years, Blogger, WordPress, Jekyll more recently, and have never once wrote the articles inside those systems. I use ghostwriter and just assumed everyone used a dedicated writing app for composing. Never accrued to me to ask anyone else how they did it.

I’ve tried Forestry. I like their system, but in order to avoid their crazy expensive prices I’d have to setup an account for every client. I can run unlimited sites on CloudCannon for $25/month and have a single login. I like Forestry’s tools a little better than CloudCannon’s, but for a single login, $750/month + $50/month per user is too expensive for me …and that doesn’t even include hosting.

It’s been about a year since I last tried Netlify. At the time they didn’t have anything like Forestry’s Blocks or CloudCannon’s Array Structures. Checking their docs just now it looks like they have Custom Widgets. Might be worth looking into again.

Most people I’ve spoken to who use Markdown use it directly in their code editor. ghostwriter is pretty similar to a code editor but with a UI catered to writing Markdown.

Those were the options that came to mind. Alternatively you could produce Markdown backups from a Ghost site. For example you could use the Ghost Content API to extract Markdown files from posts and pages whenever you make a site change.

However that does seem like a lot of effort for little gain, and you could port the content out at the point at which you want to switch :slight_smile: