Dikki Markdown is a league/commonmark
wrapper you can use to fetch contents, including the front matter, from markdown
files. Say you
write your blog posts in markdown. You can use this package to fetch the contents of the markdown file as html and the
rest of the front matter as arrays.
You can also convert html to markdown using this package.
Basically, it is just a wrapper around league/commonmark and league/html-to-markdown.
composer require dikki/markdown
The below examples will illustrate.
sample-post.md:
---
title: This is a sample title
description: This is a description
cover_image: /path/to/image.png
slug: sample-post
---
This is the body content of the blog post.
using the parser in your app:
<?php
use Dikki\Markdown\MarkdownParser;
require_once '../vendor/autoload.php';
$parser = new MarkdownParser(
__DIR__ . '/contents/'
);
$output = $parser->getFileContent("sample-post");
echo "<pre>";
print_r($output);
Output:
Array
(
[title] => This is a sample title
[description] => This is a description
[cover_image] => /path/to/image.png
[slug] => sample-post
[content] =>
This is the body content of the blog post.
)
As you can see, you get an array of meta-data you passed as Yaml in the markdown file as well as the content you wrote.
Simply use the HtmltoMdConverter()
class's convert()
method:
$string = "<h1>Heading 1 Here</h1>";
$md = (new \Dikki\Markdown\HtmlToMdConverter())->convert($string);
echo $md;
// OUTPUT:
# Heading 1 Here