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?
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 -->
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 -->
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
*/functionmergeConfigs(existingConfig:Record<string,unknown>,newConfig:Record<string,unknown>):Record<string,unknown>{return{...existingConfig,...newConfig};}// Extract current config or fallback to empty object
constcurrentConfig=frontMatter?.config??{};// Define new values to merge
constupdatedValues={something2:"A value 21"};// Merge and update frontmatter
constmergedConfig=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 -->
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
This code will REPLACE the
configfrontmatter 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 ;)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.
no need to change if we can do it with vanilla JS/TS.