# Issue using app.fileManager.processFrontMatter

**URL:** <https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233>\
**Category:** Devs: Plugins & API\
**Created:** [January 2, 2023, 6:34pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233 "2023-01-02T18:34:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [January 2, 2023, 6:34pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/1 "2023-01-02T18:34:33Z")

</div>

I’ve been trying to change the frontmatter from within a _Templater_ template, and it mostly goes OK, with the exception on when the frontmatter is present, but empty like in the following file:

```md
---
---
Empty frontmatter

```

In this particular case I can’t seem to insert a new value. Is this just me doing it the wrong way, or is this a bug/feature of either Obsidian and/or Templater? Could someone enlighten me, as to what to do with this, or how to insert a value into the empty frontmatter?

> **Files used for testing**
>
> I’ve got three files to show this behavior:
> 
> ```md
> ---
> Something: Somevalue
> ---
> NiceFrontmatter.md
> 
> ```
> 
> ```md
> ---
> ---
> EmptyFrontmatter.md
> 
> ```
> 
> ```md
> NoFrontmatter.md
> 
> ```
> 
> Each block is a file named like the body text

> **My Templater template to trigger behavior**
>
> ```md
> <%*
> const filenames = [
> 'NiceFrontmatter', 
> 'NoFrontmatter', 
> 'EmptyFrontmatter' ]
> 
> for (let name of filenames) {
> const theFile = await tp.file.find_tfile(name)
> const suggestion = tp.date.now("HH:mm:ss SSS")
>   
> await app.fileManager.processFrontMatter(theFile, (fm) => {
> console.log(suggestion, theFile.name, fm)
> if (fm == null) {
> console.log('Big red flag')
> fm = { 'Nothing' : 'No value' }
> } else {
> fm['Created'] = suggestion
> }
> })
> console.log("done")
> }
> %>
> 
> ```

> **Result after inserting the template**
>
> A screenshot of the Console from Developers Tool:  
> ![image](https://forum.obsidian.md/uploads/default/original/3X/a/d/ad073a453272a580f46eb9814bd60d3795c241da.png)
> 
> Both `NiceFrontmatter.md` and `NoFrontmatter.md` got the `Created: 19:27:18 ...` values, but as shown in the screenshot the `EmptyFrontmatter.md` has got `fm == null` and fails miserably when trying to set the `fm` (which is to be expected). I’ve also tried returning a new dictionary, like in `return {}` (or similar), but that fails as well.

---

<div class="post-metadata">

**Author:** ![AlanG](https://forum.obsidian.md/user_avatar/forum.obsidian.md/alang/32/55225_2.png) [@AlanG](https://forum.obsidian.md/u/AlanG)\
**Post date:** [January 2, 2023, 7:21pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/2 "2023-01-02T19:21:02Z")

</div>

Can replicate - looks like a bug. (unless the YAML spec requires that there be some content)

---

<div class="post-metadata">

**Author:** ![joethei](https://forum.obsidian.md/user_avatar/forum.obsidian.md/joethei/32/11969_2.png) [@joethei](https://forum.obsidian.md/u/joethei)\
**Post date:** [January 2, 2023, 7:26pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/3 "2023-01-02T19:26:57Z")

</div>

Will be fixed in a upcoming release.  
Thanks for the report.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [January 2, 2023, 7:44pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/4 "2023-01-02T19:44:49Z")

</div>

Thanks a lot! And I understand it correctly, that I don’t need to do anything else, but just wait for that upcoming release?

For those impatient, I found a workaround where I throw a custom error message from within `app.fileManager.processFrontmatter`, and then read the entire file, do a regex replace, before writing the modified file back again. Not for the faint of heart, but it seems to work.

> **A workaround using try-catch -\> app.read/app.modify**
>
> For those impatient, the following seems to work as a workaround:
> 
> ```javascript
> <%*
> 
> function emptyFrontmatter(newFrontmatter) {
> this.message = newFrontmatter
> this.name = 'emptyFrontmatter'
> }
> 
> const filenames = [
> 'NiceFrontmatter', 
> 'NoFrontmatter', 
> 'EmptyFrontmatter' ]
> 
> for (let name of filenames) {
> const theFile = await tp.file.find_tfile(name)
> const suggestion = tp.date.now("HH:mm:ss SSS")
> 
> try {
> await app.fileManager.processFrontMatter(theFile, (fm) => {
> console.log(suggestion, theFile.name, fm)
> if (fm == null) {
> throw new emptyFrontmatter({'Created': suggestion})
> } else {
> fm['Created'] = suggestion
> }
> })
> } catch (e) {
> if (e.name == 'emptyFrontmatter') {
> let str = "---\n" +
> Object.entries(e.message)
> .map(([fieldName, value]) => 
> `${fieldName}: ${value}`).join("\n") +
> "\n---"
> 
> let content = await app.vault.read(theFile)
> content = content.replace(/^---\n(?:\s*\n)*---/g, str)
> // console.log(content)
> await app.vault.modify(theFile, content)
> } else 
> throw e
>             
> } finally {
> console.log('done')
> }
> }
> %>
> 
> ```
> 
> Not entirely sure if my mapping will work in all cases, so care should be taken especially if using arrays or links or more complex objects to be inserted in the frontmatter.
> 
> **Disclaimer:** As always, use at your own risk! 😃

---

<div class="post-metadata">

**Author:** ![joethei](https://forum.obsidian.md/user_avatar/forum.obsidian.md/joethei/32/11969_2.png) [@joethei](https://forum.obsidian.md/u/joethei)\
**Post date:** [January 2, 2023, 8:26pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/5 "2023-01-02T20:26:21Z")

</div>

Correct, just wait, the frontmatter should never be null in the next release.

As for your workaround, it will work as long as nothing else is writing to that file at that moment.

---

<div class="post-metadata">

**Author:** ![JacobIO](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jacobio/32/5862_2.png) [@JacobIO](https://forum.obsidian.md/u/JacobIO)\
**Post date:** [January 12, 2023, 7:13pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/6 "2023-01-12T19:13:54Z")

</div>

@joethei, could this issue also be effecting the iOS app? I have a custom plugin that adds front-matter to notes that typically don’t already have front-matter. While it’s a bit hard to troubleshoot, I’m fairly confident the following code is throwing an error on iOS:

```auto
await app.fileManager.processFrontMatter(view.file, (fm) => {
  fm["tags"] = ['test'];
  ...
});

```

Any insights you could provide would be appreciated.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [January 12, 2023, 7:26pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/7 "2023-01-12T19:26:33Z")

</div>

In my case, the issue was only when there was an empty frontmatter, not when the frontmatter was completely missing.

---

<div class="post-metadata">

**Author:** ![joethei](https://forum.obsidian.md/user_avatar/forum.obsidian.md/joethei/32/11969_2.png) [@joethei](https://forum.obsidian.md/u/joethei)\
**Post date:** [January 13, 2023, 7:32am UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/8 "2023-01-13T07:32:53Z")

</div>

This API was added in v1.1.0, so if you are using the normal build and not the insider build this will crash.

---

<div class="post-metadata">

**Author:** ![JacobIO](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jacobio/32/5862_2.png) [@JacobIO](https://forum.obsidian.md/u/JacobIO)\
**Post date:** [January 14, 2023, 7:21pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/9 "2023-01-14T19:21:52Z")

</div>

Gotcha. Installing the Insider built fixed the issue. Thanks @joethei !

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [January 14, 2023, 9:40pm UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/10 "2023-01-14T21:40:47Z")

</div>

I’m (kind of patiently) waiting for the ordinary release, as I’m not an insider, yet. I’m just this random dude wanting this… 😃

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [November 30, 2025, 11:56am UTC](https://forum.obsidian.md/t/issue-using-app-filemanager-processfrontmatter/51233/11 "2025-11-30T11:56:42Z")

</div>

Closed on November 30, 2025, 11:56am UTC

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