From bdd2449ab59a107e1727c67bd834c991ace244da Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 20 Oct 2019 16:44:32 +0100
Subject: [PATCH] Add automatic heading id if you don't specify one

---
 Changelog.md                 |  1 +
 module_index.json            | 10 ++++----
 modules/parser-parsedown.php | 49 ++++++++++++++++++++++++++++++++----
 3 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 4fc0ef0..952536d 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -16,6 +16,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
  - Added mega-menu support to the `nav_links_extra` setting - the default value for the `nav_links_extra` setting has now changed (delete/rename it in your `peppermint.json` file to get the new version)
      - An object can now be used to define groups of items in the more menu
      - Hopefully it now looks less cluttered :P
+ - Headings now have an automatic id if you don't specify one (part of #141)
 
 ### Fixed
  - Fixed a bug in the search query performance metrics
diff --git a/module_index.json b/module_index.json
index 73a5746..311b97e 100755
--- a/module_index.json
+++ b/module_index.json
@@ -82,10 +82,10 @@
     {
         "id": "feature-guiconfig",
         "name": "Settings GUI",
-        "version": "0.1.6",
+        "version": "0.1.7",
         "author": "Starbeamrainbowlabs",
         "description": "The module everyone has been waiting for! Adds a web based gui that lets mods change the wiki settings.",
-        "lastupdate": 1571530165,
+        "lastupdate": 1571583667,
         "optional": false,
         "extra_data": []
     },
@@ -152,10 +152,10 @@
     {
         "id": "feature-theme-gallery",
         "name": "Theme Gallery",
-        "version": "0.3",
+        "version": "0.4",
         "author": "Starbeamrainbowlabs",
         "description": "Adds a theme gallery page and optional automatic theme updates. Contacts a remote server, where IP addresses are stored in automatic server logs for security and attack mitigation purposes.",
-        "lastupdate": 1571531070,
+        "lastupdate": 1571583643,
         "optional": false,
         "extra_data": []
     },
@@ -337,7 +337,7 @@
         "version": "0.10",
         "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.",
-        "lastupdate": 1569062035,
+        "lastupdate": 1571586164,
         "optional": false,
         "extra_data": {
             "Parsedown.php": "https:\/\/raw.githubusercontent.com\/erusev\/parsedown\/fe7a50eceb4a3c867cc9fa9c0aa906b1067d1955\/Parsedown.php",
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index 25fefc9..e606c02 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -774,8 +774,7 @@ class PeppermintParsedown extends ParsedownExtreme
 			
 			// ~
 			
-			if($imageCaption)
-			{
+			if($imageCaption) {
 				$rawStyle = $result["element"]["text"][0]["attributes"]["style"];
 				$containerStyle = preg_replace('/^.*float/', "float", $rawStyle);
 				$mediaStyle = preg_replace('/\s*float.*;/', "", $rawStyle);
@@ -799,6 +798,41 @@ class PeppermintParsedown extends ParsedownExtreme
 		}
 	}
 	
+	/*
+	 * ██   ██ ███████  █████  ██████  ███████ ██████
+	 * ██   ██ ██      ██   ██ ██   ██ ██      ██   ██
+	 * ███████ █████   ███████ ██   ██ █████   ██████
+	 * ██   ██ ██      ██   ██ ██   ██ ██      ██   ██
+	 * ██   ██ ███████ ██   ██ ██████  ███████ ██   ██
+	 */
+	
+	private $headingIdsUsed = [];
+	
+	protected function blockHeader($line) {
+		// This function overrides the header function defined in ParsedownExtra
+		$result = parent::blockHeader($line);
+		
+		// If this heading doesn't have an id already, add an automatic one
+		if(!isset($result["element"]["attributes"]["id"])) {
+			$heading_id = str_replace(" ", "-",
+				mb_strtolower(makepathsafe(
+					$result["element"]["handler"]["argument"]
+				))
+			);
+			$suffix = "";
+			while(in_array($heading_id . $suffix, $this->headingIdsUsed)) {
+				$heading_number = intval(str_replace("_", "", $suffix));
+				if($heading_number == 0) $heading_number++;
+				$suffix = "_" . ($heading_number + 1);
+			}
+			$result["element"]["attributes"]["id"] = $heading_id . $suffix;
+			$this->headingIdsUsed[] = $result["element"]["attributes"]["id"];
+		}
+		
+		error_log("[header info] " . var_export($result, true));
+		return $result;
+	}
+	
 	# ~
 	# Static Methods
 	# ~
@@ -840,9 +874,14 @@ class PeppermintParsedown extends ParsedownExtreme
 		return $result;
 	}
 	
-	# ~
-	# Utility Methods
-	# ~
+	
+	/*
+	 * ██    ██ ████████ ██ ██      ██ ████████ ██ ███████ ███████
+	 * ██    ██    ██    ██ ██      ██    ██    ██ ██      ██
+	 * ██    ██    ██    ██ ██      ██    ██    ██ █████   ███████
+	 * ██    ██    ██    ██ ██      ██    ██    ██ ██           ██
+	 *  ██████     ██    ██ ███████ ██    ██    ██ ███████ ███████
+	 */
 	
 	/**
 	 * Returns whether a string is a valid float: XXXXXX; value.
