simplydi/simplymarkdown

A very simple way to handle Markdown content on your website

dev-main 2023-08-04 16:58 UTC

This package is auto-updated.

Last update: 2024-04-04 18:36:13 UTC


README

A very simple way to handle Markdown content on your website

This package can be used to render the markdown content on your website. For example, you can write blog posts on your blog using markdown and parse it using SimplyMarkdown. It will return you the markdown content as html as well as the meta data on your markdown file tht you set, such as title, slug, description, etc.

The below example will illustrate.

Example 1: getting the contents of a markdown post:

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 SimplyDi\SimplyMarkdown\MdParser;

require_once '../vendor/autoload.php';

$parser = new MdParser(
    __DIR__ . '/md/'
);

$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.


)

A 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.

Converting Html to Markdown

Simply use the HtmlToMd() class's convert() method:

$string = "<h1>Heading 1 Here</h1>";

$md = (new \SimplyDi\SimplyMarkdown\HtmlToMd)->convert($string);

echo $md;

// OUTPUT:

# Heading 1 Here