Is it possible to create a header extraction (=annotations summary) action?

Hello,

I have come to use markdown headers (#) to make annotations when I review documents converted to drafts.

Headers are convenient as annotations because the red font is easy to see when I scroll down drafts.

I would like to create an action which would basically extract all the header lines from the draft and copy them into a new draft, creating the equivalent of an annotations summary which you commonly find with PDF files.

Intuitively, I wonder if some kind of regex formula could do the job, but there are probably many options.

I think that an annotations summary (header extraction) could potentially appeal to a wide user base.

thank you for your time and help

See if you can use the solutions you’ve had previously that create new drafts and this line of script to create your own solution.

alert(draft.content.replace(/\n^[^#.*].*/mg, ""));

Assuming my regular expression works correctly for your needs of course :laughing:

1 Like

thanks very much !
I will test it and give you feedback

I took the duplicate draft action and inserted your line in the script.

result: the draft is duplicated but the content is unchanged.
Did I perhaps insert your line at the wrong place?
Please note that I also tried to insert it at the very end, and it also did not work.
thank you

// See online documentation for examples
// http://getdrafts.com/scripting

const newDraft = Draft.create()
if (draft.selectionLength == 0) {
newDraft.content = draft.content
} else {
newDraft.content = editor.getSelectedText()
alert(draft.content.replace(/\n^[^#.]./mg, “”));
}

newDraft.update()

editor.load(newDraft)
editor.focus()

That line on its own should give you a pop up alert containing your headers. Instead of “alerting” it, you could set the content of your new draft to that.

Like this…

let newDraft = Draft.create();
newDraft.content = draft.content.replace(/\n^[^#.].*/mg, "");
newDraft.update();
editor.load(newDraft);
editor.focus();

From your code I can’t discern the intent you have around working with selections, but hopefully the above will get you a step closer

1 Like

thank you very much for your time

I am sorry: it does not work.
I think that the problem is that I started with just any create new draft action from the action directory, not understanding what action I should start with to which I would add your line of code.
With the above script (converted to an action) I simply end up with one long string of everything contained in the draft.
I would like to restart from the beginning: what do you mean by use the solutions you’ve had previously that create new drafts
thank you

From this I understood that you wanted an action that would take the content of the current draft, remove all of the non-header lines to just leave only the header lines, and then place those header lines into a new draft.

I spotted a typo in the second script above. An asterisk was missing in the regular expression when I compared it back to the original one-liner I posted. It turns out that it’s omission was down to me using your script as the basis for just showing how it would quickly fit together. It turns out you’d introduced the typo in your script and so it followed through to mine. I’ve now amended my version of the script above to incude the missing asterisk and then based the remainder of this post on that corrected version except where specified to the contrary.

This is the script copied into an action.

If I take a draft that looks like this.

Then run the action against it, I get a new draft that looks like this.

Which matches to my own expectation of the desired result. It’s by no means a rigorous set of testing, but I have now tried this on a few Markdown draft variations and it did look as I’d expected in each case.

In the other solutions I’ve posted to you recently in this forum, there were examples of putting content into a new draft. It is to those previous examples I was referring; but I would expect the code to essentially be the same as any you might derive it from in the Action Directory. But do remember that all of the scripting for Drafts objects, functions, etc. is documented on the Drafts web site and is the primary reference source.

That isn’t quite what I got when I ran the unedited script. It gave me the header lines and partials from the other lines, but there was definitely a clear set of changes to the content of the original draft - it didn’t pass in the content as is.

Using the corrected regular expression, I get the results as shown above, which as I’ve noted, are what I’d expect given my understanding of the request and my approach to applying it.

Is that what you were after?

1 Like

Thanks very much for your detailed reply. I feel bad about taking so much of your time. Sorry for not being clear in my request.
The end result of the desired action as depicted in your images corresponds exactly to what I want.
However your action which I downloaded and installed yield concatenated text without spaces including headers and not headers add per images below. I deleted and reinstalled the action 3 times to make sure it was not an installation problem

Thanks again

I took your example text, and whilst the result wasn’t a good one, I didn’t actually get an identical result to what you had. I’ve had a go at reworking the regular expressions a little to try and deal with the example you had and some additional thoughts I had … part of my original regular expression was being a little too aggressive on matching for what’s required.

I used this draft as my starting point.

Original Draft (Raw Text)

The following was copied directly out of Drafts.

# (1) title treat as annotation 

This is a test 

# (2) making a first annotation 

This is more text which is not an annotation

# (3) another annotation 
more text

some text with a # in it but not at the start
followed by another none annotation

now an annotation  with text immediately above
# (4) Here's that annotation

and how about an
# (5) annotation with
text above and below?

# (6) Two annotations
## (7) lines in a row

Last piece of text

Each annotation/header line I put a number marker on just so that I could check that only those lines were to come out and that they all came out.

When I run the draft action above against this gives me the following output as a new draft.

image

New 'Summary' Draft (Raw Text)

The following was copied directly out of Drafts.

# (1) title treat as annotation 
# (2) making a first annotation 
# (3) another annotation 
# (4) Here's that annotation
# (5) annotation with
# (6) Two annotations
## (7) lines in a row

So for me, for this test case, everything looks to work correctly, and only output the headers.

Hopefully this will work for you.

If it doesn’t, then I would suggest an alternative scripted approach as follows:

  1. Create our new draft.
  2. Take the content of the current draft.
  3. Split the draft by line into an array of lines.
  4. Loop over every line. If the first character is a “#”, append it to the content of the new draft. Don’t forget to add a new line back on the end each time.
  5. When you have looped over all of the lines in the draft, update and load the new draft.

Hope that helps.

1 Like

I’ve been following this thread as I thought it was a useful addition to the toolkit. The most recent action seems to work, another good job. Grateful for all the time and effort you take to support those less knowledgeable.

2 Likes

For another approach, you could modify this action:

https://actions.getdrafts.com/a/1Fx

2 Likes

It now works very well. I can‘t thank you enough for all your time.
I want to now make an effort to get into scripting.
If you knew of a basic introduction, I would be most grateful.
I don‘t want to have to ask every time I want to make the slightest modification.
For example, I now want to -

  • strip the summary of the markdown formatting (take out the # in the summary part)
  • prepend the summary to the existing draft, because I realize that it is too confusing to have so many drafts
    I have your draft as a starting point.
    Once again, mucho mucho thanks
    have a nice sunday !

I have no scripting skills which would allow me to convert to a summary, but have another use for the action: it is extremely interesting because it emulates a table of contents !

Discovering this action is a huge find for me to help me navigate long documents.

In my workflow, I ‚hijacked‘ headers which I use for annotation summaries because they are easy to identify in red when I scroll down the document (I don‘t like bold).

How that my made me discover this table of contents action, I realize that I now really need to use headers.

I tried to change the markdown format to multimarkdown and use a double ## for headers, but ## simply displays as normal unformatted text. I searched the internet and found a syntax for blue which does not work .

Would you have any idea what I could use for headers, exclusing #, bold or italics ?

thank you

  • For JavaScript as a language try https://eloquentjavascript.net
  • For Drafts extensions, the Drafts documentation on the web site.
  • For ideas, this forum and the action directory.
  • Take a look at the JavaScript replace function used I this script and learn the basic of regular expressions. That’ll be enough for this particular one.
  • Any Google search for regular expressions would suffice to get you started.
  • Instead of using a new draft, what if you referenced the current draft and said its content should be equal to some other variable you built up, plus it’s content…?

There’s lots of information online about Markdown and Multimarkdown. How they are displayed in Drafts is just a visual aid. It is when they are converted to a rich text format such as HTML and then viewed Ina browser, that real format control can occur.

Note that different levels of heading are denoted by different numbers of sequential hashes at the start of a line. The colour is just a way to visually distinguish in Drafts.

You appear to be making up your own syntax for something so the choice is yours in that sense, but if you really do want to make use of Multimarkdown tables of contents, then you must use headers as headers.

1 Like

thanks very much.
I installed Eloquent JavaScript on my kindle and will start reading today. Greatly appreciated.
I understand that Markdown as displayed in drafts is just a visual aid, but that’s how I review documents. I am trying to develop an innovative way to review documents using drafts because most alternative methods are terrible.
Example: take a PDF, annotate and save the annotations. From what I have seen, you end up with the annotations plus a lot of garbage (Page 7 highlight yellow etc) which makes those annotations too painful to read and impossible to use.
Visually in drafts, if ## looks like #, the ## is of no use to me, because I work in Drafts. I don’t want to start converting to RTF.
Therefore visually in drafts, as far as I understand, apart from unformatted text, I am limited to 3 different visual displays (which stand out when I scroll down a long document): header, bold and italics. That also means that there are only 3 types of annotations that I could potentially extract using a regular expression.
That was the point of my question.
thanks very much