append-page
code:scrapbox
code:script.js
import { initAppendPage } from '../append-page/script.js'
initAppendPage({
projects: [
'programming-notes',
'villagepump',
],
useSendingMark: true, // add "to <dst>" to the end of src page
useReferenceMark: false, // add "from <src>" to the end of dst page
});
code:script.js
export function initAppendPage({ projects, ...rest }) {
const title = 'Append page to ...';
scrapbox.PageMenu.addMenu({
title: title,
onClick: () => {
const menu = scrapbox.PageMenu(title);
menu.removeAllItems();
projects.map(async project => menu.addItem({
title: project,
image: (await (await fetch(/api/projects/${project})).json()).image,
onClick: () => append({ project, ...rest }),
}));
}
})
}
async function append({ project, useSendingMark, useReferenceMark }) {
const pageTitle = scrapbox.Page.title;
const pageTitleEnc = encodeURIComponent(scrapbox.Page.title);
let lines = scrapbox.Page.lines;
// 送り先ページへのリンクを含む行があれば消してから送る
const refMark = from [/${scrapbox.Project.name}/${pageTitle}];
const sendMark = to [/${project}/${pageTitle}];
const hasMark = lines.length - _lines.length > 0;
let body = _lines.slice(1).map(line => line.text).join('\n').trim() + '\n';
// 送り元ページへのリンクが必要ならつける
if (useReferenceMark) { body += '\n' + refMark + '\n' }
window.open(https://scrapbox.io/${project}/${pageTitleEnc}?body=${encodeURIComponent(body)});
// 送り先ページへのリンクが必要ならつける
if (useSendingMark && !hasMark) {
window.open(https://scrapbox.io/${scrapbox.Project.name}/${pageTitleEnc}?body=${encodeURIComponent(sendMark + '\n')}, "_self");
}
}