Action
Markdown List
Posted by agiletortoise,
Last update
10 months ago
UPDATES
10 months ago
Add configured value support to change list character
Toggle Markdown list for selected lines, maintaining indentation. By default, this action uses - (hyphen) as the list character.
The action can be configured to use * or + if you prefer.
Steps
-
configurationKey
name List character
key markupCharacter
-
script
// Toggle tasks marks on selected lines let listMark = "-" if (context.configuredValues["markupCharacter"]) { listMark = context.configuredValues["markupCharacter"] } // grab state let [lnStart, lnLen] = editor.getSelectedLineRange(); let lnText = editor.getTextInRange(lnStart, lnLen); let [selStart, selLen] = editor.getSelectedRange(); // just add mark if empty line if (lnText.length == 0 || lnText == "\n") { editor.setSelectedText(`${listMark} `); editor.setSelectedRange(selStart + listMark.length + 1, 0); } else { // create line array and tracking vars let lines = lnText.split('\n'); let startOffset = 0; let lengthAdjust = 0; let flTrailing = false; if (lines[lines.length - 1] == "") { lines.pop(); flTrailing = true; } let newLines = []; const re = /^(\s*)?([-\*] )?(.*)/; const containsRe = /^(\s?)([-\*] )/; // determine if we are removing or adding marks let flRemoving = true; for (let line of lines) { if (line.length > 0 && !line.match(containsRe)) { flRemoving = false; } } if (!flRemoving) { // add marks let isFirst = true; for (var line of lines) { const match = re.exec(line); if (match[2] || line.length == 0) { newLines.push(line); } else { let prefix = match[1]; let suffix = match[3]; if (!prefix) { prefix = ""; } if (!suffix) { suffix = ""; } newLines.push(`${prefix}${listMark} ${suffix}`); if (isFirst) { startOffset = listMark.length + 1; } else { lengthAdjust += (listMark.length + 1); } } isFirst = false; } } else { // remove marks let isFirst = true; for (let line of lines) { if (line.trim() == "") { newLines.push(line); continue; } const match = re.exec(line); let prefix = match[1]; let suffix = match[3]; let state = match[2]; if (!prefix) { prefix = ""; } if (!suffix) { suffix = ""; } if (suffix.startsWith(" ")) { suffix = suffix.substr(1); if (isFirst) { startOffset -= 1; } else { lengthAdjust -= 1; } } newLines.push(`${prefix}${suffix}`); if (isFirst) { startOffset -= state.length; } else { lengthAdjust -= state.length; } isFirst = false; } } // update text and selection if (flTrailing) { newLines.push(""); } let newText = newLines.join("\n"); editor.setTextInRange(lnStart, lnLen, newText); editor.setSelectedRange(selStart + startOffset, selLen + lengthAdjust); }
Options
-
After Success Nothing Notification Error Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.