Skip to content

Instantly share code, notes, and snippets.

@dg01d
Last active December 6, 2017 20:51
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 dg01d/b34daa68747b7a2e736a14c64832f28b to your computer and use it in GitHub Desktop.
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
<?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";
};
@dg01d
Copy link
Author

dg01d commented Dec 6, 2017

You’ll need to use composer to get this to work. Just one dependency:

$ composer require symfony/yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment