Obsidianでカーソルがある行に取り消し線をトグルするTemplater
code:sample.md
<%*
const editor = app.workspace.activeLeaf.view.editor;
const selection = editor.getSelection();
if (selection) {
// 【選択範囲がある場合】
// 既に取り消し線で囲まれているなら解除、そうでなければ付与(トグル)
if (selection.startsWith("~~") && selection.endsWith("~~")) {
editor.replaceSelection(selection.slice(2, -2));
} else {
editor.replaceSelection("~~" + selection + "~~");
}
} else {
// 【選択範囲がない場合】カーソル行を処理
const cursor = editor.getCursor();
const lineNr = cursor.line;
const lineText = editor.getLine(lineNr);
if (lineText.trim().length === 0) return;
// リスト記号やチェックボックスを分離する正規表現
const match = lineText.match(/^(\s*(-*+|\d+\.)\s+(?:\.\\s+)?|(\s*))(.*)/); if (match) {
let newLine;
if (content.startsWith("~~") && content.endsWith("~~")) {
newLine = prefix + content.slice(2, -2);
} else {
newLine = prefix + "~~" + content + "~~";
}
editor.setLine(lineNr, newLine);
}
}
%>