Insert next level header

Use case or problem

When I’m writing my notes I tend to user a variety of different header levels (i.e. #, ##, ###, etc) and I often want to add a new header within the current section and within the Live Preview Editor it’s difficult for me to know the next level down (i.e. if the current section has a ## heading then I want to insert ###).

Proposed solution

So my proposal is to add a command with a name similar to Insert next level header that will look for the nearest header above the cursor and then insert a new line with the correct heading level.

Current workaround (optional)

Either:

  • Move my cursor up to the nearest header to check what level heading it is then move the cursor back to where I was to insert the next level heading manually
    • I don’t like this because it takes time and interrupts my flow
  • Run Toggle Live Preview/Source Mode
    • This often doesn’t work well because if there’s lots of rich text/links/images on the page the heading will jump around significantly.
2 Likes

If they’re going to add “Insert child heading”, they should also add “Insert sibling heading”

1 Like

If they’re going to add “Insert child heading” and “Insert sibling heading”, they should also add “Insert parent heading”. :wink:

@axelson here’s a quick and easy Templater script to do exactly this:

<%*
// Get the document content up until the current cursor position
const lines = app.workspace.activeLeaf.view.editor.getCursor().line
const content = tp.file.content.split('\n').slice(0, lines).join('\n')

// Get an array of headings from the previous document content
const headings = content.match(/^#+ \S/mg)

// Get an integer representing the previous header level
const prevHeaderLevel = headings.pop().match(/^(#+)/)[1].length

// Output a child header
tR += '#'.repeat(prevHeaderLevel + 1) + ' '
%>

@obsequious @CawlinTeffid For sibling or parent header it would be this:

// Output a sibling header
tR += '#'.repeat(prevHeaderLevel) + ' '

// Output a parent header
tR += '#'.repeat(Math.max(1, prevHeaderLevel - 1)) + ' '

Assign the Templater script to a hotkey, and you can insert a child/sibling/parent header anywhere you wish.

5 Likes

@AlanG Awesome, thanks!

Is there a way to not insert a newline after the hash signs? Maybe triggering a backspace?

It doesn’t insert a newline for me. It does insert a space though, if that’s what you meant?

Just remove the + ' ' from the last line of the script.

<%*
// Get the document content up until the current cursor position
const lines = app.workspace.activeLeaf.view.editor.getCursor().line
const content = tp.file.content.split('\n').slice(0, lines).join('\n')

// Get an array of headings from the previous document content
const headings = content.match(/^#+ \S/mg)

// Get an integer representing the previous header level
const prevHeaderLevel = headings.pop().match(/^(#+)/)[1].length

// Output a child header
tR += '#'.repeat(prevHeaderLevel + 1)
%>
1 Like

Most likely you have a newline at the end of your template file. The template will insert exactly what’s in the file, so if you add blank lines, it will insert blank lines:

image

2 Likes

That was it, thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.