]> git.bts.cx Git - cx.git/commitdiff
Custom markdown for inserting images
authorBen Sherratt <redacted>
Sun, 14 Jan 2024 11:29:28 +0000 (11:29 +0000)
committerBen Sherratt <redacted>
Sun, 14 Jan 2024 11:29:28 +0000 (11:29 +0000)
cx/lib/images.php
cx/lib/markdown.php [new file with mode: 0644]
cx/lib/posts.php

index fd6d6227b5a45fdf3e5c0535181bccee20cc62cf..dbe55bf4b23fed11610799cf6a4f90f8348bd5b5 100644 (file)
@@ -58,11 +58,30 @@ function cx_images_get(int $limit = 0) {
        $sql .= ';';
 
        foreach (cx_db_query($sql) as $image) {
-               $p = new Image($image);
-               yield $p;
+               $i = new Image($image);
+               yield $i;
        }
 }
 
+function cx_images_find_image($image_id) {
+       $sql = 'SELECT
+               image_id,
+               image_uid,
+               image_type,
+               image_alt_text
+               FROM images
+               WHERE image_id == ?
+               OR image_uid LIKE ?
+               LIMIT 1;';
+
+       foreach (cx_db_query($sql, $image_id, $image_id . "%") as $image) {
+               $i = new Image($image);
+               return $i;
+       }
+
+       return null;
+}
+
 cx_setup_register(1, function() {
        cx_db_exec('CREATE TABLE images (
                        image_id INTEGER PRIMARY KEY,
diff --git a/cx/lib/markdown.php b/cx/lib/markdown.php
new file mode 100644 (file)
index 0000000..8cdc235
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+cx_require('lib', 'images.php');
+cx_require('lib', 'url.php');
+cx_require('third_party', 'parsedown', 'Parsedown.php');
+
+class ExtendedParsedown extends Parsedown {
+       public function __construct() {
+               $this->InlineTypes['{'][]= 'BlogImage';
+               $this->inlineMarkerList .= '{';
+       }
+
+       protected function inlineBlogImage($excerpt) {
+               if (preg_match('/^{img:([^}]+)}/', $excerpt['text'], $matches)) {
+                       $image_id = $matches[1];
+                       $image = cx_images_find_image($image_id);
+
+                       if ($image == null) {
+                               return;
+                       }
+
+                       $permalink = '/data/images/' . $image->url;
+
+                       return array(
+                               'extent' => strlen($matches[0]) + 1, 
+                               'element' => array(
+                                       'name' => 'img',
+                                       'attributes' => array(
+                                               'src' => cx_url($permalink),
+                                               'alt' => $image->alt_text,
+                                       ),
+                               ),
+                       );
+               }
+       }
+}
+
+function cx_markdown_generate_html($markdown) {
+       static $Parsedown = new ExtendedParsedown();
+       return $Parsedown->text($markdown);
+}
index 0a2ac4d997ff9c5133c0c0a27cffc6e4ee376f9e..988d00eb008c457b873b67876d33f581213b9bba 100644 (file)
@@ -2,13 +2,7 @@
 
 cx_require('lib', 'db.php');
 cx_require('lib', 'setup.php');
-cx_require('third_party', 'parsedown', 'Parsedown.php');
-
-function mk_markdown($markdown) {
-       static $Parsedown = new Parsedown();
-
-       return $Parsedown->text($markdown);
-}
+cx_require('lib', 'markdown.php');
 
 class PostMetadata {
        public $hero_image;
@@ -37,13 +31,13 @@ class Post {
                $this->date = $dict['post_date'];
                $this->is_draft = $dict['post_is_draft'];
                $this->data = $dict['post_data'];
-               $this->html_content = mk_markdown($this->data);
+               $this->html_content = cx_markdown_generate_html($this->data);
                $this->html_excerpt = null;
 
                // Read more...
                $segments = explode('---', $this->data, 2);
                if (count($segments) > 1) {
-                       $this->html_excerpt = mk_markdown($segments[0]);
+                       $this->html_excerpt = cx_markdown_generate_html($segments[0]);
                }
        }