diff --git a/list-sort.ais b/list-sort.ais new file mode 100644 index 0000000..710a11b --- /dev/null +++ b/list-sort.ais @@ -0,0 +1,39 @@ +/// @ 0.19.0 +### { + name: "List Sort" + version: "1.0.0" + author: "@bunnybeam@transfem.social" + description: "Adds a post form action that sorts lists in a note." + config: { + prefix: { + type: "string" + label: "Prefix" + description: "The prefix used to denote lines in a list. Beware of trailing whitespace!" + default: "- " + } + } +} + +Plugin:register_post_form_action("Sort lists", @(note, rewrite) { + let prefix = Plugin:config.prefix + let lines = note.text.split(Str:lf) + var lines_out = [] + var sort_buffer = [] + each let line, lines { + if line.starts_with(prefix) { + sort_buffer.push(line) + } else { + sort_buffer.sort(lt_lower) + lines_out = lines_out.concat(sort_buffer) + sort_buffer = [] + lines_out.push(line) + } + } + sort_buffer.sort(lt_lower) + lines_out = lines_out.concat(sort_buffer) + rewrite("text", lines_out.join(Str:lf)) +}) + +@lt_lower(a, b) { + return Str:lt(a.lower(), b.lower()) +}