From 9caadadc669d5106a503cfd3aab4b4b8151eb863 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 11 Oct 2016 19:44:01 +0100
Subject: [PATCH] Link images to their sources, properly handling short image
 urls. Fixes #111.

---
 build/index.php              | 25 ++++++++++++++++++++-----
 module_index.json            |  4 ++--
 modules/parser-parsedown.php | 25 ++++++++++++++++++++-----
 3 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/build/index.php b/build/index.php
index 5ac5096..1cb12a0 100755
--- a/build/index.php
+++ b/build/index.php
@@ -4941,7 +4941,7 @@ register_module([
 
 register_module([
 	"name" => "Parsedown",
-	"version" => "0.9.6",
+	"version" => "0.9.7",
 	"author" => "Emanuil Rusev & Starbeamrainbowlabs",
 	"description" => "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https://github.com/erusev/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.",
 	"id" => "parser-parsedown",
@@ -5352,6 +5352,7 @@ class PeppermintParsedown extends ParsedownExtra
 			$floatDirection = false;
 			$imageSize = false;
 			$imageCaption = false;
+			$shortImageUrl = false;
 			
 			if($this->isFloatValue($param1))
 			{
@@ -5395,8 +5396,8 @@ class PeppermintParsedown extends ParsedownExtra
 			
 			if(isset($pageindex->$imageUrl) and $pageindex->$imageUrl->uploadedfile)
 			{
-				//echo("Found pageindex entry: "); var_dump($pageindex->$imageUrl->uploadedfile);
 				// We have a short url! Expand it.
+				$shortImageUrl = $imageUrl;
 				$imageUrl = "index.php?action=preview&size=" . max($imageSize["x"], $imageSize["y"]) ."&page=" . rawurlencode($imageUrl);
 			}
 			
@@ -5459,6 +5460,20 @@ class PeppermintParsedown extends ParsedownExtra
 					break;
 			}
 			
+			// ~ Image linker ~
+			
+			$imageHref = $shortImageUrl !== false ? "?page=" . rawurlencode($shortImageUrl) : $imageUrl;
+			$result["element"] = [
+				"name" => "a",
+				"attributes" => [
+					"href" => $imageHref
+				],
+				"text" => [$result["element"]],
+				"handler" => "elements"
+			];
+			
+			// ~
+			
 			if($imageCaption)
 			{
 				$rawStyle = $result["element"]["attributes"]["style"];
@@ -5466,6 +5481,9 @@ class PeppermintParsedown extends ParsedownExtra
 				$mediaStyle = preg_replace('/\s*float.*;/', "", $rawStyle);
 				$result["element"] = [
 					"name" => "figure",
+					"attributes" => [
+						"style" => $containerStyle
+					],
 					"text" => [
 						$result["element"],
 						[
@@ -5473,9 +5491,6 @@ class PeppermintParsedown extends ParsedownExtra
 							"text" => $altText
 						],
 					],
-					"attributes" => [
-						"style" => $containerStyle
-					],
 					"handler" => "elements"
 				];
 				$result["element"]["text"][0]["attributes"]["style"] = $mediaStyle;
diff --git a/module_index.json b/module_index.json
index a7a1578..9bd998f 100755
--- a/module_index.json
+++ b/module_index.json
@@ -199,11 +199,11 @@
     },
     {
         "name": "Parsedown",
-        "version": "0.9.6",
+        "version": "0.9.7",
         "author": "Emanuil Rusev & Starbeamrainbowlabs",
         "description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.",
         "id": "parser-parsedown",
-        "lastupdate": 1476814064,
+        "lastupdate": 1476814092,
         "optional": false
     }
 ]
\ No newline at end of file
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index 13e064e..7a90884 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -1,7 +1,7 @@
 <?php
 register_module([
 	"name" => "Parsedown",
-	"version" => "0.9.6",
+	"version" => "0.9.7",
 	"author" => "Emanuil Rusev & Starbeamrainbowlabs",
 	"description" => "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https://github.com/erusev/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.",
 	"id" => "parser-parsedown",
@@ -412,6 +412,7 @@ class PeppermintParsedown extends ParsedownExtra
 			$floatDirection = false;
 			$imageSize = false;
 			$imageCaption = false;
+			$shortImageUrl = false;
 			
 			if($this->isFloatValue($param1))
 			{
@@ -455,8 +456,8 @@ class PeppermintParsedown extends ParsedownExtra
 			
 			if(isset($pageindex->$imageUrl) and $pageindex->$imageUrl->uploadedfile)
 			{
-				//echo("Found pageindex entry: "); var_dump($pageindex->$imageUrl->uploadedfile);
 				// We have a short url! Expand it.
+				$shortImageUrl = $imageUrl;
 				$imageUrl = "index.php?action=preview&size=" . max($imageSize["x"], $imageSize["y"]) ."&page=" . rawurlencode($imageUrl);
 			}
 			
@@ -519,6 +520,20 @@ class PeppermintParsedown extends ParsedownExtra
 					break;
 			}
 			
+			// ~ Image linker ~
+			
+			$imageHref = $shortImageUrl !== false ? "?page=" . rawurlencode($shortImageUrl) : $imageUrl;
+			$result["element"] = [
+				"name" => "a",
+				"attributes" => [
+					"href" => $imageHref
+				],
+				"text" => [$result["element"]],
+				"handler" => "elements"
+			];
+			
+			// ~
+			
 			if($imageCaption)
 			{
 				$rawStyle = $result["element"]["attributes"]["style"];
@@ -526,6 +541,9 @@ class PeppermintParsedown extends ParsedownExtra
 				$mediaStyle = preg_replace('/\s*float.*;/', "", $rawStyle);
 				$result["element"] = [
 					"name" => "figure",
+					"attributes" => [
+						"style" => $containerStyle
+					],
 					"text" => [
 						$result["element"],
 						[
@@ -533,9 +551,6 @@ class PeppermintParsedown extends ParsedownExtra
 							"text" => $altText
 						],
 					],
-					"attributes" => [
-						"style" => $containerStyle
-					],
 					"handler" => "elements"
 				];
 				$result["element"]["text"][0]["attributes"]["style"] = $mediaStyle;
