diff options
author | Freya Murphy <freya@freyacat.org> | 2024-05-24 09:05:42 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-05-24 09:05:42 -0400 |
commit | c5f39ea2cd7cf02246705ea8872d3b350526165c (patch) | |
tree | 2694f9fdc5d83b529a01f2997c1d89c271c86592 /src/web/helpers/markdown.php | |
download | website-c5f39ea2cd7cf02246705ea8872d3b350526165c.tar.gz website-c5f39ea2cd7cf02246705ea8872d3b350526165c.tar.bz2 website-c5f39ea2cd7cf02246705ea8872d3b350526165c.zip |
initial
Diffstat (limited to 'src/web/helpers/markdown.php')
-rw-r--r-- | src/web/helpers/markdown.php | 33 |
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; + } + +} |