Markdown Header (#)

Posted by agiletortoise, Last update 25 days ago

Increase Markdown header level of current line by adding a “#” at the beginning. If the line is currently at the maximum 6 levels of heading, remove the leading header characters.

Steps

  • script

    // Increase Markdown header level current line
    
    const [lnSt, lnLen] = editor.getSelectedLineRange();
    let ln = editor.getTextInRange(lnSt, lnLen);
    const [selSt, selLen] = editor.getSelectedRange();
    let prefix = "#";
    
    if (ln.startsWith("######")) {
    	// max header level, clear header status
    	let adj = 6;
    	ln = ln.substring(6);
    	while (ln.startsWith(" ")) {
    		ln = ln.substring(1);
    		adj++;
    	}
    	editor.setTextInRange(lnSt, lnLen, ln);
    	editor.setSelectedRange(selSt - adj, selLen);
    }
    else {
    	// increase level
    	if (ln.length == 0 || (ln && ln[0] != " " && ln[0] != "#")) {
      		prefix = "# ";
    	}
    	editor.setTextInRange(lnSt, 0, prefix);
    	editor.setSelectedRange(selSt + prefix.length, selLen);
    }
    
    

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.