summaryrefslogtreecommitdiff
path: root/src/web/helpers/markdown.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/helpers/markdown.php')
-rw-r--r--src/web/helpers/markdown.php33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/web/helpers/markdown.php b/src/web/helpers/markdown.php
new file mode 100644
index 0000000..39b430c
--- /dev/null
+++ b/src/web/helpers/markdown.php
@@ -0,0 +1,33 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+
+class MarkdownParser {
+
+ private $parsedown;
+
+ function __construct() {
+ $this->parsedown = new ParsedownExtra();
+ }
+
+ function parse($path) {
+ $content = file_get_contents($path);
+ $data = array(
+ 'meta' => array(),
+ 'content' => $content
+ );
+ if (str_starts_with($content, '---')) {
+ $parts = explode('---', $content);
+ $data['content'] = trim(implode('---', array_slice($parts, 2)));
+ $meta = array_filter(explode("\n", $parts[1]), fn($x) => $x != '');
+ foreach ($meta as $set) {
+ $parts = explode(": ", $set);
+ $key = trim($parts[0]);
+ $value = trim($parts[1]);
+ $data['meta'][$key] = $value;
+ }
+
+ }
+ $data['content'] = $this->parsedown->text($data['content']);
+ return $data;
+ }
+
+}