Skip to content

Instantly share code, notes, and snippets.

@davidakennedy
Last active October 20, 2023 02:08
Show Gist options
  • Save davidakennedy/f7697650c828b1e9a5a9a5f85a85b033 to your computer and use it in GitHub Desktop.
Save davidakennedy/f7697650c828b1e9a5a9a5f85a85b033 to your computer and use it in GitHub Desktop.
A PHP script to convert Markdown files with front matter to Kirby CMS text files.

Readme

This is a messy script, built to my specific needs, but it may help someone else out.

It will take a standard markdown file with front matter like this:

---
title: Super Blog Post
description: A description of this blog post.
featuredimg: true
img: ./path/to/file.jpg
imgalt: White keyboard.
date: 2009-09-16T22:26:14+00:00
permalink: /blog/super-blog-post/
tags:
  - Tag1
  - Tag2
---

And convert it to this:

Title: Super Blog Post

----

Description: A description of this blog post.

----

Cover: ./path/to/file.jpg

----

Date: 2009-09-16 22:26:14

----

Tags: Tag1, Tag2

----

Text: Blog posts. We love them.

Each file will be given a folder, according to the permalink field.

<?php
// Get files.
$directory = "/Users/dk/Desktop/articles/"; // Replace with the actual directory path
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$directory,
RecursiveDirectoryIterator::SKIP_DOTS
),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
$path = $item->getPathname();
// Read the files.
if (!is_readable($path)) {
die("File does not exist or is not readable.");
}
if (basename($path) === ".DS_Store") {
continue;
}
if (!is_file($path)) {
continue;
}
$contents = file_get_contents($path);
echo "Old content: {$contents}\n";
// Grab the permalink front matter, strip spaces.
$folderpattern = "~permalink: /blog/(,?.*)/~";
preg_match($folderpattern, $contents, $foldermatches);
$foldername = $foldermatches[1] ?? false;
trim($foldername ?? "");
echo "Folder name: {$foldername}\n";
echo $directory . $foldername . "\n";
// Get the date from the file name, make new directory.
$date = substr($item->getFilename(), 0, 10);
$cleandate = str_replace("-", "", $date);
$newDirectory = $cleandate . "_" . $foldername;
echo "Date: {$cleandate}\n";
echo "New directory:" . $directory . $newDirectory . "\n";
if (
!file_exists($directory . $newDirectory) &&
!is_dir($directory . $newDirectory)
) {
mkdir($directory . $newDirectory, 0777, true);
}
// Dashes and spaces around fields
$dashes = "----";
$newline = "\n";
// Remove first front matter dash.
file_put_contents(
$path,
preg_replace("~---~", "", file_get_contents($path), 1)
);
// Replace second front matter dash with text.
file_put_contents(
$path,
preg_replace(
"~" . "---" . $newline . $newline . "~",
"Text: ",
file_get_contents($path),
1
)
);
// Find title.
$titlepattern = "~title:(,?.*)~";
preg_match($titlepattern, $contents, $titlematches);
$title = $titlematches[0] ?? false;
$newtitlelines = $title . $newline . $newline . $dashes;
echo "Title matches: " . $newtitlelines . "\n";
// Add dashes after title.
file_put_contents(
$path,
str_replace($title, $newtitlelines, file_get_contents($path))
);
// Find description.
$descpattern = "~description:(,?.*)~";
preg_match($descpattern, $contents, $descmatches);
$desc = $descmatches[0] ?? false;
$newdesclines = $newline . $desc . $newline . $newline . $dashes;
echo "Desc matches: " . $newdesclines . "\n";
// Add dashes after description.
file_put_contents(
$path,
str_replace($desc, $newdesclines, file_get_contents($path))
);
// Remove featuredimg: true - not needed any longer.
file_put_contents(
$path,
str_replace("featuredimg: true", "", file_get_contents($path))
);
// Reload the file since it's changed. Maybe a better way around this.
$contents = file_get_contents($path);
// Find img.
$imgpattern = "~img:(,?.*)~";
preg_match($imgpattern, $contents, $imgmatches);
$img = $imgmatches[0] ?? false;
$newimglines = $img . $newline . $newline . $dashes;
echo "Img matches: " . $newimglines . "\n";
// Add dashes after img.
file_put_contents(
$path,
str_replace($img, $newimglines, file_get_contents($path))
);
// Replace img: with cover: - better field name.
file_put_contents(
$path,
str_replace("img:", "Cover:", file_get_contents($path))
);
// Find img.
$altpattern = "~imgalt:(,?.*)~";
preg_match($altpattern, $contents, $altmatches);
$alt = $altmatches[0] ?? false;
echo "Alt matches: " . $alt . "\n";
// Remove imgalt: - not needed any longer.
file_put_contents($path, str_replace($alt, "", file_get_contents($path)));
// Find the date.
$datepattern = "~date:(,?.*)~";
preg_match($datepattern, $contents, $datematches);
$date = $datematches[0] ?? false;
echo "Date matches: " . $date . "\n";
$removet = str_replace("T", " ", $date);
echo "T removed: " . $removet . "\n";
$removeplus = preg_match("~\+(,?.*)~", $removet, $removeplusmatches);
echo "Remove plus: " . $removeplusmatches[0] . "\n";
$newdate = str_replace($removeplusmatches[0], "", $removet);
echo "New date: " . $newdate . "\n";
$newdatelines = $newdate . $newline . $newline . $dashes;
// Add new date and dashes.
file_put_contents(
$path,
str_replace($date, $newdatelines, file_get_contents($path))
);
// Find the permalink.
$permapattern = "~permalink:(,?.*)~";
preg_match($permapattern, $contents, $permamatches);
$permalink = $permamatches[0] ?? false;
echo "Permalink matches: " . $permalink . "\n";
// Remove permalink - not needed any longer.
file_put_contents(
$path,
str_replace($permalink, "", file_get_contents($path))
);
// Convert old tag format into comma separated list.
$tagpattern = "~\s\s-\s(,?.*)~";
preg_match_all($tagpattern, $contents, $tagmatches);
$tags = implode(", ", $tagmatches[1] ?? false);
echo "New tags: " . $tags . "\n";
// Add new tag format.
file_put_contents(
$path,
str_replace(
"tags:",
"Tags: " . $tags . $newline . $newline . $dashes,
file_get_contents($path)
)
);
// Remove old tags - not needed any longer.
file_put_contents(
$path,
str_replace($tagmatches[0], "", file_get_contents($path))
);
// Remove any multiple blank lines and replace with one.
file_put_contents(
$path,
preg_replace("~\n(\s*\n){2,}~", "\n\n", file_get_contents($path))
);
// Capitalize a few words for consistency.
file_put_contents(
$path,
str_replace("title:", "Title:", file_get_contents($path))
);
file_put_contents(
$path,
str_replace("description:", "Description:", file_get_contents($path))
);
file_put_contents(
$path,
str_replace("date:", "Date:", file_get_contents($path))
);
// Move files into new directory, made earlier and change file extension.
rename($path, $directory . $newDirectory . "/" . "article.txt");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment