Main ➡️    🎥YouTube   🏴󠁡󠁲󠁸󠁿 @willblew  ₿ = 96020.1 USD   
Will Blew - I added markdown support to the blog.

Adding markdown without overthinking it.

So, to start how is this blog written to begin with... Well to keep it simple, it uses code similar to below to load Yaml files then input the values to the blog entry based on the ID of the blog in yaml and renders via PHP echoing. At least it did, see below for an example of this code:

function load_yaml_generic($file_name) {
    try {
        $parsed = yaml_parse_file($file_name);
        return $parsed;
    } catch (Exception $e) {
        header('Location: index.php');
        exit;
    }
}

Once this is done it will load each value into the blog post. So in order to move out of this intentionally simplistic model I needed to support both the Yaml loading and now markdown to get us all this fancy formatting you see here without requiring additional html or two systems.

So, I did the shortest throw and all it required was:

In this case I went with Parsedown since I've used it many times and it's simple.

The end change to the blog itself ended up looking like this in total

Value setting up top of blog.php

$parsed_data = load_yaml($id); // $id is validated elsewhere for safeness
$title = $parsed_data['title'];
$blog_text = $parsed_data['blog_text'];
$author = $parsed_data['author'];
$markdown_mode = $parsed_data['markdown'];

if ($markdown_mode) {
    $file = file_get_contents("mods/blogs/" . $id . ".md", true);
    $markdown_text = $file;
    $Parsedown = new Parsedown();
}

Blog post switching based on Yaml value

    if ($markdown_mode) {
        echo $Parsedown->text($markdown_text);
    } else {
        echo $blog_text; 
    }
    ?>

That's it! Now we have markdown support via Parsedown, comments (because they're imlemented independent of the blog post themselves), and now this blog post using it!



Author: Will Blew



Add a comment!