番号を振るPopupMenu
選択した部分に番号を振るPopupMenu
基準となるインデントのみにつけて、逆操作もできるようにする
1. ①
2. ②
読み飛ばされる
3. ③
一旦消す処理はなくてもよさそう?あんも.icon
振り直し機能が必要かどうか
code:script.js
scrapbox.PopupMenu.addButton({
title: "#",
onClick: (text) => {
const lines = text.split("\n");
const baseIndent = lines0.match(/^\s*/)0;
// 基準インデントの行だけ取得
const baseLines = lines.filter(
line => line.match(/^\s*/)0 === baseIndent
);
// 基準インデントの行がすべて番号付きなら除去モード
const removeMode = baseLines.every(
line => /^\s*\d+\.\s/.test(line)
);
let count = 0;
return lines.map(line => {
const indent = line.match(/^\s*/)0;
// 子要素はそのまま
if (indent !== baseIndent) {
return line;
}
if (removeMode) {
// 番号を除去
return line.replace(/^(\s*)\d+\.\s/, "$1");
} else {
// 既存番号があれば一旦消す
const body = line.replace(/^(\s*)\d+\.\s/, "$1");
count++;
return body.replace(/^(\s*)/, $1${count}. );
}
}).join("\n");
},
});
基準となるインデントのみにつける
行頭まで囲むような操作を想定しておくあんも.icon
1. ①
2. ②
読み飛ばされる
3. ③
code:script.jsold
scrapbox.PopupMenu.addButton({
title: "#",
onClick: (text) => {
const lines = text.split("\n");
const baseIndent = lines0.match(/^\s*/)0; // 行頭の空白を取り出してインデントの基準にする
let count = 0;
return lines
.map((line) => {
const indent = line.match(/^\s*/)0;
// 基準インデントだけ番号を付ける
if (indent === baseIndent) {
count++;
return line.replace(/^(\s*)/, $1${count}. );
}
// より深いインデントはそのまま
return line;
})
.join("\n");
},
});
シンプルバージョン
1. ①
2. ②
3. ③
code:script.jsold
scrapbox.PopupMenu.addButton({
title: "#",
onClick: text => text
.split("\n")
.map((line, index) => line.replace(/^(\s*)/, $1${index + 1}. ))
.join("\n")
});
空行を勢い余って選択するとうまくいかないあんも.icon
空行を確認するのに使える?
String.prototype.trim() - JavaScript | MDN
line.trim() === ""
インデント部分を別カウント
1. ①
2. ②
1. ②1
2. ②2
3. ③
code:script.js.old
scrapbox.PopupMenu.addButton({
title: "#",
onClick: (text) => {
let indentLevels = {};
return text.split("\n")
.map((line) => {
let indent = line.match(/^\s*/)0;
indentLevelsindent = (indentLevelsindent || 0) + 1;
return line.replace(/^(\s*)/, $1${indentLevels[indent]}. );
})
.join("\n");
},
});
/customize/箇条書きに番号を付けるUserScript