インデントが半角スペースに統一されていない時警告するUserScript
from 読み込むUserScripts
https://gyazo.com/fbef7f915174b5a8245a016c6f70b22f
code:script.js
(() => {
const allWhitespace = /^\s*/;
const toWarnWhitespace = / |\t/g;
const errorDoms = new Map();
scrapbox.on('lines:changed', showMixedIndent);
showMixedIndent();
// 文頭から始まるWhitespaceの連続に全角スペースとタブが混ざっている場合trueを返す
function isMixedIndent(text) {
if (text.match(allWhitespace)0.match(toWarnWhitespace) === null) return false;
return true;
}
// 半角スペース、全角スペース、タブを置換して可視化して返す
function replaceWhitespaceToVisibleCharacter(text) {
return text.replace(/ /g, '_').replace(/\t/g, '^').replace(/ /g, '□');
}
function createErrorDom () {
const div = document.createElement('div');
div.classList.add('mixed-indent');
div.style = 'position:absolute;top:0;right:0;white-space:pre;background-color:yellow';
return div;
}
function showMixedIndent() {
if (window.scrapbox.Project.name.startsWith('gosyujin')) return;
if (!window.scrapbox.Page.lines) return;
for (const line of scrapbox.Page.lines) {
if(isMixedIndent(line.text)) {
if (!errorDoms.has(line.id)) {
errorDoms.set(line.id, createErrorDom());
document.querySelector(#L${line.id}).appendChild(errorDoms.get(line.id));
}
errorDoms.get(line.id).innerText =
${replaceWhitespaceToVisibleCharacter(line.text.match(allWhitespace)[0])};
} else {
if (errorDoms.has(line.id)) {
document.querySelector(#L${line.id}).removeChild(errorDoms.get(line.id))
errorDoms.delete(line.id);
}
}
}
}
})();
内容
半角スペース、全角スペース、タブを可視化するUserScript Event(2021/11/08)
可視化されていない/統一されていないと何が気になるのか
文章を再利用する時にインデントがズレる
Scrapboxで文頭のタブと空白が同じ扱いなのが気になる
Scrapboxのインデントはタブよりも半角スペースを使う方がよさそう
文頭を統一するUserScriptと合わせて使う
コードブロックの空白を可視化するUserScriptで直接見られるようにした
更新履歴
作成(2021/11/08)
他人のプロジェクトは可視化されてもどうすることもできないし、文章が見えなくなるだけなので条件判定で弾くようにした(2026/04/13)