Extract title from the first H1 line

I write Markdown notes often starting with something like this

Base Header Level:  2
CSS: /_resources/default.css

# Note about finding the first H1 line
.
.
.

When I want to send this Draft to Devonthink or iA Writer or any other Notetaking app I need to specify a title for the note.

As title I would like to use just the text in the first H1 line (here “Note about finding the first H1 line”), but what is the easiest way to extract it from the Drafts object?

Is the format always the same?
So is your H1 header always in line 4?
Then you can use the drafts templates to get the content of line 4

Or otherwise you can create a script that searched through the lines of the current draft and return the line with the H1 title.

1 Like

Drafts tends to think about the first line as the title. While you could extract any line and use it, I would tend to suggest it would be best to just use the title in the first line in Drafts and let your action re-organize the text. For example, write like:

# Note about finding the first H1 line
.Base Header Level:  2
CSS: /_resources/default.css
.
.

Then, when outputting in an action template, you [[display_title]] to get the title and [[body]] for the remaining text to send off to another app.

1 Like

Thanks for your reply! :smile:

No, not always on teh 4th line. It’s just the first H1 line.

I was hoping Drafts as an AST-like representation of its Markdown text - after all it can preview it perfectly…

Interesting idea to turn things around! Thanks for this.

Ok, I put the title text on the first line, but the destination application needs the title on the H1 line as well.

Can Drafts insert the title in the first H1 line on export, say, when using the “_ Send to DEVONthink_” action? Just a thought…

For example in Drafts I write

Note about finding the first H1 line
Base Header Level:  2
CSS: /_resources/default.css

# 

More text...

and when Drafts sends it to DEVONthink, that app receives as note body

Base Header Level:  2
CSS: /_resources/default.css

# Note about finding the first H1 line

More text...

Just another idea…
do you alwys use the same settings like in your examples like:

for the drafts you want to send to DEVONthink?
or do you have “set” of CSS / header levels that you use?

If you don’t need the metadata in the Draft, then you can write the draft without these lines in the first place. then use the standard template tags like agiletortoise suggested and just add those lines below the title.

Example draft:

# title
. some
. notes
. here

the script could do the following steps:

  1. get the relevant templates (e.g. [[display_title]] and [[body]]
    [optional] display a prompt to select the metadata (base header level and CSS)
  2. modify the body of the draft to
.Base Header Level:  [selected level]
CSS: [selected css file]
. some
. notes
. here
  1. update the draft / body content
  2. use the url scheme of DEVONthink to pass the text over

Well, modifiying the body is what I would like to do, if I follow @agiletortoise’s idea:

In the first line I have the title, so the action than needs to find the first H1 line (which can be on an unknown line) and add there the text from the title. How can I modify the body like this? (I’m a bit of a newbie in tehse regards…)