インデントが半角スペースに統一されていない時警告するUserScript
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.style = 'position:absolute;top:0;right:0;white-space:pre;background-color:yellow';
return div;
}
function showMixedIndent() {
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);
}
}
}
}
})();
内容
可視化されていない/統一されていないと何が気になるのか
文章を再利用する時にインデントがズレる
更新履歴
作成(2021/11/08)