文頭を統一するUserScript
from 読み込むUserScripts
https://gyazo.com/382fbc864edeebf0b6279a0becdac4ac
code:script.js
const leadingSpaceCharacter = /^\s*/;
const toSpace = / |\t/g;
const toTab = / | /g;
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({
title: '⚒ > ',
onClick: (text) => formatted(text, 'quote')
});
scrapbox.PopupMenu.addButton({
title: '⚒ N. ',
onClick: (text) => formatted(text, 'ordered')
});
/*
scrapbox.PopupMenu.addButton({
title: '⚒ ^ ',
onClick: (text) => formatted(text, 'toTab')
});
scrapbox.PopupMenu.addButton({
title: '⚒ □ ',
onClick: (text) => formatted(text, 'toFullWidthSpace')
});
scrapbox.PopupMenu.addButton({
title: '⚒ • ',
onClick: (text) => formatted(text, 'toSpace')
});
*/
更新履歴
一行も変更がない場合は更新しないようにした(2021/11/09)
1.0.0 UserScript作成(2021/11/08)
Scrapboxのインデントはタブよりも半角スペースを使う方がよさそう
インデントが半角スペースに統一されていない時警告するUserScript
名称を文頭を統一するUserScriptに変更(2021/12/16)
toTabをコメントアウト(2022/02/22)
順序付きリストを追加(2022/08/01)
番号を振った後、行の挿入、追加、削除があった場合に一度番号をクリアして振りなおしたい
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上のテキストの文頭を全角にしたいわけじゃなかった
テキストをメール等にペーストする時、いい感じにインデントされてほしいだけだった
Scrapbox APIにリンクからアクセスするUserScriptでtxt取得して、文頭整形して、window.openするだけで良いと判断
使ってないので⚒をコメントアウトした(2026/05/14)