aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace/Parsers/MarkdownParser.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/namespace/Parsers/MarkdownParser.php')
-rw-r--r--core/namespace/Parsers/MarkdownParser.php37
1 files changed, 37 insertions, 0 deletions
diff --git a/core/namespace/Parsers/MarkdownParser.php b/core/namespace/Parsers/MarkdownParser.php
new file mode 100644
index 0000000..27a18ad
--- /dev/null
+++ b/core/namespace/Parsers/MarkdownParser.php
@@ -0,0 +1,37 @@
+<?php
+namespace Parsers;
+use Parsedown;
+
+class MarkdownParser implements ParserInterface {
+ private $Parsedown;
+
+ #===========================================================================
+ # Initialize
+ #===========================================================================
+ public function __construct() {
+ $this->Parsedown = new Parsedown();
+ $this->Parsedown->setUrlsLinked(FALSE);
+ }
+
+ #===========================================================================
+ # Parse Markdown (currently only images)
+ #===========================================================================
+ public function parse(string $text): array {
+ $image = '#\!\[(.*)\]\((.*)(?:\s[\'"](.*)[\'"])?\)#U';
+
+ if(preg_match_all($image, $text, $matches)) {
+ $data['img']['src'] = $matches[2];
+ $data['img']['alt'] = $matches[1];
+ $data['img']['title'] = $matches[3];
+ }
+
+ return $data ?? [];
+ }
+
+ #===========================================================================
+ # Transform Markdown to HTML
+ #===========================================================================
+ public function transform(string $text): string {
+ return $this->Parsedown->text($text);
+ }
+}