obsidian-digital-garden icon indicating copy to clipboard operation
obsidian-digital-garden copied to clipboard

Link aliases in YAML

Open davidkopp opened this issue 1 year ago • 3 comments

With the new Obsidian Properties feature it is now quite common to use links to notes in the YAML frontmatter. Also link aliases are supported. The following YAML is valid within Obsidian:

dg-publish: true
links:
  - "[[Link to Note|Link Alias]]"

However, this leads to the following error raised by Eleventy:

Error:

Original error stack trace: YAMLException: unknown escape sequence at line 11, column 71:
[11ty]      ... Link to Note\|Link Alias]]"

davidkopp avatar Jan 11 '24 10:01 davidkopp

Related to https://github.com/oleeskild/obsidian-digital-garden/issues/502

davidkopp avatar Jan 11 '24 10:01 davidkopp

As a quick workaround I'm rewriting the frontmatter before build. I put this to .eleventy.js.

eleventyConfig.on('eleventy.before', async ({ dir }) => {
  const files = await fs.promises.readdir(dir.input, {recursive: true});
  const markdownFiles = files.filter((file) => file.endsWith('.md'));
  for (const file of markdownFiles) {
    const content = await fs.promises.readFile(`${dir.input}/${file}`, 'utf-8');
    const frontmatterRegex = /^---([\s\S]*?)---/m;
    const match = content.match(frontmatterRegex);

    if (match) {
      const updatedFrontmatter = match[1].replace(/\\\|/g, '|');
      const updatedContent = content.replace(match[0], `---${updatedFrontmatter}---`);
      await fs.promises.writeFile(`${dir.input}/${file}`, updatedContent, 'utf-8');
    }
  }
});

filhit avatar Mar 15 '24 12:03 filhit

The other option is to rearrange the compiler steps so that convertFrontMatter is the last step, rather than the first. However, for me, it’s easier to maintain a custom template than a custom Obsidian extension. The proper fix would likely involve adjusting some of the compiler steps that replace links, ensuring they don’t affect links within the frontmatter.

https://github.com/oleeskild/obsidian-digital-garden/blob/6d382830e63e192997efc49564db5ac41046dcca/src/compiler/GardenPageCompiler.ts#L103-L111

filhit avatar Mar 16 '24 16:03 filhit