Regex rule to format cover image wikilinks in YAML

Here’s a regex replacement rule that formats the wikilink to an image file in frontmatter correctly by removing the preceding !. It also adds ^ as link caption so that the link isn’t an eyesore.

regex to find:

^(cover:\s*")(!\[\[|\[\[)([^|\]]+)(\|[^\]]*)?(\]\]")$

flags: gm

regex to replace:

$1$2[[$4|^]]"$7

This rule on regex101

Before:

After:

Note that only the link in the cover property is affected.

I set this up as a custom regex replacement rule in Linter settings > Custom.

I have Linter set up to automatically lint files on save and focused file change, so this rule is applied automatically.

This is useful when working with:

  • Bases card view.
  • Community plugins that add banners to notes.
  • Images localized by Local images plus.

Make sure to back up your vault before running this rule for the whole vault in case of bugs.

2 Likes