Find markdown header for a given location in a draft

Anyone— what’s the most efficient way to return the closest preceding header for any given location within a draft?

I’ve tried experimenting with regex lookarounds, but haven’t been successful thus far…

Assuming a flavour of Markdown, how about utilising navigation markers?

Good to be pointed in this direction. I haven’t played around with any of these navigation marker methods yet.

In my use case, I’m working with an array of drafts from a query… hence the initial assumption that a regex might be the way forward…

Regex then, or if you can’t get that to work, split the draft content at each heading and get the text lengths of the resulting array elements to figure out where each sections limits are to locate your position and the name of the heading it is in.

1 Like

Hm. That second suggestion is interesting. Will investigate. Thanks!

Solution I came up with:

  • for each draft returned by the query, generate an array of header index values (test the drafts.lines array for lines that match /^#+ /)
  • then, for a given search string:
    • find draft.line[n] containing search string
    • return index of preceding header (next smallest value in array of headers)

I’m using this to add a little more information to the block level filter action I put together— for each block that the filter returns, I can now also display the markdown header it sits under (though I’m suppressing this in drafts with one header or less to keep the noise down). So it’s a little more complicated than the summary above, but this gives a general sense of the methodology.

When I first came up with the action, I was maintaining daily journals with timestamped log lines, but I’ve since relaxed on that front, and I’m starting to make much more use of (GitHub flavour) Markdown headers in my notes and logs, “block-logging” at few points during the day, so multiple lines/paragraphs per timestamped heading. This allows for that shift.

Oo, will you be sharing that update? :relaxed:.

It probably won’t do exactly the same, but I had to do something similar with headers for my workout log. The idea was to see how I improved, or not, doing the workout multiple times.

Happy to! Actually, I might just share a version here first— it’d be good to know that I haven’t inadvertently broken anything!

Beyond the update for headers, this unlisted version might be a little different from the one I last posted; I’ve been making tweaks for my own use and haven’t been great at logging the differences… let me know how it works for you.

Also, I’d be curious to hear more about what you’ve done re: those workout logs. I also log workouts in Drafts, and I use Charty for a bit of basic visualisation/motivation, but I’m sure there’s more I could be doing with that…

Cool, thanks, I’ll give that version a look and let you know.

As for the workout logs, I doubt what I do would be helpful, :relaxed:. Remember, I’m totally blind. Charts and graphs etc are really unhelpful for me, so I’ve found tracking stats in a way I’d find useful really tricky.

Anyway, I’m using a kind of hybrid journal system. I do use daily notes, but they’re for longform journaling, and that’s where the workout log goes. Mostly, it’s just a Level 2 header called workout, where I talk about how I felt during the workout, did I sub any movements, did I complete the whole thing, etc etc. In other words, it’s mostly prose. Then I just put a wiki link to the workout I did at the bottom of that.

The other part is my bullet journal type system. The main part of that is a file for each week that I pretty much live in and a monthly file for each month.

So in the weekly file under a workouts header is my workout plan for the week as tasks, with links to each workout. so when I do them, I check them off and write the log, which gets appended to my daily note.

The only other thing I do is, in the monthly file under the week, I say how many workouts I did. So if I planned 5, and I only did 4, I’ll put 4/5 under that.

So, obviously, my workout note merge thing had to find every link to a particular workout in my journal, look backwards for a workout header and then grab just that bit of Draft cutting off everything after that workout entry, since obviously there’s often other stuff in the daily note. Then it just appends that to a variable that gets sent to preview. Oh, I also make sure the header has a link to the daily note the log is in, in case I need. to jump back there for some reason.

I actually can’t even remember how I did it, it was a heck of a long time ago, but I know it was ugly, :relaxed:.

now that was more than you wanted to know, wasn’t it? :smirk:

Nope! I have a healthy respect for Drafts’ limitations, but by the same token, I think there’s still more I can do to refine the way I use it. Even when there’s nothing specific for me to adopt/adapt/repurpose, other people’s workflows usually get me thinking about what else is possible.

Charts/graphs: fair point! I’m already pretty happy with what I have on that front, though. I remain interested in determining whether there’s anything more I can do (beyond fancy visual things) to generate meaningful insights beyond graphing.

(At this point, I acknowledge that we’re going way beyond the topic of the original post, but as the original poster, it’s fine by me!)

It sounds like you’re observing planned vs actual workshops on a month by month basis, with longform reflection on the quality/impact of each workout? In my set-up, I have a daily log and a separate daily “bodywork” draft to log exercise. I used to push the exercise log to a Numbers sheet, but that hasn’t proven to be a sustainable way of working.

I’m still figuring this out, but there’s something about keyword concurrence/correlation that I’m chasing. This thinking (among other things) was a driver for me to make inline tags available in Drafts, and it’s part of the reason the block filter shows the tags associated with each block it returns (though that could be refined further). So, in theory, I could have a line in my log that’s something like:

- 20m run #hard

If I have a series of 20m runs spread through a set of logs, running a block level filter for “20m run” should allow me to retrieve whatever inline metadata I store in tags, and get a sense of the evolution of my relationship with 20m runs over time since I first started logging them. If I use any other inline tags in the workout log (or in your case, your daily journal), the filter should reveal other keywords in the draft that may or may not be connected/correlated (per block). And since the prompt has its own search facility, I should be able to filter within the initial set of results to look for patterns and trends.

That’s the theory, at least. I think most of the components are there. I just need to round out the methodology. :slight_smile:

For me, if I wanted to do what you’re doing, the tags would probably go after the workout task in the weekly log. I write in prose in my longform journals, you’re working with blocks, which don’t work as well for a journal entry with multiple paragraphs, :relaxed:.

As I suspected, seeing the header for my workout logs is useless, since the link is by itself on its own line at the bottom of the journal entry and the header’s always the same, Workout. Anyway, I’m reasonably happy with the solution I have, although I might generalise it a bit to grab any journal entry and not rely on it being called workout. The thing I’m working on right now is to be able to run it from a link to the workout and not have to open the workout Draft first. Might steal your bracket-link detection code for that, :relaxed:.

As for what I’m aiming for, you’re pretty much right. To use your run example, it might give me a hint when to increase the time I run, or to find a hill or something. For me, it’s often things I notice about my form in a movement, say, and tracking improvement on that. Since I use workout videos, it might give me an idea of what to swap in the next 4/6 week block. For example, I’m transitioning right now, and I want a workout for lower body to improve strength, since the current lower-body workoutwas more of a balance workout.

A lot of this is all about my basic problems with habit trackers. If I skip a habit, to me, it’s more important why I skipped it. If I’m having a hard time starting a workout, why? Did I not sleep well? am I stressed? etc. Simply saying I did something or I didn’t is never enough for me, :relaxed:.

As for Drafts limitations and improving workflows etc, unless authors of apps like Obsidian or Craft or whatever change radically in the next few years, I’m here until Greg gives up. Nobody else seems to give a damn about Voiceover accessibility, so this is the best so called “tool for thought” I have. But honestly, as the years go on I realise it’s much more about workflow than tools, anyway. Particularly given that right now I’m trying to be as manual as possible with the log/Bullet journal system. Minimal templates, no automatic task imports, no automatic moving incomplete tasks to the next day etc. I find that the more automated it is, the less attention I pay to it. If I don’t do a task I planned for the week, I actually have to move it to next week myself, or move it back to where it was and work out why I didn’t do it, :relaxed:. Wow, this is even more off topic.