Last active
March 8, 2020 14:24
-
-
Save opdavies/9ec9e47086bd7347d9519514f4faaa8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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