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.
@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) + ' '
%>
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)
%>
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: