What this is about
You are reading a page. A few steps ago it was a plain text file. This article follows the path of a post on amarbaro.com from that file to the page on your screen: the file, the check of its fields, the database row and the page. Knowing the path helps you read our blog, and it helps you build one of your own.
Why it matters
Many people think a web page is a file that sits on a server. For a blog with an admin, it usually is not. It is data, turned into a page when you ask. That has a nice side effect: you can fix a typo with a save, not with a new deploy. It also means there are two places a post can live, and you have to know which one is the truth.
How it works
Step 1: A text file with a header
A post starts as a file with the ending .md, which stands for Markdown. Markdown is a simple way to write text with a few marks: a pound sign for a heading, a dash for a list item. At the top of the file is a block between two lines of dashes. It holds the facts about the post, one per line.
The ones our blog needs are: the title, the date, the author, a short description for search results, a summary for the card on the index, the cover image, a text for the image for readers who cannot see it, the category, and a one sentence claim. There are optional ones too, such as tags, a series name and the position in the series. A post marked as a draft is kept out of every listing.
Step 2: The check
When the posts are loaded, a program reads each file and looks for the required fields. If one is missing, the import fails loudly with the name of the file and the field. We did that on purpose. We prefer a stop to a half finished card on the index.
The category is checked too. It must be one from a fixed list. The list is fixed so a typo cannot create a category page with a single post in it. The list of categories shown on the blog comes from what posts actually say. A category exists for as long as at least one post claims it.
Step 3: A row in the database
If the checks pass, the program makes one row per post in the database, but only for posts that are not there yet. It never overwrites a row that exists. The reason is written in the code: the row may have been edited in the admin since it was made.
This is the key idea. The file is the origin and the row is the truth. After the first import, if we fix a typo, we save the row in the admin. We do not need to edit the file or deploy the site.
Step 4: The page
When you open a post, the app looks up its row, converts the Markdown into HTML, and puts the HTML into a page template. The template adds the header, the footer, the cover and the links to the next and previous posts in the same series. That page is what your browser receives.
If the post is part of a series, it carries a series name and a number, and the blog index puts the posts in that order, with links between them. A series is a reading order, not just a tag.
A picture of the path
text file (.md) -> check fields -> database row -> page
origin loud failure the truth HTML for you
What this does not cover
The path has side trips too. A post can be translated into other languages, as we explained in the article on languages. A copy can be sent to other places after it goes live, which is the subject of the last article in this series. And a cached copy can delay a change, as the article on caching explained.
Why we split it this way
A file is easy to write, review and keep in version control. You can read the history of a post line by line. A database row is easy to edit from a browser and does not need a deploy. Each does one job well. Using the file as the origin and the row as the truth gives us both: careful writing at the start, quick fixes afterwards. The price is that you must remember the order. If you edit the file after the first import, the site will not change, because the row already exists. The change belongs in the admin.
A second price is that two copies can disagree. We accept it, and we say which one wins.
Words you may not know
- Markdown: a simple way to write formatted text with a few marks.
- Frontmatter: the block of fields at the top of a post file.
- Import: reading files and turning them into rows.
- Row: one record in a database table.
- Template: a page layout with blanks that data fills in.
- Draft: a post that is hidden until it is ready.
- Series: a set of posts with a reading order.
Try it
- Open the blog at amarbaro.com/blog and pick any post. Find the series name and the links to the next and previous posts.
- Write a short Markdown file with a title, a few words and a list. Open it in a Markdown viewer.
- If you run a blog, decide which of your two copies is the truth, the file or the database, and write it down.
- See the sites we run at amarbaro.com/web.
Read next
- A Reboot Is Not a Health Check: a post from the same blog that is a good example of the format.

Commentaires
Pas encore de commentaires.
Se connecter pour commenter.