見出し一覧出すスクリプト
見出し一覧
単純に line.text から /^\[\*+ ([^\]]+)\].*$/ で抜き出すようにした
つまり、行頭にある場合のみ、括弧内のみを見出しに置く
code:script.js
const makeIndent = n => {
if (n === 0) {
return ''
}
let blank = ''
for (let i = 0; i < (4 - n); i++) {
blank = ${blank}
}
return ${blank}*
}
const subtitleExpr = /^\[(\*+|\*(^\+)\]+.*$/ const countAsterisk = str => {
if (!str.match(/^\[(\*+|\[)/)) {
return 0
}
return str.replace(subtitleExpr, '$1').length
}
const addMidashi = line => {
const image = ""
const noIcon = false
const text = line.text.replace(subtitleExpr, '$2')
const title = ${makeIndent(countAsterisk(line.text))}${text}
const onClick = () => location.hash = line.id
scrapbox.PageMenu('contents').addItem({ title, image, onClick })
}
scrapbox.PageMenu.addMenu({
title: 'contents',
onClick: () => {
scrapbox.PageMenu('contents').removeAllItems()
addMidashi(scrapbox.Page.lines0) scrapbox.Page.lines.forEach(line => {
if (line.text.match(/^\[(\*+|\[)/)) {
addMidashi(line)
}
})
}
})