切り出しを先頭へ
#UserScript
「New page(切り出し)」を末尾 append ではなく、対象ページのタイトル直下=先頭に書き込む版に置換する。
① ネイティブ New page ボタン(.new-page-button)を CSS で隠す(semantic class 狙い撃ち=順序非依存・自作ボタンには当たらない)
② "先頭に切り出し" ラベルの自作ボタンを追加。唯一の任意位置 write プリミティブ patch(@cosense/std=cosense-ws-bundle)でタイトル(行0)直下へ insert
canonical = このページ。script_allから import。
code:script.js
import { patch } from '/api/code/tkgshn-extension/cosense-ws-bundle/script.js';
// ① ネイティブ「New page」ボタンを隠す(?body= 由来で末尾 append 固定なので置換する)。
// .new-page-button = 安定 semantic class 狙い撃ち=nth/順序非依存。addButton 製の自作 .button には当たらない。
const css = '.popup-menu .button-container .button.new-page-button{display:none!important}';
document.head.appendChild(Object.assign(document.createElement('style'), { textContent: css }));
// ② 置換ボタン "先頭に切り出し":選択を対象ページの「タイトル直下=先頭」へ切り出す。
// 先頭行→タイトル(ネイティブ準拠で先頭 # と外側 .. を剥がす), 残り→本文。
const toTitle = (line) => line.replace(/^\s*#/, '').replace(/^\(.+)\$/, '$1').trim();
const textOf = (l) => (typeof l === 'string' ? l : l.text);
// 行0(タイトル)を温存し、その直下に body を差し込む不変変換 f(lines) -> lines'。
const prependUnderTitle = (body) => (lines) => [
...(lines.length ? [textOf(lines0)] : []),
...body,
...lines.slice(1).map(textOf),
];
scrapbox.PopupMenu.addButton({
title: (text) => (text.includes('\n') ? '先頭に切り出し' : undefined),
onClick: (text) => {
const lines = text.split('\n');
const title = toTitle(lines0);
const body = lines.slice(1);
const _ = title &&
patch(scrapbox.Project.name, title, prependUnderTitle(body))
.catch((e) => console.error('new-page-top patch error', e));
return title ? [${title}] : undefined;
},
});
console.log('new-page-top native New page hidden; prepend button active');