summaryrefslogtreecommitdiff
path: root/src/web/lib/markdown.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/lib/markdown.php')
-rw-r--r--src/web/lib/markdown.php36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/web/lib/markdown.php b/src/web/lib/markdown.php
new file mode 100644
index 0000000..5279a1f
--- /dev/null
+++ b/src/web/lib/markdown.php
@@ -0,0 +1,36 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+
+class MarkdownParser {
+
+ private $parsedown;
+
+ function __construct() {
+ $this->parsedown = new ParsedownExtra();
+ }
+
+ /**
+ * @return array<string,mixed>
+ */
+ function parse(string $path): array {
+ $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;
+ }
+
+}