Action

Goto Markdown Section

Posted by dchar, Last update almost 6 years ago

This action let’s you quickly jump to a Markdown heading.

Steps

  • script

    //
    // Goto markdown section
    //
    
    (() => {
        const getMarkdownHeaders = function (s) {
            let m = new Map();
            m.set("Start of Draft", 0);
    
            let pos = 0;
            for (let l of s.split('\n')) {
                let match = l.match(/\s*(#+.*)/);
                if (match) {
                    // make button label unique with trailing spaces
                    let h = match[1];
                    while (m.has(h)) {
                        h = h + ' ';
                    }
    
                    m.set(h, pos);
                }
                pos += l.length + 1;
            }
            m.set("End of Draft", pos);
            return m;
        }
    
        let p = Prompt.create();
        p.title = "Goto Section";
    
        let headers = getMarkdownHeaders(draft.content);
        headers.forEach((v,k,m) => p.addButton(k));
    
        if (p.show()) {
            editor.activate();
            editor.setSelectedRange(headers.get(p.buttonPressed), 0);
        }
    })();
    

Options

  • After Success Nothing
    Notification None
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.