Add maxLevel setting to plugin configuration; enhance TOC heading depth selection
This commit is contained in:
1 parent
85db2cdbcb
commit
ef100b2dcd
1 file changed
+18
-3
+18
-3
@@ -7,6 +7,7 @@ class PluginBluditToc extends Plugin {
|
|||||||
$this->dbFields = array(
|
$this->dbFields = array(
|
||||||
'title' => 'On this page',
|
'title' => 'On this page',
|
||||||
'navbarHeight' => 80,
|
'navbarHeight' => 80,
|
||||||
|
'maxLevel' => 4,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ class PluginBluditToc extends Plugin {
|
|||||||
|
|
||||||
$title = htmlspecialchars($this->getValue('title'), ENT_QUOTES, 'UTF-8');
|
$title = htmlspecialchars($this->getValue('title'), ENT_QUOTES, 'UTF-8');
|
||||||
$navbarHeight = (int) $this->getValue('navbarHeight');
|
$navbarHeight = (int) $this->getValue('navbarHeight');
|
||||||
|
$maxLevel = (int) $this->getValue('maxLevel');
|
||||||
|
|
||||||
$h = '';
|
$h = '';
|
||||||
|
|
||||||
@@ -23,6 +25,16 @@ class PluginBluditToc extends Plugin {
|
|||||||
$h .= '<input class="form-control" type="text" name="title" value="' . $title . '">';
|
$h .= '<input class="form-control" type="text" name="title" value="' . $title . '">';
|
||||||
$h .= '</div>';
|
$h .= '</div>';
|
||||||
|
|
||||||
|
$h .= '<div class="form-group">';
|
||||||
|
$h .= '<label>' . $L->get('Heading depth') . '</label>';
|
||||||
|
$h .= '<select class="form-control" name="maxLevel">';
|
||||||
|
$h .= '<option value="2"' . ($maxLevel === 2 ? ' selected' : '') . '>h2</option>';
|
||||||
|
$h .= '<option value="3"' . ($maxLevel === 3 ? ' selected' : '') . '>h2, h3</option>';
|
||||||
|
$h .= '<option value="4"' . ($maxLevel === 4 ? ' selected' : '') . '>h2, h3, h4</option>';
|
||||||
|
$h .= '</select>';
|
||||||
|
$h .= '<small class="form-text text-muted">' . $L->get('Deepest heading level included in the table of contents.') . '</small>';
|
||||||
|
$h .= '</div>';
|
||||||
|
|
||||||
$h .= '<div class="form-group">';
|
$h .= '<div class="form-group">';
|
||||||
$h .= '<label>' . $L->get('Navbar height (px)') . '</label>';
|
$h .= '<label>' . $L->get('Navbar height (px)') . '</label>';
|
||||||
$h .= '<input class="form-control" type="number" name="navbarHeight" min="0" max="300" value="' . $navbarHeight . '">';
|
$h .= '<input class="form-control" type="number" name="navbarHeight" min="0" max="300" value="' . $navbarHeight . '">';
|
||||||
@@ -76,17 +88,20 @@ class PluginBluditToc extends Plugin {
|
|||||||
public function siteBodyEnd() {
|
public function siteBodyEnd() {
|
||||||
$title = json_encode($this->getValue('title'));
|
$title = json_encode($this->getValue('title'));
|
||||||
$navbarHeight = (int) $this->getValue('navbarHeight');
|
$navbarHeight = (int) $this->getValue('navbarHeight');
|
||||||
|
$maxLevel = (int) $this->getValue('maxLevel');
|
||||||
|
|
||||||
return '<script>(function(){' .
|
return '<script>(function(){' .
|
||||||
'var TITLE=' . $title . ',' .
|
'var TITLE=' . $title . ',' .
|
||||||
'NAVBAR_HEIGHT=' . $navbarHeight . ';' .
|
'NAVBAR_HEIGHT=' . $navbarHeight . ',' .
|
||||||
|
'MAX_LEVEL=' . $maxLevel . ';' .
|
||||||
|
|
||||||
/* ---- Find content area ---- */
|
/* ---- Find content area ---- */
|
||||||
'var content=document.querySelector(".content")||document.querySelector("article .entry-content")||document.querySelector(".entry-content")||document.querySelector(".post-content");' .
|
'var content=document.querySelector(".content")||document.querySelector("article .entry-content")||document.querySelector(".entry-content")||document.querySelector(".post-content");' .
|
||||||
'if(!content)return;' .
|
'if(!content)return;' .
|
||||||
|
|
||||||
/* ---- Collect headings ---- */
|
/* ---- Collect headings (up to MAX_LEVEL) ---- */
|
||||||
'var headings=content.querySelectorAll("h2,h3,h4");' .
|
'var sel="h2";if(MAX_LEVEL>=3)sel+=",h3";if(MAX_LEVEL>=4)sel+=",h4";' .
|
||||||
|
'var headings=content.querySelectorAll(sel);' .
|
||||||
'if(!headings.length)return;' .
|
'if(!headings.length)return;' .
|
||||||
|
|
||||||
/* ---- Assign IDs ---- */
|
/* ---- Assign IDs ---- */
|
||||||
|
|||||||
Reference in new issue
Block a user