decaporg / decap-cms

A Git-based CMS for Static Site Generators
https://decapcms.org
MIT License
17.86k stars 3.04k forks source link

Make sluggification visible, customizable, and fail-safe #445

Open erquhart opened 7 years ago

erquhart commented 7 years ago

Default sluggification is simplistic, relying on a title field and performing no inference whatsoever.

With no field named "title", entries currently fail to save.

The ideal fix is to simply make slugs editable in the editor UI.

Considerations:

erezrokah commented 3 years ago

Something crossed my mind. With netlify you can create a _redirects file in root. May be complicated. But if Netlify CMS could manage a file like that. Then changing the slug wouldnt be a problem with already shared links. Just a crazy thought.

Not crazy at all, but I don't think this should be done by a content management system as it's very specific to how the site is built and deployed. Perhaps a Netlify Build Plugin.

karllhughes commented 3 years ago

@hanneskuettner's solution looks great, but I'm guessing it's not implemented yet? When I tried it, I got this error in my editor:

No control for widget 'slug'.

Is this widget something I need to register separately?

jmayergit commented 3 years ago

To further @hanneskuettner's example above,

widget

// index.html

<script>
  var SlugControl = createClass({
    getInitialState: function () {
      return { touched: false }
    },

    handleChange: function (e) {
      this.props.onChange(e.target.value)
      !this.state.touched && this.state.touched = true
    },

    render: function () {
      return h('input', {
        id: this.props.forID,
        className: this.props.classNameWrapper,
        type: 'text',
        value: this.state.touched ? this.props.value : this.props.entry.get('slug'),
        onChange: this.handleChange,
      })
    },
  })

  var SlugPreview = createClass({
    render: function () {
      return null // or show a preview
    },
  })

  var schema = {
    properties: {},
  }

  CMS.registerWidget('slug', SlugControl, SlugPreview, schema)
</script>

config

// config.yml

    slug: '{{fields.slug}}'
    fields:
      - { label: 'slug', name: 'slug', widget: slug, hint: 'slug' }

comparison with hanneskuettner's implementation

  • Have a slug value field with a custom slug widget (that's basically a string widget) that can be used for the entry slug with fields.slug 🙆
  • The value in slug is automatically updated and correctly slugified according to the slug pattern and the root slug configuration 🙅
  • Once an entry is saved the slug is no longer updated when the dependent fields are updated 🙅
  • The user can manually update the slug but receives a warning that this might break existing URLs since it is a slug field 🙅

âž• works with previously created entries

Notes

... slug: 'my-slug-template-{{fields.slug}}' fields:

// index.html

...

  handleChange: function (e) {
    // use the schema property in your handler function
    // this.props.field.get('mySchemaProperty')
  }, 

...

var schema = {
  properties: {
    mySchemaProperty: { type: 'string' },
  },
}
timtorres commented 3 years ago

Something crossed my mind. With netlify you can create a _redirects file in root. May be complicated. But if Netlify CMS could manage a file like that. Then changing the slug wouldnt be a problem with already shared links. Just a crazy thought.

Not crazy at all, but I don't think this should be done by a content management system as it's very specific to how the site is built and deployed. Perhaps a Netlify Build Plugin.

Shopify does this behind the scenes pretty nicely. It edits a redirects file, adding a new entry every time a slug is changed. There's at least one 3rd party app that extends this by adding a redirects management UI. Scans for broken links or something like that and lets you manage multiple aliases for a single url.

But I do agree with you that the CMS itself should remain divorced from that in order to support different setups.

erezrokah commented 3 years ago

Shopify does this behind the scenes pretty nicely. It edits a redirects file, adding a new entry every time a slug is changed. There's at least one 3rd party app that extends this by adding a redirects management UI. Scans for broken links or something like that and lets you manage multiple aliases for a single url.

I think https://github.com/sw-yx/netlify-plugin-no-more-404 is supposed to do some of that, but seeking maintainers.

ryangittings commented 2 months ago

Any update on this? It's a constant issue for editors for me that they can't go and control slugs of their blog posts for example.

martinjagodic commented 2 months ago

Not really, this is a complex feature and so far we haven't gotten to it. The best way to start would be a pull request :)