簡単見出し
http://lh3.ggpht.com/--Y3hjPlthd0/UVmgLn3PeEI/AAAAAAAAChY/fzPvllFfwbs/optimise-headings-h1-h2-blogger%25255B5%25255D.png
PageMenuに見出しを簡単に付けてくれる
[***** hoge] を <h1>
[*** fuga] を <h2>
のように扱う
code:heading.js
scrapbox.PageMenu.addMenu({
title: '見出し',
onClick: () => {
scrapbox.PageMenu('見出し').removeAllItems()
for (let line of scrapbox.Page.lines) {
if (!line.section.start) continue
if (!line.text.startsWith("[***")) continue
if (!line.nodes) continue
const image = ""
const noIcon = false
const title = (line.text.startsWith("[*** ") ? " " : "") + renderPlainText(line.nodes)
const onClick = () => location.hash = line.id
scrapbox.PageMenu('見出し').addItem({ title, image, onClick })
}
}
})
function renderPlainText(node) {
if (node instanceof Array) return node.map(node => renderPlainText(node)).join('')
if (typeof node === 'string') return node
switch (node.type) {
case 'icon':
case 'strong-icon':
return node.unit.page
}
return renderPlainText(node.children)
}
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
}