MageWork

Back to home

Write content in Markdown

Library

In this example, we use the michelf/php-markdown package (see external libraries with composer).

composer require michelf/php-markdown

You can adapt it with your preferred Markdown library.

Page override

To write the page content in markdown, we need to override the default Core_Page class (see object override).

packages > Acme > Page.php

<?php

declare(strict_types=1);

use Michelf\MarkdownExtra;

class Acme_Page extends Core_Page
{
    public function include(?string $template): string
    {
        $markdown = App::getPackagePath('template' . DS . $template . '.md');

        if (!is_file($markdown)) {
            return parent::include($template);
        }

        $content = file_get_contents($markdown);

        return MarkdownExtra::defaultTransform($content);
    }
}

Page configuration

Then, we add a new page with the content path (see create a page).

packages > Acme > etc > pages.php

<?php

$config = [
    Core_Page::TYPE => [
        /* ... */
        '/markdown.html' => [
            'content' => 'content/my-markdown-content',
            'meta_title' => 'Markdown Content',
            'meta_description' => 'The content of this page is written in Markdown',
        ],
        /* ... */
    ],
];

Markdown file

packages > Acme > template > content > my-markdown-content.md

# Hello World!

Welcome to my website.