webhook/webhook-cms

Slug not written to Firebase

kenzik opened this issue · 3 comments

I posted this in the forum, but thought it should be here for easier tracking. I've created a very simple API to retrieve my data directly from Firebase, and it uses the slug for lookups, so I'm stuck until it's resolved. I'll try to dig into it this weekend:

While working on direct access to my site's content via Firebase, I noticed that the content type slug is not written to Firebase when the content type entry is saved. It appears to only write the slug after it is modified.

Here is a quick video demonstrating what I saw: http://quick.as/7wd6sg4xm

PR sent.

One thing to note is that since this will set the item.slug.value, it will no longer mirror changes made to item.name on subsequent edits.

Hey @dkenzik, if you do not edit the slug, we don't save anything in firebase and we automatically generate the slug for each piece of content when you build your site. We generate a preview of the slug in the CMS to let the user know what the URL will be.

Here is the relevant code: https://github.com/webhook/webhook-cms/blob/master/app/controllers/wh/content/type/edit.js#L70-L84

  setDefaultSlug: function () {

    if (Ember.isEmpty(this.get('nameControl.value')) || Ember.isEmpty(this.get('type.id')) || !Ember.isEmpty(this.get('slugControl.value'))) {
      this.set('defaultSlug', null);
      return;
    }

    var slug = slugger({
      name: this.get('nameControl.value'),
      publish_date: (Ember.isEmpty(this.get('publishDate')) ? moment() : moment(this.get('publishDate'))).format()
    }, this.get('type.id'), this.get('type.customUrls'));

    this.set('defaultSlug', slug);

  }.observes('nameControl.value', 'type.id', 'slugControl.value'),

and https://github.com/webhook/webhook-cms/blob/master/app/utils/slugger.js

/*global uslug*/
export default function slugger (item, type, customUrls) {
  var tmpSlug = '';
  tmpSlug = uslug(item.name).toLowerCase();

  if(customUrls && customUrls.individualUrl) {
    tmpSlug = parseCustomUrl(customUrls.individualUrl, item, type) + '/' + tmpSlug;
  } 

  if(customUrls && customUrls.listUrl) {
    tmpSlug = customUrls.listUrl + '/' + tmpSlug;
  } else {
    tmpSlug = type + '/' + tmpSlug;
  }

  return tmpSlug;
}

function parseCustomUrl (url, object, type) {
  var publishDate = object.publish_date ? object.publish_date : object;

   publishDate = moment(publishDate);

  function replacer(match, timeIdent, offset, string){
    if(timeIdent === 'Y') {
      return publishDate.format('YYYY').toLowerCase();
    } else if (timeIdent === 'y') {
      return publishDate.format('YY').toLowerCase();
    } else if (timeIdent === 'm') {
      return publishDate.format('MM').toLowerCase();
    } else if (timeIdent === 'n') {
      return publishDate.format('M').toLowerCase();
    } else if (timeIdent === 'F') {
      return publishDate.format('MMMM').toLowerCase();
    } else if (timeIdent === 'M') {
      return publishDate.format('MMM').toLowerCase();
    } else if (timeIdent === 'd') {
      return publishDate.format('DD').toLowerCase();
    } else if (timeIdent === 'j') {
      return publishDate.format('D').toLowerCase();
    } else if (timeIdent === 'T') {
      return type.toLowerCase();
    } else {
      return match;
    }
  }

  url = url.replace(/#(\w)/g, replacer);

  return url;
}

If you need non-custom slugs you'll have to mimic this code. Let me know if that makes sense.

@gpbmike Thanks for the confirmation! I found that code last week and am using it for my project:

https://github.com/dkenzik/webhook-simple-api