Scrapboxのタイトルの下にページの見出しを作るUserScript
参考文献
yuiseki.iconyuiseki
test
code:script.js
(function () {
function getPlainText (node, opts) {
if (node instanceof Array) return node.map(node => getPlainText(node, opts)).join('');
if (typeof node === 'string') return node;
switch (node.type) {
case 'icon':
case 'strong-icon':
return opts.noIcon ? ' ' : node.unit.page;
}
return getPlainText(node.children, opts);
}
function getIconUrl (node) {
if (node instanceof Array) return node.map(getIconUrl).find(img => img);
if (/icon/.test(node.type)) {
return /api/pages/${node.unit.project||scrapbox.Project.name}/${node.unit.page}/icon;
}
return null;
};
const titleElm = document.querySelector('.line:nth-child(2)');
const tocElm = document.createElement('div');
document.querySelector('.lines').insertBefore(tocElm, titleElm)
for (let line of scrapbox.Page.lines) {
if (!line.section.start) continue;
if (line.section.number === 0) continue;
console.info(line);
const image = line.nodes && getIconUrl(line.nodes);
const noIcon = !!image;
const title = line.nodes ? getPlainText(line.nodes, {noIcon}) : line.text;
const onClick = () => location.hash = line.id;
const hElm = document.createElement('h4');
const aElm = document.createElement('a');
aElm.href = '#' + line.id;
if (image) {
const img = document.createElement('img');
img.src = image;
img.height = 18;
img.width = 18;
aElm.appendChild(img);
}
const text = document.createElement('span');
text.innerText = title;
aElm.appendChild(text);
hElm.appendChild(aElm);
tocElm.appendChild(hElm);
}
})()