|
| 1 | +# strapi-plugin-slugify |
| 2 | + |
| 3 | +A plugin for [Strapi](https://github.com/strapi/strapi) that provides the ability to auto slugify a field for any content type. It also provides a findOne by slug endpoint as a utility. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +The installation requirements are the same as Strapi itself and can be found in the documentation on the [Quick Start](https://strapi.io/documentation/developer-docs/latest/getting-started/quick-start.html) page in the Prerequisites info card. |
| 8 | + |
| 9 | +### Supported Strapi versions |
| 10 | + |
| 11 | +- v4.x.x |
| 12 | + |
| 13 | +**NOTE**: While this plugin may work with the older Strapi versions, they are not supported, it is always recommended to use the latest version of Strapi. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```sh |
| 18 | +npm install strapi-plugin-slugify |
| 19 | +``` |
| 20 | + |
| 21 | +**or** |
| 22 | + |
| 23 | +```sh |
| 24 | +yarn add strapi-plugin-slugify |
| 25 | +``` |
| 26 | + |
| 27 | +## Configuration |
| 28 | + |
| 29 | +The plugin configuration is stored in a config file located at `./config/plugins.js`. |
| 30 | + |
| 31 | +A sample configuration |
| 32 | + |
| 33 | +```javascript |
| 34 | +module.exports = ({ env }) => ({ |
| 35 | + slugify: { |
| 36 | + enabled: true, |
| 37 | + config: { |
| 38 | + contentTypes: { |
| 39 | + article: { |
| 40 | + field: 'slug', |
| 41 | + references: 'title', |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +This will listen for any record created or updated in the article content type and set a slugified value for the slug field automatically based on the title field. |
| 50 | + |
| 51 | +**IMPORTANT NOTE**: Make sure any sensitive data is stored in env files. |
| 52 | + |
| 53 | +### The Complete Plugin Configuration Object |
| 54 | + |
| 55 | +| Property | Description | Type | Default | Required | |
| 56 | +| -------- | ----------- | ---- | ------- | -------- | |
| 57 | +| contentTypes | The Content Types to add auto slugification and search findOne by slug search utility to | Object | {} | No | |
| 58 | +| contentTypes[modelName] | The model name of the content type (it is the `singularName` in the [model schema](https://docs.strapi.io/developer-docs/latest/development/backend-customization/models.html#model-schema)) | String | N/A | Yes | |
| 59 | +| contentTypes[modelName]field | The name of the field to add the slug | String | N/A | Yes | |
| 60 | +| contentTypes[modelName]references | The name of the field that is used to build the slug | String | N/A | Yes | |
| 61 | +| slugifyOptions | The options to pass the the slugify function. All options can be found in the [slugify docs](https://github.com/sindresorhus/slugify#api) | Object | {} | No | |
| 62 | + |
| 63 | +## Usage |
| 64 | + |
| 65 | +Once the plugin has been installed, configured and enabled the configured content types will have the following additional functionality |
| 66 | + |
| 67 | +### Slugification |
| 68 | + |
| 69 | +Any time the respective content types have an entity created or updated the slug field defined in the settings will be auto generated based on the provided reference field. |
| 70 | + |
| 71 | +### Find One by Slug |
| 72 | + |
| 73 | +Hitting the `/api/slugify/slugs/:modelName/:slug` endpoint for any configured content types will return the entity type that matches the slug in the url. |
| 74 | + |
| 75 | +#### Additional Requirements |
| 76 | + |
| 77 | +Like all other created API endpoints the `findSlug` route must be allowed under `User & Permissions -> Roles -> Public/Authenticated` for the user to be able to access the route. |
| 78 | + |
| 79 | +#### Example Request |
| 80 | + |
| 81 | +Making the following request with the sample configuration will look as follows |
| 82 | + |
| 83 | +```js |
| 84 | +await fetch(`${API_URL}/api/slugify/slugs/article/lorem-ipsum-dolor`); |
| 85 | +// GET /api/slugify/slugs/article/lorem-ipsum-dolor |
| 86 | +``` |
| 87 | + |
| 88 | +#### Example Response |
| 89 | + |
| 90 | +If an article with the slug of `lorem-ipsum-dolor` exists the reponse will look the same as a single entity response |
| 91 | + |
| 92 | +```json |
| 93 | +{ |
| 94 | + "data": { |
| 95 | + "id": 1, |
| 96 | + "title": "lorem ipsum dolor", |
| 97 | + "slug": "lorem-ipsum-dolor", |
| 98 | + "createdAt": "2022-02-17T01:49:31.961Z", |
| 99 | + "updatedAt": "2022-02-17T03:47:09.950Z", |
| 100 | + "publishedAt": null |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +**IMPORTANT NOTE** To be inline with Strapi's default behaviour for single types if an article with the slug of `lorem-ipsum-dolor` does not exist a 404 error will be returned. |
| 106 | + |
| 107 | +```json |
| 108 | +{ |
| 109 | + "data": null, |
| 110 | + "error": { "status": 404, "name": "NotFoundError", "message": "Not Found", "details": {} } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Bugs |
| 115 | + |
| 116 | +If any bugs are found please report them as a [Github Issue](https://github.com/ComfortablyCoding/strapi-plugin-slugify/issues) |
0 commit comments