From 6835c77384d2694a193f9422a5e69864f82d3f41 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 20 May 2017 16:07:42 +0100
Subject: [PATCH] Add initial jump to comments link, but it doesn't work yet.

---
 build/index.php              | 23 +++++++++++++++++++++--
 module_index.json            |  4 ++--
 modules/feature-comments.php |  6 +++++-
 modules/page-view.php        |  2 +-
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/build/index.php b/build/index.php
index fdd05c3..9c7a9de 100644
--- a/build/index.php
+++ b/build/index.php
@@ -729,6 +729,20 @@ function endsWith($whole, $end)
 {
     return (strpos($whole, $end, strlen($whole) - strlen($end)) !== false);
 }
+/**
+ * Replaces the first occurrence of $find with $replace.
+ * @param  string $find    The string to search for.
+ * @param  string $replace The string to replace the search string with.
+ * @param  string $subject The string ot perform the search and replace on.
+ * @return string		   The source string after the find and replace has been performed.
+ */
+function str_replace_once($find, $replace, $subject)
+{
+	$index = strpos($subject, $find);
+	if($index !== false)
+		return substr_replace($subject, $replace, $index, strlen($find));
+	return $subject;
+}
 
 /**
  * Returns the system's mime type mappings, considering the first extension
@@ -2210,7 +2224,7 @@ register_module([
 				
 				
 				$comments_html = "<aside class='comments'>" . 
-					"<h2>Comments</h2>\n";
+					"<h2 id='comments'>Comments</h2>\n";
 				
 				if($env->is_logged_in) {
 					$comments_html .= "<form class='comment-reply-form' method='post' action='?action=comment&page=" . rawurlencode($env->page) . "'>\n" . 
@@ -2233,7 +2247,12 @@ register_module([
 				
 				$comments_html .= "</aside>\n";
 				
+				$to_comments_link = "<a href='#comments'>Jump to comments</a>";
+				
 				$parts["{extra}"] = $comments_html . $parts["{extra}"];
+				
+				// fixme
+				$parts["{content}"] = str_replace_once("</h1>", "</h1>\n$to_comments_link", $parts["{content}"]);
 			});
 			
 			$reply_js_snippet = <<<'REPLYJS'
@@ -6098,7 +6117,7 @@ register_module([
 			$page = $env->page;
 			if(!isset($pageindex->$page))
 			{
-				// todo make this intelligent so we only redirect if the user is acutally able to create the page
+				// todo make this intelligent so we only redirect if the user is actually able to create the page
 				if($settings->editing)
 				{
 					// Editing is enabled, redirect to the editing page
diff --git a/module_index.json b/module_index.json
index 539783f..abe1709 100755
--- a/module_index.json
+++ b/module_index.json
@@ -50,7 +50,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds threaded comments to the bottom of every page.",
         "id": "feature-comments",
-        "lastupdate": 1495289583,
+        "lastupdate": 1495292573,
         "optional": false
     },
     {
@@ -230,7 +230,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to view pages. You really should include this one.",
         "id": "page-view",
-        "lastupdate": 1482008539,
+        "lastupdate": 1495291771,
         "optional": false
     },
     {
diff --git a/modules/feature-comments.php b/modules/feature-comments.php
index 6225a89..1431d88 100644
--- a/modules/feature-comments.php
+++ b/modules/feature-comments.php
@@ -137,7 +137,7 @@ register_module([
 				
 				
 				$comments_html = "<aside class='comments'>" . 
-					"<h2>Comments</h2>\n";
+					"<h2 id='comments'>Comments</h2>\n";
 				
 				if($env->is_logged_in) {
 					$comments_html .= "<form class='comment-reply-form' method='post' action='?action=comment&page=" . rawurlencode($env->page) . "'>\n" . 
@@ -160,7 +160,11 @@ register_module([
 				
 				$comments_html .= "</aside>\n";
 				
+				$to_comments_link = "<a href='#comments'>Jump to comments</a>";
+				
 				$parts["{extra}"] = $comments_html . $parts["{extra}"];
+				
+				$parts["{content}"] = str_replace_once("</h1>", "</h1>\n$to_comments_link", $parts["{content}"]);
 			});
 			
 			$reply_js_snippet = <<<'REPLYJS'
diff --git a/modules/page-view.php b/modules/page-view.php
index 10e67e1..42e9cd1 100644
--- a/modules/page-view.php
+++ b/modules/page-view.php
@@ -34,7 +34,7 @@ register_module([
 			$page = $env->page;
 			if(!isset($pageindex->$page))
 			{
-				// todo make this intelligent so we only redirect if the user is acutally able to create the page
+				// todo make this intelligent so we only redirect if the user is actually able to create the page
 				if($settings->editing)
 				{
 					// Editing is enabled, redirect to the editing page
