Example of retrieving a YAML value from a note and updating a YAML property with it in the current note

The following example illustrates one way to retrieve the “PROJ_ID” YAML value from the"PROJ_INFO" file in the same directory as the current note. Once the value has been retrieved, it is used to update the “PROJ_ID” YAML in the current note. The goal of this is to allow a complete sub-directory structure to be setup as a project framework. Each time a new project is required the directory branch is copied and the PROJ_INFO YAMLs are manually updated. These updated properties are retrieved from the subdirectory objects, Eg, Buttons used to create logs in a dedicated project directory or used in a dataview query to retrieve a list of items in a project directory. By using the PROJ_INO in the project’s root directory, it is used to setup local project paths without the need to perform repetitive hard coding of paths.

{fileName} as currentFileName
---
const currentFile = app.workspace.activeLeaf.view.file;

// Ensure we have a valid file and content is accessible
if (!currentFile || !currentFile.path || !app.vault.getAbstractFileByPath(currentFile.path)) {
  return "❌ Error: No valid file or file content available.";
}

// Define the user-provided value for PROJ_ID (in this case, "PROJ_23")
const userDefinedProjID = "PROJ_23";  // Changed from "0007" to "PROJ_23"

// Retrieve the content of the current note (including frontmatter)
let content;
try {
  content = await app.vault.read(currentFile); // Correct way to read file content in Obsidian
} catch (err) {
  console.error("Error reading current file:", err);
  return "❌ Error: Failed to read current note content.";
}

// Extract the frontmatter and content
let frontmatter = {};
let noteContent = content;

// Check for frontmatter in the note content
const frontmatterMatch = content.match(/^---([\s\S]*?)---/);
if (frontmatterMatch) {
  frontmatter = frontmatterMatch[1];
  noteContent = content.replace(/^---([\s\S]*?)---/, ''); // Remove the frontmatter to get the rest of the content
}

// Parse the frontmatter and update PROJ_ID
let frontmatterData = {};
if (frontmatter) {
  frontmatterData = frontmatter
    .split("\n")
    .filter(line => line.trim() !== "")
    .reduce((acc, line) => {
      const [key, ...value] = line.split(":");
      acc[key.trim()] = value.join(":").trim();
      return acc;
    }, {});
}

// Ensure the PROJ_ID is set with the correct type
// If PROJ_ID should be a string (ensure it has quotes)
frontmatterData.PROJ_ID = `"${userDefinedProjID}"`;  // Updated value to "PROJ_23"

// Rebuild the frontmatter
const updatedFrontmatter = `---\n${Object.entries(frontmatterData)
  .map(([key, value]) => `${key}: ${value}`)
  .join("\n")}\n---`;

// Combine the updated frontmatter and the rest of the note content
const updatedContent = `${updatedFrontmatter}\n${noteContent}`;

// Save the updated content back into the note
try {
  await app.vault.modify(currentFile, updatedContent);
  return `PROJ_ID has been successfully updated to: ${userDefinedProjID}`;
} catch (err) {
  console.error("Error updating frontmatter:", err);
  return "❌ Error: Failed to update frontmatter.";
}

HERE IS AN EXAMPLE PROJ_INFO FILE STORED IN THE PROJECT ROOT DIRECTORY:

PROJ_INFO


PROJ_ID: PROJ1
PROJ_NAME: ROBOT
PROJ_STATUS: ACTIVE
tags:

  • PROJ1
    START_DTE: 2025-04-14
    PATH_IMAGES: 01 - SUPPORT FILES/IMAGES
    PATH_POCs: 01 - SUPPORT FILES/POCs
    PATH_POC_TMP: 01 - SUPPORT FILES/POCs/TMP - POC.md
    PATH_PRJLOGS: 04 - ACTIVE PROJECTS/_PROJECT TEMPLATE/07 PROJECT LOPROJ_INFOGS
    PATH_PRJLOG_TMP: 04 - ACTIVE PROJECTS/_PROJECT TEMPLATE/07 PROJECT LOGS/TMP - PROJECT LOG.md
    PATH_MTGLOGS: 04 - ACTIVE PROJECTS/_PROJECT TEMPLATE/07 MEETING LOGS
    PATH_MTGLOG_TMP: 04 - ACTIVE PROJECTS/_PROJECT TEMPLATE/07 MEETING LOGS/TMP - MEETING LOG.md

Project Background

This note contains information about the project.

Hope Folks Find This Useful
Best Regards
-Tim C.