From fb9eec2d330828a6107166d633e80f91d6c3a35b Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Wed, 21 Jul 2021 00:44:31 +0100 Subject: [PATCH] Fix & improve sidebar --- Changelog.md | 1 + modules/extra-sidebar.php | 35 +++++++++++++++++++++++++++-------- peppermint.guiconfig.json | 3 ++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Changelog.md b/Changelog.md index fac4259..64c1dce 100644 --- a/Changelog.md +++ b/Changelog.md @@ -25,6 +25,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t - Add `sidebar_show` to the settings GUI and the [configuration guide](https://starbeamrainbowlabs.com/labs/peppermint/peppermint-config-info.php) - Fix crash when using the search bar with PHP 8.0+ - Prefix the default value of the `logo_url` setting with `https:` + - Fix display of subpages in the sidebar, and also wrap subpage lists in a `
` element to allow collapsing them ## v0.22 diff --git a/modules/extra-sidebar.php b/modules/extra-sidebar.php index 9ccdca3..d04e33f 100644 --- a/modules/extra-sidebar.php +++ b/modules/extra-sidebar.php @@ -77,7 +77,7 @@ register_module([ } }); - add_help_section("50-sidebar", "Sidebar", "

$settings->sitename has an optional sidebar which displays a list of all the current pages (but not subpages) that it is currently hosting. It may or may not be enabled.

+ add_help_section("50-sidebar", "Sidebar", "

$settings->sitename has an optional sidebar which displays a list of all the current pages and their respective subpages that it is currently hosting in a tree like structure. It may or may not be enabled.

If it isn't enabled, it can be enabled for your current browser only by appending sidebar=yes to the current page's query string.

If it is enabled, it can be disabled for your current browser only by appending nosidebar to the current page's query string.

"); } @@ -90,14 +90,20 @@ register_module([ * @param string $root_pagename The pagename that should be considered the root of the rendering. You don't usually need to use this, it is used by the algorithm itself since it is recursive. * @return string A HTML rendering of the sidebar for the given pageindex. */ -function render_sidebar($pageindex, $root_pagename = "") +function render_sidebar($pageindex, $root_pagename = "", $depth = 0) { global $settings; + if($depth > $settings->sidebar_maxdepth) + return null; + + if(mb_strlen($root_pagename) > 0) $root_pagename .= "/"; + $result = " $details) { // If we have a valid root pagename, and it isn't present at the @@ -110,20 +116,33 @@ function render_sidebar($pageindex, $root_pagename = "") continue; // If the page already appears on the sidebar, skip it - if(preg_match("/>$pagename<\a>/m", $result) === 1) - continue; + if(strpos($result, ">$pagename<\a>") !== false) + continue; + + $pagename_relative = substr($pagename, strlen($root_pagename)); // If the part of the current pagename that comes after the root // pagename has a slash in it, skip it as it is a sub-sub page. - if(strpos(substr($pagename, strlen($root_pagename)), "/") !== false) + if(strpos($pagename_relative, "/") !== false) continue; - $result .= "
  • $pagename\n"; - $result .= render_sidebar($pageindex, $pagename); - $result .= "
  • \n"; + $subpage_sidebar = render_sidebar($pageindex, $pagename, $depth + 1); + + if($subpage_sidebar === null) { + $result .= "
  • $pagename_relative
  • "; + } + else { + $result .= "
  • + $pagename_relative + $subpage_sidebar +
  • \n"; + } + $subpages_added++; } $result .= "\n"; + if($subpages_added === 0) return null; + return $result == "
      \n" ? "" : $result; } diff --git a/peppermint.guiconfig.json b/peppermint.guiconfig.json index 85ba987..f7df454 100644 --- a/peppermint.guiconfig.json +++ b/peppermint.guiconfig.json @@ -274,5 +274,6 @@ "css_custom": { "type": "textarea", "description": "A string of custom CSS to include on top of the base theme css. Allows for theme customisations while still enabling automatic updates :D Just like the css setting, this one can also be a url.", "default": "/* Enter your custom css here. */" }, "cli_enabled": { "type": "checkbox", "description": "Whether the Pepperminty Wiki CLI is enabled or not.", "default": true }, "cli_prompt": { "type": "text", "description": "The string to use as the prompt in the CLI shell.", "default": "\u0001\u001b[1m\u001b[31m\u0002#\u0001\u001b[0m\u0002 " }, - "sidebar_show": { "type": "checkbox", "description": "Whether to show the sidebar by default to all users or not.", "default": false } + "sidebar_show": { "type": "checkbox", "description": "Whether to show the sidebar by default to all users or not.", "default": false }, + "sidebar_maxdepth": { "type": "number", "description": "The maximum depth of pages to show in the sidebar. Top-level pages are of depth 0, subpages thereof are of depth 1, etc. Defaults to a depth of 1, which indicates to display both top-level pages and their subpages.", "default": 1 } }