Last active
December 6, 2017 20:51
-
-
Save dg01d/b34daa68747b7a2e736a14c64832f28b to your computer and use it in GitHub Desktop.
Quick'n'dirty script to convert a folder of YAML-frontmatter files to JSON-frontmatter
This file contains 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 | |
require('vendor/autoload.php'); | |
use Symfony\Component\Yaml\Parser; | |
$yaml = new Parser(); | |
// Path for the content you want to convert | |
$files = glob('./micro/*.md'); | |
foreach($files as $file) { | |
$post = file_get_contents($file); | |
$parts = preg_split('/[\n]*[-]{3}[\n]/', $post, 3); | |
if (!empty($parts['1'])) { | |
$content = $parts['2']; | |
$value = $yaml->parse($parts['1']); | |
$json = json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); | |
file_put_contents($file, $json . "\n\n"); | |
file_put_contents($file, $content, FILE_APPEND); | |
} else echo $file . " is not YAML\n"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You’ll need to use composer to get this to work. Just one dependency:
$ composer require symfony/yaml