Action

Jump/Goto Section

Posted by @jsamlarose, Last update almost 2 years ago

UPDATES

almost 2 years ago

Now offers the ability to set jump destination as the beginning or end of a section (defaults to end).

Another take on an action to aid navigation within a draft. This allows for jumping to the beginning or end of a section in a markdown document (where a section = a header + any text under that header).

Steps

  • script

    function escapeRegExp(string) {
        return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
    }
    
    const array = draft.lines
    const headers = array.filter(value => /^#+/.test(value));
    
    let p = Prompt.create();
        p.title = "go to";
    
        for (var header of headers) {
            p.addButton(header);
        }       
        p.addSegmentedControl("pos", "", ["beginning","end"], "end")
        p.isCancellable = true
    
        if (p.show()) {
            selHeader = p.buttonPressed
            sel = editor.getSelectedLineRange()
            editor.setSelectedRange(sel[0],sel[1])
            selText =  editor.getSelectedText()
    
            regex = new RegExp(escapeRegExp(p.buttonPressed) + "\n(.|[\r\n\t\f\v ])*?\n\n","g")
            // regex for header and text (allows to locate the end of a block)
            	// I should be able to use \s, but for some reason the slash was being ignored... 
    
    		block = editor.getText().match(regex)[0].replace(/\n+$/, "")
    
    		if (p.fieldValues["pos"] == "end") { 
    			editor.setSelectedRange(editor.getText().indexOf(block)+block.length,0)	
    		} else {	
    			editor.setSelectedRange(editor.getText().indexOf(selHeader)+selHeader.length,0)	
    		}
    		editor.activate()		
    					    	        
           }
    

Options

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