Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
use Symfony\Component\Finder\Finder;
require_once __DIR__.'/vendor/autoload.php';
$finder = new Finder();
$files = $finder->in(__DIR__.'/source/_posts')->files();
/** @var \Symfony\Component\Finder\SplFileInfo $file */
foreach ($files as $file) {
$beginsWithDate = preg_match('/^(?<date>\d{4}-\d{2}-\d{2})/', $file->getFilename(), $matches);
if (!$beginsWithDate) {
continue;
}
// Add the date into the front-matter, after the title.
$contents = explode("\n", $file->getContents());
array_splice($contents, 2, 0, "date: {$matches['date']}");
file_put_contents($file->getPathname(), implode("\n", $contents));
// Rename the file to remove the date.
$newPathname = str_replace("{$matches['date']}-", '', $file->getPathname());
rename($file->getPathname(), $newPathname);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment