Hi there,
I have the following thing I want to do with Drafts:
I have a list with 7 items (always 7), like this:
- Earth
- Europe
- Germany
- Stuttgart
- Mauerstraße
- 7
- Hinterhaus
Now I want to reverse the order of the items to
- Hinterhaus
- 7
- Mauerstraße
- Stuttgart
- Germany
- Europe
- Earth
And as a last step I would like to add specific text in front of each line, always the same text, so it looks like this:
- Etage: Hinterhaus
- Number: 7
- street: Mauerstraße
- city: Stuttgart
- country: Germany
- continent: Europe
- planet: Earth
How can I achieve this (as you can see I have no idea how to use JavaScript for this…)
Thanks in advance…
Here you go, this should work in a script step:
let prefixes = ["Etage", "Number", "street", "city", "country", "continent", "planet"]
let output = "- " + editor.getSelectedText().substr(2).split("\n- ").reverse().map((item, index) => prefixes[index] + ": " + item).join("\n- ")
editor.setSelectedText(output)
Note: you have to select the text first. That’s easy to change if that’s not what you wanted.
1 Like
Thank you so much, works!
How can I change it so it just takes all the text in the current draft?
Try this:
let prefixes = ["Etage", "Number", "street", "city", "country", "continent", "planet"]
draft.content = "- " + draft.content.substr(2).split("\n- ").reverse().map((item, index) => prefixes[index] + ": " + item).join("\n- ")
1 Like
This is fabulous, thank you. How can I add text before the sorted text, something like:
## this is the sorted list:
?
Like so:
let prefixes = ["Etage", "Number", "street", "city", "country", "continent", "planet"]
draft.content = "## this is the sorted list:\n- " + draft.content.substr(2).split("\n- ").reverse().map((item, index) => prefixes[index] + ": " + item).join("\n- ")
1 Like
Awesome, thank you. Looks complicated…
It’s not actually as bad as it looks, although doing all the work in one line doesn’t help! I could probably write a much clearer version in 10 lines.
Here’s a breakdown if you’re interested in picking up some JavaScript:
-
draft.content
is all the text in the draft.
-
substr(2)
removes the first two characters of the first line: the hyphen and the space, which means that the text is now just a list of items separated by a newline then a hyphen then a space.
-
split("\n- ")
splits the text up using this separator (\n
is the newline character) to give a list of all the items.
-
reverse()
reverses this list.
-
map()
is a method that allows you to do something to all the items in a list, in this case add the corresponding item (prefix[index]
) from prefixes
plus a colon and a space.
-
join()
is like the opposite of split()
- Then finally you add on the text at the beginning, including a newline and hyphen and a space (which were previously removed from the first item).
1 Like
Glad you showed reverse()
becuse this isn’t actual sorting. Perhaps the title of this thread should be edited to say “Reverse” instead of “Sort”.
(While I probably have title editing privileges I’m loth to use them.)