Skip to content

Instantly share code, notes, and snippets.

@opdavies
Last active March 8, 2020 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opdavies/9ec9e47086bd7347d9519514f4faaa8a to your computer and use it in GitHub Desktop.
Save opdavies/9ec9e47086bd7347d9519514f4faaa8a to your computer and use it in GitHub Desktop.
<?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