文頭を統一するUserScript
https://gyazo.com/382fbc864edeebf0b6279a0becdac4ac
code:script.js
const leadingSpaceCharacter = /^\s*/;
const toFullWidthSpace = / |\t/g; const toHead = /^(\s*)/;
const formatted = (text, type) => {
let isFormat = false;
let to;
let toCharacter;
let ordered = 0;
let orderedSection;
if (type === 'toSpace') {
to = toSpace;
toCharacter = ' ';
orderedSection = '';
} else if (type === 'toFullWidthSpace') {
to = toFullWidthSpace;
toCharacter = ' ';
orderedSection = '';
} else if (type === 'toTab') {
to = toTab;
toCharacter = ' ';
orderedSection = '';
} else if (type === 'quote') {
to = toHead;
toCharacter = '$1>';
orderedSection = '';
} else if (type === 'ordered') {
to = toHead;
toCharacter = $1#. ;
}
const formattedText = text.split('\n').map(line => {
if (line === '') return;
const regexp = line.match(leadingSpaceCharacter)0.match(to); // 順序付きリストのとき、リストを振るセクションの高さを取得しておく
if (orderedSection === undefined) orderedSection = regexp.input;
if (regexp === null) {
return line;
} else {
// 順序付きリストのときの準備
if (type === 'ordered') {
// セクションの高さが違う行はなにもしない
if (orderedSection !== regexp.input) return line;
ordered += 1;
toCharacter = $1${ordered}. ;
}
isFormat = true;
return line.replace(leadingSpaceCharacter, s => s.replace(to, toCharacter));
}
})
if (isFormat) {
return formattedText.join('\n');
}
// 全ての行が変更なしなら何も返さない
return;
};
scrapbox.PopupMenu.addButton({
onClick: (text) => formatted(text, 'quote')
});
scrapbox.PopupMenu.addButton({
onClick: (text) => formatted(text, 'ordered')
});
/*
scrapbox.PopupMenu.addButton({
onClick: (text) => formatted(text, 'toTab')
});
scrapbox.PopupMenu.addButton({
onClick: (text) => formatted(text, 'toFullWidthSpace')
});
*/
scrapbox.PopupMenu.addButton({
onClick: (text) => formatted(text, 'toSpace')
});
更新履歴
一行も変更がない場合は更新しないようにした(2021/11/09)
1.0.0 UserScript作成(2021/11/08)
toTabをコメントアウト(2022/02/22)
番号を振った後、行の挿入、追加、削除があった場合に一度番号をクリアして振りなおしたい
code:before
1. aaaa
2. bbbb
3. cccc
code:change
1. aaaa
dddd
3. cccc
2. bbbb
code:after
1. aaaa
2. dddd
3. cccc
4. bbbb
順序付きリスト以外もorderedSectionを初期化(2022/08/05)
全角は不要になった(2023/04/15)
Scrapbox上のテキストの文頭を全角にしたいわけじゃなかった
テキストをメール等にペーストする時、いい感じにインデントされてほしいだけだった