erusev / parsedown

Better Markdown Parser in PHP

Home Page:https://parsedown.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support Frontmatter

wraybowling opened this issue · comments

I'm rather surprised that frontmatter isn't supported. This means that content can't set any useful values for templates to make use of. I came across a repo using Parsedown today that had a different .php file for literally every Markdown file that needed to be parsed because of this. Just so simple things like previous/next links, page title, and language could be set.

Parsedown was intended to only parse markdown syntax, nothing more.

I would add that feature manually instead, and use external library that can parse YAML string to parse the front-matter parts:

$content = file_get_contents(/* ... */);

// Has front-matter
if (0 === strpos($content, "---\n")) {
    $parts = explode("\n---\n", $content, 2);
    $data = Alchemy\Component\Yaml\Yaml::loadString(substr($parts[0], 4));
    $content = (new Parsedown)->text($parts[1] ?? "");
// No front-matter
} else {
    $data = [];
    $content = (new Parsedown)->text($content);
}


var_dump($data, $content);

Slept on the idea and came to the same conclusion. Now I'm not sure why markdown frontmatter was ever a thing. I think the same could be achieved by simply have an adjacent .yaml or .json file with the added benefit of the file extension specifying what type of frontmatter it is. Thank you for the slick solution, though. It may come in handy!