added page toc plugin

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-11-29 20:22:46 +03:00
parent 0211c76b1d
commit 811ed21223
4 changed files with 69 additions and 3 deletions

37
pwiki/dom/toc.js Executable file
View File

@ -0,0 +1,37 @@
/**********************************************************************
*
*
*
**********************************************************************/
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
(function(require){ var module={} // make module AMD/node compatible...
/*********************************************************************/
//---------------------------------------------------------------------
var makeToc =
module.makeToc =
function(){
return [...document.querySelectorAll('toc')]
.map(function(toc){
toc.innerHTML = ''
var parent = toc.parentElement
var base = pwiki.path
;[...parent.querySelectorAll('[id]')]
.filter(function(n){
return /^h[0-9]$/i.test(n.nodeName) })
.forEach(function(section){
var e = document.createElement('a')
e.classList.add(section.nodeName.toLowerCase())
e.setAttribute('href', '#'+ base +'#'+ section.id)
e.innerHTML = section.innerHTML
toc.appendChild(e) })
return toc }) }
/**********************************************************************
* vim:set ts=4 sw=4 : */ return module })

View File

@ -1795,7 +1795,7 @@ object.Constructor('Page', BasePage, {
await data()
: typeof(data) == 'function' ?
data
: 'text' in data ?
: data && 'text' in data ?
data.text
: null
if(data instanceof Array
@ -2021,6 +2021,7 @@ object.Constructor('CachedPage', Page, {
//---------------------------------------------------------------------
var toc = require('./dom/toc')
var wikiword = require('./dom/wikiword')
//var textarea = require('./dom/textarea')
@ -2035,6 +2036,7 @@ object.Constructor('pWikiPageElement', Page, {
domFilters: {
toc: toc.makeToc,
// XXX see Page.filters.wikiword for notes...
wikiword: wikiword.wikiWordText,
//textarea: textarea.setupTextarea,
@ -2263,6 +2265,7 @@ module.System = {
@load(./edit)
<macro texteditor>
<toc></toc>
<div class="editor"
wikiwords="no"
contenteditable

View File

@ -150,6 +150,34 @@ body.loading .page.spinner span {
}
}
/* TOC */
toc {
--toc-level-offset: 2em;
}
toc a {
display: block;
}
toc .h1 {
margin-left: 0em;
}
toc .h2 {
margin-left: calc(var(--toc-level-offset) * 1);
}
toc .h3 {
margin-left: calc(var(--toc-level-offset) * 2);
}
toc .h4 {
margin-left: calc(var(--toc-level-offset) * 3);
}
toc .h5 {
margin-left: calc(var(--toc-level-offset) * 4);
}
toc .h5 {
margin-left: calc(var(--toc-level-offset) * 5);
}
.error .msg {
font-weight: bold;
color: red;

View File

@ -38,8 +38,6 @@
*
*
*
* XXX macros: else/default macro args essentially mean the same thing, should we
* unify them to use the same name???
* XXX parser: error handling: revise page quoting...
* ...need a standard mechanism to quote urls, url-args and paths...
* XXX BUG? can't use < and > (both?) in page title...