Templater icon indicating copy to clipboard operation
Templater copied to clipboard

A template to fetch the nearest header above the position of the template

Open SkepticMystic opened this issue 3 years ago • 5 comments

Describe the solution you'd like

  • I want to have a template {{tp_heading}} which resolves to the content of the first heading above the template.

For example, when placed here:

# Heading

## Subheading

{{tp_heading}}

The template would resolve to the string Subheading


It would also help to be able to feed an argument level which says which level of heading to look for.

For example, placed here:

# Heading

## Subheading

{{tp_heading: level=1}}

The template would resolve to the first level 1 heading above it: Heading


Describe alternatives you've considered

I have literally just started learning js, but after having scrolled trhough the existing internal template, I have written the following rough outline of the template:

async function tp_header(app: App, args: {[key: string]: number}): Promise < String > {

  let activeLeaf = app.workspace.activeLeaf;
  let content = app.vault.read(activeLeaf);

  const level;

  if (existing_argument(args, "heading")) {
    level = args["heading"];
  } else {
    // Fetch the nearest heading level above the position of the template
    level = ;
  }


  // Regex to find heading content at the specified level (doesn't work as intended)
  const regex_findHeadingContent = /^#{${level}} ([^\n])/;

  let heading_string = regex_findHeadingString.exec(content);
  return heading_string
}

As you can see, I don't know what I'm doing. I hope you get the idea though.

SkepticMystic avatar Mar 27 '21 14:03 SkepticMystic

Thanks a lot for this well detailed feature request ! I'm not sure I understand the use case though, what's the point of duplicating the header above the template ?

SilentVoid13 avatar Mar 27 '21 14:03 SilentVoid13

One example that it could be used with right away is to create a query block listing all other notes which have that heading:

```query
"# {{tp_heading}}"
```

Or to list all other notes with the same heading structure (but possibly different content under those headings):

```query
section:({{tp_heading:level=1}} section:({{tp_heading:level=2))
```

Also, perhaps when dataview allows querying by heading, or note content in general, this would also allow for more complex queries using this template.

I agree that it is quite a specific use case, though.

SkepticMystic avatar Mar 27 '21 15:03 SkepticMystic

I was looking for this as well, my use case would be in making summaries of mathematical courses.

My latest heading would be something like: Symmetric Matrix then within that section I would use a template for a definition like his:

Definition tp_header

where the definition for a symmetric matrix would go.

edit: not entirely sure this is what was meant by OP, but I'll still leave my comment here. If you feel this is not appropriate please delete or contact me to delete this comment.

jerrephysics avatar Jan 04 '22 08:01 jerrephysics

I was looking for something similar and came across this issue. Another use case could be adding DataView annotation specific to a section within a larger note. For example, to quickly label an issue in a meeting or daily document based on the section within that document:

[label::{{tp_heading}}]

PanicTrain avatar May 12 '22 12:05 PanicTrain

Bump on this, to give another example of a use case. I want a template which I can use with the tasks plug-in which would automatically grab the preceding header and inject that as the relevant input to a tasks query. Something like `

User1

not done
tags include #<% tp_heading %>

`

naven87 avatar Feb 23 '24 17:02 naven87