Collapsible Bar for YAML Fields

I find the YAML field’s to be a little distracting while editing notes ( I have 6 Some fields for almost all notes) .

Is there any chance that we might have a collapsible bar for these fields? Similar to link below

firefox_SoZwu02Ftq

You can fold/unfold the YAML section using a hotkey. I use Ctrl + Y which is unassigned by default.

Steps to set up

  1. Install Templater, and paste this into a new template note:
<%*
// Get the current cursor location
const cursor = app.workspace.activeLeaf.view.editor.getCursor()
// Move the cursor to the start of the document
app.workspace.activeLeaf.view.editor.setCursor({line: 0, ch: 0})
// Fold/unfold the YAML
app.commands.executeCommandById('editor:toggle-fold')
// Move the cursor back to the original location
app.workspace.activeLeaf.view.editor.setCursor(cursor)
%>
  1. Open the Templater config page and set a hotkey for the new template.

  2. That’s it, you’re done!


Automatically hiding YAML in newly created notes

You can also make this part of another template, so for example, once you insert your new note template the YAML will be automatically hidden.

If you’re doing that, you will need to put the script in its own Templater “User scripts” file per the User Scripts documentation.

  1. Create a hideYaml.js file (not a Markdown .md note file). Place this in the “Script files folder location” which is configured in Templater config.

  2. Paste this into your hideYaml.js file:

function main() {
  setTimeout(() => {
    const cursor = app.workspace.activeLeaf.view.editor.getCursor()
    app.workspace.activeLeaf.view.editor.setCursor({line: 0, ch: 0})
    app.commands.executeCommandById('editor:toggle-fold')
    app.workspace.activeLeaf.view.editor.setCursor(cursor)
    return ''
  }, 100)
}
module.exports = main
  1. Call the script file from any other template file like this:
<%* tp.user.hideYaml() %>

That way it’s just a single one-liner which you can put in any other template. The reason for the setTimeout delay is so the new note can be created, and only after that will the YAML be hidden.

1 Like

I’m attempting to glue everything together; hopefully it works okay. :slight_smile:

Thank you! :innocent:

Let me know if you run into any issues and I’ll update the docs to fill in the gaps.

1 Like

I’m grateful. I value your assistance.

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