ページの見出しを作るUserScript
参考
code:script.js
(function () {
scrapbox.PageMenu.addMenu({
title: '目次',
onClick: () => {
scrapbox.PageMenu('目次').removeAllItems()
for (let line of scrapbox.Page.lines) {
if (!line.section.start && (line.text.indexOf('IMP') == -1) && (line.text.indexOf('TODO') == -1)) continue let image = line.nodes && getIconUrl(line.nodes)
if(line.text.indexOf('IMP') != -1){ image = /api/pages/Hyouhyan/IMP/icon
}
if(line.text.indexOf('TODO') != -1){ image = /api/pages/Hyouhyan/TODO/icon
}
const noIcon = !!image
const title = line.nodes ? renderPlainText(line.nodes, {noIcon}) : line.text
const onClick = () => location.hash = line.id
scrapbox.PageMenu('目次').addItem({title, image, onClick})
}
}
})
function renderPlainText (node, options) {
if (node instanceof Array) return node.map(node => renderPlainText(node, options)).join('')
if (typeof node === 'string') return node
switch (node.type) {
case 'icon':
case 'strong-icon':
return options.noIcon ? ' ' : node.unit.page
}
return renderPlainText(node.children, options)
}
function getIconUrl (node) {
if (/icon/.test(node.type)) {
return /api/pages/${node.unit.project||scrapbox.Project.name}/${node.unit.page}/icon
}
if (node instanceof Array) {
return node.map(getIconUrl).find(img => img)
}
if (node.children) return getIconUrl(node.children)
return null
}
})()