Action

List + Heading Converter

Posted by jochi, Last update over 1 year ago

Convert text in both directions between Markdown List and Markdown Heading formats.

Select text in any list format (unordered, ordered, or simple list) it will give you options to convert the items to markdown headings at your preferred level (H1–H6) and how many newlines to include between them.

Select markdown headings and it will offer to convert the text to any list format.

Set your preferred defaults for Heading Level, number of newlines between headings, and default list type.

Steps

  • script

    // Define the default settings
    let defaultHeadingLevel = 3; // Change this to your desired default level
    let defaultNewlines = 2; // Change this to your desired default newlines between headings
    let defaultListType = "Simple List"; // Change this to your desired default list type ("Unordered List", "Ordered List", "Simple List")
    
    // Get the current draft and the selected text
    let selectedText = editor.getSelectedText();
    
    // If no text is selected, alert the user and abort the script
    if (selectedText.length === 0) {
        alert("Please select text before running this script.");
        throw new Error("No text selected");
    }
    
    // Function to determine if the selected text is in list format
    const isList = text => {
      let regex = new RegExp("^(\\d+\\. |- |- \[ \] )");
      return regex.test(text.trim());
    }
    
    // Function to convert list to headings
    const listToHeadings = (text, headingLevel, newlines) => {
      let lines = text.split('\n');
      let headingPrefix = '#'.repeat(headingLevel) + ' ';
      let converted = lines.map(line => headingPrefix + line.replace(/^(\\d+\\. |- |- \[ \] )/, '') + '\n'.repeat(newlines + 1)).join('');
      return converted;
    }
    
    // Function to convert headings to list
    const headingsToList = (text, listType) => {
      let lines = text.split('\n');
      let index = 1;
      let converted = lines
        .filter(line => line.trim() !== '') // Remove empty lines
        .map(line => {
          let listPrefix;
          switch(listType) {
            case "Unordered List":
              listPrefix = "- ";
              break;
            case "Ordered List":
              listPrefix = `${index++}. `;
              break;
            case "Simple List":
              listPrefix = "- [ ] ";
              break;
          }
          return listPrefix + line.replace(/^#{1,6} /, ''); // Convert to list format
        })
        .join('\n');
      return converted;
    }
    
    let newText;
    // Determine if the selected text is in list format and convert accordingly
    if (isList(selectedText)) {
      // Prompt the user for the heading level and newlines
      let p = Prompt.create();
      p.title = "Convert List to Headings";
      p.message = "Select the markdown heading level to convert to and the number of newlines between headings.";
      p.addSelect("level", "Heading Level", ["H1", "H2", "H3", "H4", "H5", "H6"], ["H" + defaultHeadingLevel.toString()], false);
      p.addSelect("newlines", "Newlines Between Headings", ["1", "2", "3"], [defaultNewlines.toString()], false);
      p.addButton("OK");
      if (p.show()) {
        let headingLevel = parseInt(p.fieldValues["level"][0].substring(1)); // Get the first item of the array and remove the "H" prefix before parsing
        let newlines = parseInt(p.fieldValues["newlines"][0]);
        newText = listToHeadings(selectedText, headingLevel, newlines);
      }
    } else {
      // Prompt the user for the list type
      let p = Prompt.create();
      p.title = "Convert Headings to List";
      p.message = "Select the list type to convert to.";
      p.addSelect("listType", "List Type", ["Unordered List", "Ordered List", "Simple List"], [defaultListType], false);
      p.addButton("OK");
      if (p.show()) {
        let listType = p.fieldValues["listType"][0];
        newText = headingsToList(selectedText, listType);
      }
    }
    
    // Replace the selected text with the new text and select it
    editor.setSelectedText(newText);
    editor.setSelectedRange(editor.getSelectedRange()[0], newText.length);
    

Options

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