Enhancement: Enable placeholders for file prefixes #663

Closed
opened 2026-10-05 02:59:16 +02:00 by adam · 2 comments
Owner

Based on feedback from Discord, it would be useful to have the ability to use placeholders in the filePrefix template. That way, users can generate unique IDs for their files.

Documentation

  • Add information on the file prefix that placeholders can be used
  • Add information to the placeholder page
  • Update the default value from the frontMatter.templates.prefix setting
Based on feedback from Discord, it would be useful to have the ability to use placeholders in the `filePrefix` template. That way, users can generate unique IDs for their files. ## Documentation - [x] Add information on the file prefix that placeholders can be used - [x] Add information to the placeholder page - [x] Update the default value from the `frontMatter.templates.prefix` setting <!-- github-sync-id:2470099938 -->
adam closed this issue 2026-10-05 02:59:16 +02:00
Author
Owner

@wottpal it is now possible in the beta to use placeholders for defining your file prefixes. You are also able to use a custom placeholders with which you would be able to generate an index number during the file creation.

Example configuration:

{
   "frontMatter.content.pageFolders": [{
    "title": "articles",
    "path": "[[workspace]]/content/prefixes/",
    "filePrefix": "{{indexNr}}"
  }],
  "frontMatter.content.placeholders": [{
    "id": "indexNr",
    "script": "./scripts/indexNr.mjs",
    "command": "~/.nvm/versions/node/v18.17.1/bin/node"
  }]
}

The script:

import { PlaceholderScript } from "@frontmatter/extensibility";
import { readdir } from "fs/promises";

(async () => {
  // Filepath is the directory path where the file will be created
  const { filePath } = PlaceholderScript.getArguments();
  if (!filePath) {
    PlaceholderScript.done(`000`);
    return;
  }

  const files = await readdir(filePath);
  if (!files) {
    PlaceholderScript.done(`000`);
    return;
  }

  // Return the number of files + 1 with leading zeros
  PlaceholderScript.done((files.length + 1).toString().padStart(3, `0`));
})();

Notice that the @frontmatter/extensibility library is used. More information on how to install it can be found here: https://frontmatter.codes/docs/custom-actions#extensibility-library

@wottpal it is now possible in the beta to use placeholders for defining your file prefixes. You are also able to use a custom placeholders with which you would be able to generate an index number during the file creation. Example configuration: ```json { "frontMatter.content.pageFolders": [{ "title": "articles", "path": "[[workspace]]/content/prefixes/", "filePrefix": "{{indexNr}}" }], "frontMatter.content.placeholders": [{ "id": "indexNr", "script": "./scripts/indexNr.mjs", "command": "~/.nvm/versions/node/v18.17.1/bin/node" }] } ``` The script: ```js import { PlaceholderScript } from "@frontmatter/extensibility"; import { readdir } from "fs/promises"; (async () => { // Filepath is the directory path where the file will be created const { filePath } = PlaceholderScript.getArguments(); if (!filePath) { PlaceholderScript.done(`000`); return; } const files = await readdir(filePath); if (!files) { PlaceholderScript.done(`000`); return; } // Return the number of files + 1 with leading zeros PlaceholderScript.done((files.length + 1).toString().padStart(3, `0`)); })(); ``` Notice that the `@frontmatter/extensibility` library is used. More information on how to install it can be found here: https://frontmatter.codes/docs/custom-actions#extensibility-library <!-- github-comment-id:2338034144 -->
Author
Owner

This functionality is now part of the v10.4.0 main release.

This functionality is now part of the `v10.4.0` main release. <!-- github-comment-id:2373239290 -->
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#663