Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Publish Flag shortcut destroys frontmatter meta-tags #644

Open
janfromm opened this issue Oct 8, 2024 · 0 comments
Open

Set Publish Flag shortcut destroys frontmatter meta-tags #644

janfromm opened this issue Oct 8, 2024 · 0 comments

Comments

@janfromm
Copy link

janfromm commented Oct 8, 2024

I encountered a problem when using the "Digital Garden: Add Publish Flag" feature. It seems to corrupt the meta-tags in the frontmatter. For example:
dg-metatags: { description: "some description" }
gets replaced by:
dg-metatags: [object Object]
This breaks the structure and format of the metadata.

Suggested Solution
To avoid this issue, I propose modifying the setPublishFlagValue() function so that only the dg-publish flag is updated without affecting other frontmatter fields. Here's an updated version of the function:

setPublishFlagValue(value) {
  return __async(this, null, function* () {
    const activeFile = this.getActiveFile(this.app.workspace);
    if (!activeFile) {
      return;
    }

    const fileContent = yield this.app.vault.read(activeFile);

    // Extract the frontmatter by matching the first three lines
    const frontMatterMatch = fileContent.match(/^---\n([\s\S]+?)\n---/);
    
    if (!frontMatterMatch) {
      // If no frontmatter exists, add it
      const newFrontMatter = `---\ndg-publish: ${value}\n---\n\n${fileContent}`;
      yield this.app.vault.modify(activeFile, newFrontMatter);
      return;
    }

    // Parse the frontmatter
    let frontMatter = frontMatterMatch[1];

    // Modify only the "dg-publish" flag
    if (/dg-publish:\s*(true|false)/.test(frontMatter)) {
      // Replace the existing "dg-publish" flag
      frontMatter = frontMatter.replace(/dg-publish:\s*(true|false)/, `dg-publish: ${value}`);
    } else {
      // If the "dg-publish" flag doesn't exist, add it
      frontMatter += `\ndg-publish: ${value}`;
    }

    // Create the new file content
    const newFileContent = fileContent.replace(/^---\n[\s\S]+?\n---/, `---\n${frontMatter}\n---`);

    // Save the file with the updated frontmatter
    yield this.app.vault.modify(activeFile, newFileContent);
  });
}

Why This Works:
It directly manipulates the frontmatter, ensuring other fields like dg-metatags remain intact.
Only the dg-publish flag is updated or added if it doesn’t exist.
The rest of the frontmatter stays unchanged, avoiding issues with formatting or data loss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant