|
| 1 | +--- |
| 2 | +title: Custom actions/scripts |
| 3 | +slug: custom-actions |
| 4 | +description: null |
| 5 | +date: 2021-08-30T16:13:00.546Z |
| 6 | +lastmod: 2024-08-12T14:58:47.310Z |
| 7 | +weight: 500 |
| 8 | +--- |
| 9 | + |
| 10 | +# Custom actions and scripts |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +<!-- markdownlint-disable MD013 MD033 --> |
| 15 | +<div class="iframe__wrapper"> |
| 16 | + <iframe src="https://www.youtube.com/embed/wvH9Tn5LQ2c" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe> |
| 17 | +</div> |
| 18 | +<!-- markdownlint-enable MD013 MD033 --> |
| 19 | + |
| 20 | +Not every website is the same. That is why we want to give you the ability to extend Front Matter |
| 21 | +and you can do this by adding your custom actions to the Front Matter panel. A custom action is |
| 22 | +nothing more than a Node.js script which is referenced from within your project. |
| 23 | + |
| 24 | +![Custom action][01] |
| 25 | + |
| 26 | +## Settings |
| 27 | + |
| 28 | +The content and media custom actions can be defined by using the `frontMatter.custom.scripts` setting. |
| 29 | + |
| 30 | +Custom actions can be configured with the following properties: |
| 31 | + |
| 32 | +| Title | Type | Description | Default | |
| 33 | +| -------------- | --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- | |
| 34 | +| `id` | `string` | The id of the custom action/script | `""` | |
| 35 | +| `title` | `string` | The title of the custom action | `""` | |
| 36 | +| `script` | `string` | The path to the script to execute | `""` | |
| 37 | +| `command` | `string` | The command to execute (optional). Example: `node`, `path to your node executable`, `bash`, `python`, ... | `node` | |
| 38 | +| `type` | `<content \| mediaFile \| mediaFolder>` | The type for which the script will be used (optional). <br /><br /> Use one of the following types: `content`, `mediaFile`, or `mediaFolder`. | `content` | |
| 39 | +| `bulk` | `boolean` | Run the script for one file or multiple files. | `false` | |
| 40 | +| `output` | `<notification \| editor` | Specifies the output type (optional). <br /><br /> Available values are: `notification` and `editor`. <br /><br /> `notification`: The output will be passed as a notification. <br /> `editor`: The output will be passed to the editor. | `notification` | |
| 41 | +| `outputType` | `<text \| html>` | Specifies the output type (optional). <br /><br /> Available values the editor values from VS Code like: <br /><br /> `text`: The output will be passed as a text file. <br /> `html`: The output will be passed as an HTML file. <br /> `markdown`: The output will be passed as a Markdown file. | `text` | |
| 42 | +| `hidden` | `boolean` | Hide the action from the UI. This is mostly used when creating a content script that will be used to post process new content (optional). | `false` | |
| 43 | +| `environments` | `environment` | The environments option allows you to specify in which environments the script should be executed (optional). <br /><br /> Available values are: `macos`, `linux`, or `windows`. | `undefined` | |
| 44 | +| `contentTypes` | `string[]` | The content types for which the script will be used (optional). <br /><br /> Example: `["post"]` | `undefined` | |
| 45 | + |
| 46 | +> **Important**: Previously, you could define the `nodeBin` property to define the path to your node |
| 47 | +> executable. This path was needed when you are working with for instance `nvm` and have multiple |
| 48 | +> versions of node installed. You can now use the `command` property instead. |
| 49 | +
|
| 50 | +### Environment type |
| 51 | + |
| 52 | +The environment option contains the following properties: |
| 53 | + |
| 54 | +| Title | Type | Description | Default | |
| 55 | +| --------- | ----------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | |
| 56 | +| `type` | `<macos \| linux \| windows>` | The environment type for the script to run at. | `""` | |
| 57 | +| `script` | `string` | The path to the script to execute. | `""` | |
| 58 | +| `command` | `string` | The command to execute (optional). Example: `node`, `path to your node executable`, `bash`, `python`, ... | `node` | |
| 59 | + |
| 60 | +#### Example of defining a custom action with an environment |
| 61 | + |
| 62 | +```json {{ "title": "Custom action configuration" }} |
| 63 | +{ |
| 64 | + "frontMatter.custom.scripts": [ |
| 65 | + { |
| 66 | + "title": "Create image folder", |
| 67 | + "id": "create-image-folder", |
| 68 | + "script": "./.frontmatter/config/custom/scripts/create-image-folder.sh", |
| 69 | + "command": "bash", |
| 70 | + "environments": [ |
| 71 | + { |
| 72 | + "type": "windows", |
| 73 | + "script": "./.frontmatter/config/custom/scripts/create-image-folder.ps1", |
| 74 | + "command": "powershell" |
| 75 | + } |
| 76 | + ] |
| 77 | + } |
| 78 | + ] |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +> **Info**: The above sample would execute the bash script on macOS and Linux and the PowerShell for |
| 83 | +> Windows. In case the PowerShell script would fail, it would fallback to the bash script. |
| 84 | +
|
| 85 | +## Extensibility library |
| 86 | + |
| 87 | +The [@frontmatter/extensibility](https://www.npmjs.com/package/@frontmatter/extensibility) library |
| 88 | +provides you with the necessary methods to interact with the Front Matter CMS. |
| 89 | + |
| 90 | +You can use the library for your custom actions to update the front matter of your content or media files. |
| 91 | + |
| 92 | +### Installing the extensibility library |
| 93 | + |
| 94 | +When using JavaScript, you can make use of the |
| 95 | +[@frontmatter/extensibility](https://www.npmjs.com/package/@frontmatter/extensibility) package. |
| 96 | + |
| 97 | +```bash {{ "title": "Install the extensibility package" }} |
| 98 | +npm i @frontmatter/extensibility |
| 99 | +``` |
| 100 | + |
| 101 | +With this `@frontmatter/extensibility` dependency, |
| 102 | +you can easily get the arguments and ask questions to the user. |
| 103 | + |
| 104 | +<!-- Link References --> |
| 105 | + |
| 106 | +[01]: /assets/custom-action.png |
0 commit comments