Skip to content

Commit 3f1b23c

Browse files
committed
Added placeholder script sample
1 parent bdd2574 commit 3f1b23c

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

content/docs/content-creation/placeholders.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Placeholders
33
slug: content-creation/placeholders
44
description: null
55
date: 2022-03-14T08:42:21.626Z
6-
lastmod: 2023-08-20T11:04:12.770Z
6+
lastmod: 2024-01-31T16:17:03.272Z
77
weight: 200.4
88
---
99

@@ -140,26 +140,30 @@ Here is an example of a dynamic `uniqueId` placeholder:
140140
"frontMatter.content.placeholders": [
141141
{
142142
"id": "uniqueId",
143-
"script": "./scripts/uniqueId.js",
144-
"command": "~/.nvm/versions/node/v16.13.1/bin/node"
143+
"script": "./scripts/uniqueId.mjs",
144+
"command": "~/.nvm/versions/node/v18.17.1/bin/node"
145145
}
146146
]
147147
```
148148

149149
#### Placeholder script
150150

151-
The base script looks like this:
151+
To get started, you first need to install the
152+
[@frontmatter/extensibility](https://www.npmjs.com/package/@frontmatter/extensibility)
153+
dependency.
154+
155+
```bash
156+
npm i @frontmatter/extensibility
157+
```
158+
159+
Once installed, you can use the following example:
152160

153161
```javascript
154-
const arguments = process.argv;
162+
import { PlaceholderScript } from "@frontmatter/extensibility";
155163

156-
if (arguments && arguments.length > 0) {
157-
const workspaceArg = arguments[2]; // The workspace path
158-
const filePath = arguments[3]; // The path of the file
159-
const title = arguments[4]; // Title of the page
164+
const { workspacePath, filePath, title, answers } = PlaceholderScript.getArguments();
160165

161-
console.log(Math.random().toString(36).substring(2, 15));
162-
}
166+
PlaceholderScript.done(Math.random().toString(36).substring(2, 15));
163167
```
164168

165169
> **Info**: Like the other content scripts, you can use other types of scripts like Python, Bash,
@@ -172,6 +176,38 @@ that the file is still not completely processed, and not all front matter fields
172176
> **Important**: In case you need to retrieve the whole front matter object, you can make use of the
173177
> `postScript` property on your content type in combination with a [content script][01].
174178
179+
You can also ask additional input/questions during the placeholder script execution.
180+
For instance, if you want to pick between a category upon content creation, you can use the
181+
`PlaceholderScript.askQuestions` method.
182+
183+
```javascript
184+
import { PlaceholderScript } from "@frontmatter/extensibility";
185+
186+
(async () => {
187+
const { answers } =
188+
PlaceholderScript.getArguments();
189+
190+
if (!answers) {
191+
PlaceholderScript.askQuestions([
192+
{
193+
name: "category",
194+
message: "What category do you want to use for this article?"
195+
},
196+
]);
197+
return;
198+
}
199+
200+
const { category } = answers;
201+
202+
if (!category) {
203+
PlaceholderScript.done(undefined);
204+
return;
205+
}
206+
207+
PlaceholderScript.done(category);
208+
})();
209+
```
210+
175211
#### Placeholder usage
176212

177213
To use the `ogImage` placeholder, you need to define the `{{ogImage}}` value in your field as

content/docs/custom-actions.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Custom actions
33
slug: custom-actions
44
description: null
55
date: 2021-08-30T16:13:00.546Z
6-
lastmod: 2023-12-07T16:37:05.124Z
6+
lastmod: 2024-01-31T12:48:37.504Z
77
weight: 500
88
---
99

@@ -116,12 +116,12 @@ When you do not want to use the `@frontmatter/extensibility` package, you can cr
116116
follows:
117117

118118
```javascript
119-
const arguments = process.argv;
119+
const args = process.argv;
120120

121-
if (arguments && arguments.length > 0) {
122-
const workspaceArg = arguments[2]; // The workspace path
123-
const fileArg = arguments[3]; // The file path
124-
const frontMatterArg = arguments[4]; // Front matter data
121+
if (args && args.length > 0) {
122+
const workspaceArg = args[2]; // The workspace path
123+
const fileArg = args[3]; // The file path
124+
const frontMatterArg = args[4]; // Front matter data
125125

126126
console.log(`The content returned for your notification.`);
127127
}
@@ -130,9 +130,9 @@ if (arguments && arguments.length > 0) {
130130
The current workspace-, file-path, and front matter data will be passed as a arguments. Like you can
131131
see in the above sample script, you can fetch these argument values as follows:
132132

133-
- `arguments[2]`: The workspace path
134-
- `arguments[3]`: The file path (Markdown file)
135-
- `arguments[4]`: The front matter data as object
133+
- `args[2]`: The workspace path
134+
- `args[3]`: The file path (Markdown file)
135+
- `args[4]`: The front matter data as object
136136

137137
### Creating a script with the extensibility package
138138

@@ -272,8 +272,8 @@ Here is a sample on how you can define a media script:
272272

273273
The script will provide you the following arguments:
274274

275-
- `arguments[2]`: The workspace path
276-
- `arguments[3]`: The file or folder path. This depends on the type of script.
275+
- `args[2]`: The workspace path
276+
- `args[3]`: The file or folder path. This depends on the type of script.
277277

278278
When using the `@frontmatter/extensibility` package, you can get the arguments as follows:
279279

@@ -383,15 +383,15 @@ print(f'frontMatterArg: {sys.argv[3]}')
383383
```javascript
384384
const path = require("path");
385385

386-
const arguments = process.argv;
386+
const args = process.argv;
387387

388388
(async () => {
389-
if (arguments && arguments.length > 0) {
389+
if (args && args.length > 0) {
390390
const imagemin = (await import("imagemin")).default;
391391
const imageminJpegtran = (await import("imagemin-jpegtran")).default;
392392
const imageminPngquant = (await import("imagemin-pngquant")).default;
393393

394-
const fileArg = arguments[3]; // The file path
394+
const fileArg = args[3]; // The file path
395395

396396
await imagemin([fileArg], {
397397
destination: path.dirname(fileArg),
@@ -413,16 +413,16 @@ const arguments = process.argv;
413413
```javascript
414414
const path = require("path");
415415

416-
const arguments = process.argv;
416+
const args = process.argv;
417417

418418
(async () => {
419-
if (arguments && arguments.length > 0) {
419+
if (args && args.length > 0) {
420420
const imagemin = (await import("imagemin")).default;
421421
const imageminJpegtran = (await import("imagemin-jpegtran")).default;
422422
const imageminPngquant = (await import("imagemin-pngquant")).default;
423423

424-
const workspaceArg = arguments[2]; // The workspace path
425-
const folderArg = arguments[3]; // The folder path
424+
const workspaceArg = args[2]; // The workspace path
425+
const folderArg = args[3]; // The folder path
426426

427427
const files = await imagemin([path.join(folderArg, "*.{jpg,png}")], {
428428
destination: folderArg,

0 commit comments

Comments
 (0)