Skip to content

Commit

Permalink
Merge pull request #14 from keymanapp/chore/support-hash-headers-in-m…
Browse files Browse the repository at this point in the history
…arkdown-parse

chore: support hash headers in Markdown parser
  • Loading branch information
mcdurdin authored Feb 26, 2024
2 parents cdafd6a + 92b40b2 commit 88d06fc
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions _common/MarkdownHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,36 @@ function __construct($file) {
//

$lines = explode("\n", $contents);
$headers = [];

$found = count($lines) > 3 && rtrim($lines[0]) == '---';
$headers = [];
for($i = 1; $i < count($lines); $i++) {
if($lines[$i] == '---') break;
if(!preg_match('/^([a-z0-9_-]+):(.+)$/', $lines[$i], $match)) {
$found = false;
break;
} else {
$headers[$match[1]] = trim($match[2]);
if($found) {
for($i = 1; $i < count($lines); $i++) {
if($lines[$i] == '---') break;
if(!preg_match('/^([a-z0-9_-]+):(.+)$/', $lines[$i], $match)) {
$found = false;
break;
} else {
$headers[$match[1]] = trim($match[2]);
}
}
$found = $found && $i < count($lines);
}
$found = $found && $i < count($lines);

if($found) $contents = implode("\n", array_slice($lines, $i));

if(isset($headers['redirect'])) {
header("Location: {$headers['redirect']}");
exit;
}

if($found) {
$contents = implode("\n", array_slice($lines, $i));
} else {
// We are going to search for the first '#' style heading
if(preg_match('/^(#)+ (.+)$/m', $contents, $sub)) {
$headers['title'] = $sub[2];
}
}

$this->pagetitle = isset($headers['title']) ? $headers['title'] : 'Untitled';
$this->showMenu = isset($headers['showmenu']) ? $headers['showmenu'] == 'true' : true;

Expand Down

0 comments on commit 88d06fc

Please sign in to comment.