3 months ago
Share this article Reddit Twitter Facebook Google+

How to create a simple markdown based blog system using PHP Laravel

Recently I rebuilt my personal website with PHP laravel and Bootstrap 4. I need a simple blogging system to write some of my ideas. I examined some existing Laravel and PHP code packages. I also researched some complete blogging systems. Eventually I decided to write a simple blogging system from scratch. This article will write some of my ideas and experience in building a blogging system.

My requirements

Why Create from scratch

Create indexing and metadata database

Wait, doesn't it need to be completely file-based, how can there be a database? Yes, the blog content itself is file based, but for performance reasons, blog related indexes and metadata should be placed in the database. The database is only used for performance optimization. All data in the database is parsed from the blog file. That is, I can delete all data from the database and then recover the data from the markdown files. This allows the database to be used to optimize performance, rather than as a strong dependency of the blogging system. This is important, not only that the code design should be decoupled, but also that the system design should be decouple as well.

The database structure is simple, with only one table storing metadata for information such as update time, and a table that holds tag information. Other tables may be added in the future, but the principle is simple, all the data is parsed from the blog files.

Data in the database supports lazy creation. When a blog page is loaded, metadata is recreated if the system discovers that there are no related data in the database or the metadata expires.

When an article is created or deleted, the index needs to be rebuilt to allow the change reflects on the index page. The blog engine doesn't automatically detect the index change for performance reason. I just write a tool in PHP to rebuild the index. The tool is a Laravel artisan command and reuse most of the blogging code base.

Since the database data is almost read-only except the lazy creation, I first considered using SQLite. However, since there are other modules that need to use the database, MySQL was eventually chosen.

Markdown to HTML conversion

There are a lot of good enough open source markdown to HTML conversion Libraries, I finally chose Parsedown. But I need to extend it to add target= "_blank" and rel= "nofollow" to external links. I also do some extra regular expression replacement to support code syntax highlighting.

Parsing markdown Files

Important blog metadata are stored in a special marker block of markdown. Before converting the markdown into HTML, the blog engine parses and extracts the metadata block and store them into the database. Metadata may be used in formats such as YAML and parsed with third-party libraries, but I decided to parse the simple format myself because it's a lot faster than finding a third-party library.

Examples of some metadata

update_time = 2019-01-20 15:19 GMT+08:00
title = This is a sample title
summary = This is some summary,
another line of summary
tags = tag1, tag2

Comment System

My requirements for a comment system

After I looked at some PHP based article comment systems, I had thought to write a comment system from scratch myself. I hesitated for a long time, always feeling unworthy of reinventing a wheel, and most importantly, the comment system was not a must to have. Finally I decided to use the third party comment service, such as HTML comment box (in fact, there is Disqus, but I don't like it very much). Luckily, before I used the third-party service, I found a very good package, Commentics, which basically met my needs.

Summary

It makes a lot of sense to write a blog system based on Markdown from the beginning. In the case of not a lot of time spent, I have a blog system that fully meets my needs, and I will slowly add new features.

Add Comment

* Required information
1000000
Powered by Commentics

Comments (1)

Mario says...

Can you show how to parse markdown metadata using parsedown? I have tried to find a resource on that and red dosumentation but haven't found anything...

Wang Qi says...

It's pretty simple.

$parsedown = new Parsedown();

$html = $parsedown->text($mdText);

My code is just like that.