krz/php-blog

A PHP blogging system with built-in static comments.

clone: git clone https://gitbay.org/krz/php-blog.git

main: _functions/parseMarkdown.php · raw

 1<?php
 2
 3function parseMarkdown(string $fileName = null, string $fileContents = null)
 4{
 5    if ($fileName != null) {
 6        // Get file contents
 7        $fileContents = file_get_contents('/var/www/' . $GLOBALS['shortDomain'] . '/' . $fileName, FILE_USE_INCLUDE_PATH);
 8    }
 9
10    // Parse the markdown to HTML
11    include_once('_classes/Parsedown.php');
12    $md = Parsedown::instance()->text($fileContents);
13    $html = new DOMDocument();
14    $html->loadHTML($md);
15    $html_links = $html->getElementsByTagName('a');
16    foreach ($html_links as $html_ink) {
17        $html_ink->setAttribute('rel', 'noopener,noreferrer');
18        $html_ink->setAttribute('target', '_blank');
19    }
20    return $html->saveHTML();
21}