Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GitMurf/cffd1446cbd693f6b145e77388163754 to your computer and use it in GitHub Desktop.
Save GitMurf/cffd1446cbd693f6b145e77388163754 to your computer and use it in GitHub Desktop.

Revisions

  1. GitMurf revised this gist Aug 12, 2022. No changes.
  2. GitMurf created this gist Aug 12, 2022.
    73 changes: 73 additions & 0 deletions obsidian.templater.operation.outliner.select-list-items.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <%*
    const editor = app.workspace.activeLeaf.view.editor;
    const getCurSelection = editor.getSelection();
    const getCur = editor.getCursor("from");
    const curLineNo = getCur.line;
    const curLineText = editor.getLine(curLineNo);
    const matchBullet = curLineText.match(/^[ \t]*[-\*] (.*)/);
    let foundContent = "";
    if(matchBullet) {
    foundContent = matchBullet[1];
    }
    if(!foundContent) {
    // Not a list item, so just select the entire line
    const multiSelect = editor.listSelections();
    const startLine = editor.getCursor("from").line;
    const endLine = editor.getCursor("to").line;
    let compareSelection = curLineText;
    if(startLine !== endLine) {
    // already multiple lines selected
    compareSelection = editor.getRange({ line: startLine, ch: 0 }, { line: endLine, ch: editor.getLine(endLine).length })
    }
    if(getCurSelection === compareSelection) {
    // if line is already selected then extend selection to the line below
    editor.setSelection({ line: startLine, ch: 0 }, { line: endLine + 1, ch: editor.getLine(endLine + 1).length });
    } else {
    const matchBullet = curLineText.match(/^[ \t]*(.*)/);
    if(matchBullet) {
    // this is to account for if in code files like templater select the line content without the prepended indented spaces
    const foundPosStart = curLineText.indexOf(matchBullet[1]);
    const selectedWithoutSpace = editor.getRange({ line: curLineNo, ch: foundPosStart }, { line: curLineNo, ch: curLineText.length });
    if(getCurSelection === selectedWithoutSpace) {
    editor.setSelection({ line: curLineNo, ch: 0 }, { line: curLineNo, ch: curLineText.length });
    } else {
    editor.setSelection({ line: curLineNo, ch: foundPosStart }, { line: curLineNo, ch: curLineText.length });
    }
    }
    }
    return;
    }
    const secondSelection = getCurSelection === foundContent ? true : false;

    switch(secondSelection) {
    case false:
    const foundPosition = curLineText.indexOf(foundContent);
    editor.setSelection({ line: curLineNo, ch: foundPosition }, { line: curLineNo, ch: curLineText.length });
    break;
    case true:
    // list item already selected so now select all children
    const activeFile = app.workspace.getActiveFile();
    if(!activeFile) { return; }
    const mdCache = app.metadataCache.getFileCache(activeFile);
    if(!mdCache) { return; }
    const mdCacheListItems = mdCache.listItems;
    if(!mdCacheListItems) { return; }
    let foundParentLine = false;
    let foundLastChild = false;
    let foundLastChildLine = curLineNo;
    mdCacheListItems.forEach(eachLI => {
    if(foundParentLine === true && foundLastChild === false) {
    if(eachLI.parent >= curLineNo) {
    foundLastChildLine = eachLI.position.start.line;
    } else {
    foundLastChild = true;
    }
    }
    if(eachLI.position.start.line === curLineNo) { foundParentLine = true; }
    });
    editor.setSelection({ line: curLineNo, ch: 0 }, { line: foundLastChildLine, ch: editor.getLine(foundLastChildLine).length });
    break;
    }

    return;
    %>