`processFrontMatter` creates undesired line break for long strings

Yes, I also have this issue with Linter.

I just wrote my own version of processFrontMatter to workaround this problem with line breaks

so instead of

await app.fileManager.processFrontMatter(file, fn);

I use

await customJS.FrontMatter.process(file, fn);

Here I use GitHub - saml-dev/obsidian-custom-js: An Obsidian plugin to allow users to reuse code blocks across all devices and OSes , but my code could be easily adopted to be used elsewhere.


class FrontMatter {
  async process(file, frontMatterFn) {
    const { load, dump } = await import('https://cdn.jsdelivr.net/npm/[email protected]/+esm');

    await app.vault.process(file, (content) => {
      const match = content.match(/^---\r?\n((?:.|\r?\n)*?)\r?\n?---(?:\r?\n|$)((?:.|\r?\n)*)/);
      let frontMatterStr;
      let mainContent;
      if (match) {
        frontMatterStr = match[1];
        mainContent = match[2];
      } else {
        frontMatterStr = '';
        mainContent = content;
      }

      if (!mainContent) {
        mainContent = '\n';
      } else {
        mainContent = '\n' + mainContent.trim() + '\n';
      }

      const frontMatter = load(frontMatterStr) ?? {};
      frontMatterFn(frontMatter);
      let newFrontMatterStr = dump(frontMatter, {
        lineWidth: -1,
        quotingType: '"'
      });
      if (newFrontMatterStr === '{}\n') {
        newFrontMatterStr = '';
      }

      const newContent = `---
${newFrontMatterStr}---
${mainContent}`;

      return newContent;
    });
  }
}
2 Likes