From 16076a8d8f5efd46749e093a70476d1c197d3964 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 19 Aug 2016 19:39:53 +0100
Subject: [PATCH] Allow linking in internal links to subheadings.

---
 build/index.php              | 17 ++++++++++++++++-
 module_index.json            |  2 +-
 modules/page-move.php        |  2 +-
 modules/parser-parsedown.php | 17 ++++++++++++++++-
 4 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/build/index.php b/build/index.php
index 7cc7bda..d9a5509 100755
--- a/build/index.php
+++ b/build/index.php
@@ -5040,7 +5040,7 @@ class PeppermintParsedown extends ParsedownExtra
 	
 	protected function inlineInternalLink($fragment)
 	{
-		global $pageindex;
+		global $pageindex, $env;
 		
 		if(preg_match('/^\[\[([^\]]*)\]\]/', $fragment["text"], $matches))
 		{
@@ -5053,6 +5053,18 @@ class PeppermintParsedown extends ParsedownExtra
 				$display = trim($parts[1]); // The text to display
 			}
 			
+			$hashCode = "";
+			if(strpos($linkPage, "#") !== false)
+			{
+				// We want to link to a subsection of a page
+				$hashCode = substr($linkPage, strpos($linkPage, "#") + 1);
+				$linkPage = substr($linkPage, 0, strpos($linkPage, "#"));
+				
+				// If $linkPage is empty then we want to link to the current page
+				if(strlen($linkPage) === 0)
+					$linkPage = $env->page;
+			}
+			
 			// If the page doesn't exist, check varying different
 			// capitalisations to see if it exists under some variant.
 			if(empty($pageindex->$linkPage))
@@ -5070,6 +5082,9 @@ class PeppermintParsedown extends ParsedownExtra
 				$this->internalLinkBase
 			);
 			
+			if(strlen($hashCode) > 0)
+				$linkUrl .= "#$hashCode";
+			
 			$result = [
 				"extent" => strlen($matches[0]),
 				"element" => [
diff --git a/module_index.json b/module_index.json
index 55ef372..12cbbc7 100755
--- a/module_index.json
+++ b/module_index.json
@@ -203,7 +203,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": 1471599779,
+        "lastupdate": 1471631572,
         "optional": false
     }
 ]
\ No newline at end of file
diff --git a/modules/page-move.php b/modules/page-move.php
index ce524f4..4461d70 100644
--- a/modules/page-move.php
+++ b/modules/page-move.php
@@ -82,7 +82,7 @@ register_module([
 				$pageindex->$new_name->$key = $value;
 			}
 			unset($pageindex->$page);
-			$pageindex->$new_name->filename = $new_name;
+			$pageindex->$new_name->filename = "$new_name.md";
 			
 			// If this page has an associated file, then we should move that too
 			if(!empty($pageindex->$new_name->uploadedfile))
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index 68c30e8..dbdbab8 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -295,7 +295,7 @@ class PeppermintParsedown extends ParsedownExtra
 	
 	protected function inlineInternalLink($fragment)
 	{
-		global $pageindex;
+		global $pageindex, $env;
 		
 		if(preg_match('/^\[\[([^\]]*)\]\]/', $fragment["text"], $matches))
 		{
@@ -308,6 +308,18 @@ class PeppermintParsedown extends ParsedownExtra
 				$display = trim($parts[1]); // The text to display
 			}
 			
+			$hashCode = "";
+			if(strpos($linkPage, "#") !== false)
+			{
+				// We want to link to a subsection of a page
+				$hashCode = substr($linkPage, strpos($linkPage, "#") + 1);
+				$linkPage = substr($linkPage, 0, strpos($linkPage, "#"));
+				
+				// If $linkPage is empty then we want to link to the current page
+				if(strlen($linkPage) === 0)
+					$linkPage = $env->page;
+			}
+			
 			// If the page doesn't exist, check varying different
 			// capitalisations to see if it exists under some variant.
 			if(empty($pageindex->$linkPage))
@@ -325,6 +337,9 @@ class PeppermintParsedown extends ParsedownExtra
 				$this->internalLinkBase
 			);
 			
+			if(strlen($hashCode) > 0)
+				$linkUrl .= "#$hashCode";
+			
 			$result = [
 				"extent" => strlen($matches[0]),
 				"element" => [
