1.0.8 • Published 10 months ago

@x3m-group/strapi-plugin-auto-links v1.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

strapi-plugin-auto-links

A plugin for Strapi that auto generate links for resources (self, canonical, alternates, ...)

Downloads Install size Package version

Requirements

The installation requirements are the same as Strapi itself and can be found in the documentation on the Quick Start page in the Prerequisites info card.

Supported Strapi versions

  • v4.x.x

Installation

npm install @x3m-group/strapi-plugin-auto-links

# or

yarn add @x3m-group/strapi-plugin-auto-links

Configuration

The plugin configuration is stored in a config file located at ./config/plugins.js or ./config/plugins.ts.

A minimal configuration

module.exports = ({ env }) => ({
  // ...
  "auto-links": {
    enabled: true,
    config: {
      contentTypes: {
        article: {},
      },
    },
  },
  // ...
});

Custom Canonical configuration

module.exports = ({ env }) => ({
  // ...
  "auto-links": {
    enabled: true,
    config: {
      canonical: {
        host: "https://{item.locale}.example.com",
        path: "/resources/{model.info.pluralName}/{item.slug}",
      },
      contentTypes: {
        author: {},
        article: {},
        tag: {
          canonical: {
            host: "https://tags.example.com",
            path: "/{item.slug}?q=all",
          },
        },
      },
    },
  },
  // ...
});

Example result

http://localhost:1337/api/articles?populate=*

{
  "data": [
    {
      "id": 1,
      "attributes": {
        "title": "My First Article English",
        "slug": "my-first-article-english",
        "locale": "en",
        "localizations": {
          "data": [
            ...
          ]
        },

        // newly auto generated links
        "links": {
          "self": "http:/localhost:1337/api/articles/1",
          "canonical": "https:/example.com/articles/my-first-article-english",
          "alternates": [
            {
              "locale": "nl",
              "self": "http:/localhost:1337/api/articles/2",
              "canonical": "https:/example.com/articles/mijn-eerste-artikel-nederlands"
            }
          ]
        }
        // ...

      }
    }
  ],
  "meta": {
    ...
  }
}

Configuration Object

PropertyDescriptionTypeDefaultRequired
selfglobal self configurationObject{}No
self.enabledenable self url renderingBooleantrueNo
self.hostdefault global self host patternStringenv SELF_HOST or http://localhost:1337No
self.pathdefault global self path patternString/api/{model.info.pluralName}/{item.id}No
canonicalglobal canonical configurationObject{}No
canonical.enabledenable canonical url renderingBooleantrueNo
canonical.hostdefault global canonical host patternStringenv CANONICAL_HOST or https://example.comNo
canonical.pathdefault global canonical path patternString/{item.locale}/{model.info.pluralName}/{item.slug}No
alternatesglobal alternates configurationObject{}No
alternates.enabledenable alternates url rendering (localized links)BooleantrueNo
contentTypesThe Content Types to add auto linksObject{}No
contentTypesmodelNameThe model name of the content type (it is the singularName in the model schema)Object{}Yes
contentTypesmodelName.selfoverride global self configurationObject{}No
contentTypesmodelName.self.enabledenable self url renderingBooleantrueNo
contentTypesmodelName.self.hostself host patternStringglobal valueNo
contentTypesmodelName.self.pathself path patternStringglobal valueNo
contentTypesmodelName.canonicaloverride global canonical configurationObject{}No
contentTypesmodelName.canonical.enabledenable canonical url renderingBooleantrueNo
contentTypesmodelName.canonical.hostcanonical host patternStringglobal valueNo
contentTypesmodelName.canonical.pathcanonical path patternStringglobal valueNo
contentTypesmodelName.alternatesoverride global alternates configurationObject{}No
contentTypesmodelName.alternates.enabledenable alternates url rendering (localized links)BooleantrueNo

Context Vars

PropertyDescriptionType
itemthe current resource itemObject
item.*all available properties of the resource item (must be selected/populated)Any
modelthe current resource schema informationObject
model.kinddefines if the content-type is collectionType or singleTypeString
model.collectionNamedatabase table name in which the data should be storedString
model.infostrapi model informationObject
model.info.displayNamedefault name to use in the admin panelString
model.info.singularNamesingular form of the content-type nameString
model.info.pluralNameplural form of the content-type nameString