quolc
level 3
level 2
content
level 1
content
階層構造が見やすい見出しを出すユーザースクリプト
こんな感じ
https://i.gyazo.com/ba5f8b1df98f178bca3695a4a021a248.png
code: script.js
(function () {
scrapbox.PageMenu.addMenu({
title: '見出し',
onClick: () => {
scrapbox.PageMenu('見出し').removeAllItems()
var deepest = 0;
for (let line of scrapbox.Page.lines) {
if (!line.section.start) continue
var depth = line.nodes ? calcDepth(line.nodes) : 0
if (deepest < depth) deepest = depth
}
console.log('deepest index is ' + deepest)
for (let line of scrapbox.Page.lines) {
if (!line.section.start) continue
const image = line.nodes && getIconUrl(line.nodes)
const noIcon = !!image
const title = line.nodes ? renderPlainText(line.nodes, {noIcon, level: 0}, deepest) : line.text
const onClick = () => location.hash = line.id
scrapbox.PageMenu('見出し').addItem({title, image, onClick})
}
}
})
function calcDepth (node) {
if (node instanceof Array) return node.map(node => calcDepth(node)).reduce(function(a,b) {return Math.max(a,b);}, 0)
if (typeof node === 'string') return 0
if (node.type == 'deco') {
var deco = '*'
for (var i=1; i<=5; i++) {
if (node.unit.deco == deco) return i
deco = deco + '*'
}
}
return calcDepth(node.children)
}
function renderPlainText (node, options, deepest) {
if (node instanceof Array) return node.map(node => renderPlainText(node, options, deepest)).join('')
if (typeof node === 'string') {
var indent = deepest - options.level
var item = node
if (options.level == 1) item = '・' + item
if (options.level == 2) item = '● ' + item
if (options.level == 3) item = '■ ' + item
if (options.level == 4) item = '■ ' + item
if (options.level == 5) item = '■ ' + item
for (var i=0; i<indent; i++) item = ' ' + item;
return item
}
var new_options = Object.assign({}, options)
switch (node.type) {
case 'icon':
case 'strong-icon':
return options.noIcon ? ' ' : node.unit.page
case 'deco':
new_options.level = 0
var deco = '*'
for (var i=1; i<=5; i++) {
if (node.unit.deco == deco) new_options.level = i
deco = deco + '*'
}
}
return renderPlainText(node.children, new_options, deepest)
}
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)
}
return null
}
})()