aboutsummaryrefslogtreecommitdiff
path: root/_functions/parseMarkdown.php
blob: 6ca15d8a1aeb1caf7630f67e633e63c7fc468a19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

function parseMarkdown(string $fileName = null, string $fileContents = null)
{
    if ($fileName != null) {
        // Get file contents
        $fileContents = file_get_contents('/var/www/' . $GLOBALS['shortDomain'] . '/' . $fileName, FILE_USE_INCLUDE_PATH);
    }

    // Parse the markdown to HTML
    include_once('_classes/Parsedown.php');
    $md = Parsedown::instance()->text($fileContents);
    $html = new DOMDocument();
    $html->loadHTML($md);
    $html_links = $html->getElementsByTagName('a');
    foreach ($html_links as $html_ink) {
        $html_ink->setAttribute('rel', 'noopener,noreferrer');
        $html_ink->setAttribute('target', '_blank');
    }
    return $html->saveHTML();
}