importしたscrapbox pageを関連ページリストに表示する
from /nekobatoken/Importable Code Block#5ce54f9ac95d0f0000de3979
コードブロック記法内でimport構文 (ES6)を使って取り込んだscrapbox pageを関連ページリストに表示する
2020-11-02 10:56:02
import構文 (ES6)がないときはカードの見出しを表示しないようにした
便利そうなので導入してみるtakker.icon
/icons/done.icon適当にアレンジした
scrapboxのページカードを作成するscriptを使う
ページ読み込み時に表示する
再読込はしない
既知の問題
他のprojectに遷移したときに残ってしまう
(解決)UserScriptがresetされない問題
code:script.js
import {createPageCard} from '/api/code/takker/scrapboxのページカードを作成するscript/script.js';
const relatedPageList = document.getElementsByClassName('related-page-list clearfix')?.0;
const relationLabel = `
<li class="relation-label links">
<a>
<span class="title">Dependencies</span>
<span class="kamon kamon-link-on icon-lg"></span>
</a>
<span class="arrow"></span>
</li>`;
const newList = document.createElement('ul');
newList.classList.add('grid', 'ex-code-link-list');
newList.insertAdjacentHTML('beforeend', relationLabel);
relatedPageList?.appendChild(newList);
const codelines = window.scrapbox.Page.lines.filter(line => line.codeBlock);
const dependencies = codelines.reduce((acc, line) => {
const text = line.text.trim();
const matched = text.match(/^import\s(\'|\")(.+)(\'|\")/);
if (matched) {
acc.push(matched2)
}
return acc;
}, []);
//console.log(dependencies);
dependencies.forEach(async path => {
path = path.replace(/^\/api\/code\/(^\/+)\/(^\/+)\/.*$/, '$1/$2');
const page = await fetch(/api/pages/${path})
.then(res => res.json());
//console.log(page);
newList.insertAdjacentHTML('beforeend',createPageCard({
project: scrapbox.Project.name,
title: page.title,
description: page.descriptions.join('\n'),
imageUrl: page.image}));
});
if (newList.children.length < 2) newList.style.display='none';
#2020-11-02 10:56:33
#2020-10-26 06:43:49