mirror of
https://github.com/directus/directus.git
synced 2026-01-28 06:08:01 -05:00
1.6 KiB
1.6 KiB
description, tags, skill_level, directus_version, author_override, author
| description | tags | skill_level | directus_version | author_override | author |
|---|---|---|---|---|---|
| A simple recipe to slugify a string of text using the Run Script operation. | 9.18.1 | Eron Powell |
Slugify Text
{{ $frontmatter.description }}
:::tip Author: {{$frontmatter.author}}
Directus Version: {{$frontmatter.directus_version}}
:::
Explanation
In some cases, you may want to take text from a title or other source and slugify it. Here's how you can implement this in a flow. Keep in mind, slugification methods can get quite complex. This recipe is intended for basic, everyday English text.
The Recipe
:::tip Requirements
You'll need a string somewhere in your data chain.
:::
- Create a Run Script operation in your flow.
- Paste the following function into your Run Script operation.
module.exports = async function (data) {
// Index data to get the string you want to slugify
// Assign it to the "text" var below.
const text = data.opKey.nested_value;
const slug = text
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
return slug;
};
Final Tips
Remember, the returned value doesn't need to be a string. You can append any valid JSON onto the data chain. You could take in an array of strings, slugify all of them, push each to a new array, and append it onto the data chain... or whatever your use-case calls for!