Feedback: Either more in-depth docs or merge deep frontmatter #723

Closed
opened 2026-10-05 03:02:14 +02:00 by adam · 2 comments
Owner
import { ContentScript } from "@frontmatter/extensibility";

ContentScript.updateFrontMatter({
  config: {
    something2: "A value 21"
  }
})

This code will REPLACE the config frontmatter completely. I was wondering how to update a single item of the config "sub array" and was playing around. It appears that the only way to change/add subitems to frontmatter will be (1) reading the item in and then (2) replacing it. I think that should be somehow documented with an example.

I know that the easy way out would be to read in the frontmatter under config (*1) and then replace the item I want to change and then updateFrontMatter. But... (*1) - how?

The docs here are very skimpy on the details ;)

I think the easiest way might be to give us a ContentScript.getFrontMatter('config') or something along these lines ;)

``` import { ContentScript } from "@frontmatter/extensibility"; ContentScript.updateFrontMatter({ config: { something2: "A value 21" } }) ``` This code will REPLACE the `config` frontmatter completely. I was wondering how to update a single item of the config "sub array" and was playing around. It appears that the only way to change/add subitems to frontmatter will be (1) reading the item in and then (2) replacing it. I think that should be somehow documented with an example. I know that the easy way out would be to read in the frontmatter under `config` (*1) and then replace the item I want to change and then updateFrontMatter. But... (*1) - how? The docs [here](https://frontmatter.codes/docs/custom-actions/content-scripts#update-the-front-matter) are very skimpy on the details ;) I think the easiest way might be to give us a `ContentScript.getFrontMatter('config')` or something along these lines ;) <!-- github-sync-id:2904827252 -->
adam closed this issue 2026-10-05 03:02:14 +02:00
Author
Owner

To dive deeper into this rabbit hole, let's assume a use case where I want to use the first item in a frontmatter subitem (like resources > src in gohugo) and then do something to that item and write it back to another frontmatter item (like config > something) without destroying/changing any of the other items.

To dive deeper into this rabbit hole, let's assume a use case where I want to use the first item in a frontmatter subitem (like resources > src in gohugo) and then do something to that item and write it back to another frontmatter item (like config > something) without destroying/changing any of the other items. <!-- github-comment-id:2708339839 -->
Author
Owner
import { ContentScript } from "@frontmatter/extensibility";

const {
  command,
  scriptPath,
  workspacePath,
  filePath,
  frontMatter,
  answers
} = ContentScript.getArguments();

/**
 * Merges existing config with new values and updates frontmatter
 * @param {object} existingConfig - The current config from frontmatter
 * @param {object} newConfig - The new values to merge into config
 * @returns {object} - The merged config object
 */
function mergeConfigs(existingConfig: Record<string, unknown>, newConfig: Record<string, unknown>): Record<string, unknown> {
  return {
    ...existingConfig,
    ...newConfig
  };
}

// Extract current config or fallback to empty object
const currentConfig = frontMatter?.config ?? {};

// Define new values to merge
const updatedValues = {
  something2: "A value 21"
};

// Merge and update frontmatter
const mergedConfig = mergeConfigs(currentConfig, updatedValues);

ContentScript.updateFrontMatter({
  config: mergedConfig
});

no need to change if we can do it with vanilla JS/TS.

```js import { ContentScript } from "@frontmatter/extensibility"; const { command, scriptPath, workspacePath, filePath, frontMatter, answers } = ContentScript.getArguments(); /** * Merges existing config with new values and updates frontmatter * @param {object} existingConfig - The current config from frontmatter * @param {object} newConfig - The new values to merge into config * @returns {object} - The merged config object */ function mergeConfigs(existingConfig: Record<string, unknown>, newConfig: Record<string, unknown>): Record<string, unknown> { return { ...existingConfig, ...newConfig }; } // Extract current config or fallback to empty object const currentConfig = frontMatter?.config ?? {}; // Define new values to merge const updatedValues = { something2: "A value 21" }; // Merge and update frontmatter const mergedConfig = mergeConfigs(currentConfig, updatedValues); ContentScript.updateFrontMatter({ config: mergedConfig }); ``` no need to change if we can do it with vanilla JS/TS. <!-- github-comment-id:2764406114 -->
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: adam/vscode-front-matter#723