Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

slugify all path components #39

Open
waynedpj opened this Issue · 11 comments

3 participants

@waynedpj

ahoy,

wondering if it would be possible to use this plugin to slugify the entire permalink path instead of just the basename as is currently done?

i am still trying to get replacements to work to try and do it that way, but i imagine that this is a common enough request that it could be an option.

thanks

@jonschlinkert

If you slugify the entire path that would turn all of the slashes/directories into dashes. So foo/Bar Baz/quux.html would turn into foo-bar-baz-quux.html.

So I'm guessing what you want to do is slugify each path segment. Either way, we're in luck! Any custom pattern is possible using custom replacement patterns:

options: {
  permalinks: {
    structure: ':foo/:bar/:baz/:slug:ext',
    patterns: [
      {
        pattern: /:\b(foo|bar|baz)\b/
        replacement: function (match) {
          var _str = require('underscore.string');
          return _str.slugify(match);
        }
      }
    ]
  }
}
@waynedpj

you want to do is slugify each path segment

bingo. it is a bad sign when i am not even asking the right question! thanks for divining my intent.

i am trying your suggestion but using the following setup the page title is not being substituted, instead all pages are written to page/title/index.html (i.e. the word "title" is not being replaced but written as is):

            # the 'pages' collection
            pages:
                options:
                    layout: "content.html"
                    permalinks:
                        slugify: true
                        structure: ":title/index.html"
                        patterns: [
                            pattern: /:\b(title)\b/
                            replacement: (match) ->
                                _str = require "underscore.string"
                                grunt.verbose.ok "match = " + match
                                _str.slugify match
                            ]

i have tried different variations all with the same result. my test repo is https://github.com/waynedpj/Assemble-playground/ where you can see the full setup.

i am also having trouble using permalinks to generate the tracks permalink path as release/<release title slugified>/track/<track title slugified>/index.html under the correct release. however, i think that can be solved once i have the replacement pattern working.

finally, i still think an option to slugify all path components/segments would be very useful, especially since (if i am following correctly .. which most likely i am not!) currently i would have to write a pattern+replacement for each unique collection's path, which seems like a lot of duplication when i just would like all output to be slugified. again, maybe there is a more general 'slugify everything' patter+replacement combo that i am not realizing that could be used in assemble.options.permalink as a default.

thanks as always.

@jonschlinkert

arg, coffescript! lol jk. K I'll take a look at it

@waynedpj

woops, i apologize. i thought it would be OK as i have seen some CoffeeScript around your repos, and this issue. i like YAML+Ruby for readability (and am not a big JavaScript fan) so CoffeeScript is my goto right now for JS projects. that being said the site http://js2coffee.org/ makes good conversions . here is a quick convert of my Gruntfile to JS https://github.com/waynedpj/Assemble-playground/blob/Gruntfile.js/Gruntfile.js

sorry for the trouble

@doowb
Owner

FWIW, I'm in the process of updating this plugin to use our new strings library and in most places I have a slugify option that will slugify the value no matter what it is. However, this will only work on segments that have a replacement pattern. It won't slugify the entire path.

@jonschlinkert

woops, i apologize.

I was just kidding around! I @doowb and I use it sparingly, and the handlebars-helpers repo used to be coffee. please continue using whatever you like using, I'm totally comfortable reading coffee. it has become more of an inside joke, so my apologies for making you feel that way about it. and thanks for being so gracious

@jonschlinkert

However, this will only work on segments that have a replacement pattern. It won't slugify the entire path.

I was about to implement that in this plugin, but that's great! I forgot that you did that.

this will only work on segments that have a replacement pattern. It won't slugify the entire path.

we can add this pretty easily if we agree that dashes should be used in lieu of characters that are replaced (which is recommended for Google SEO anyway). All we would have to do is split the paths by path.sep, slugify each segment and then join them again. but would this belong in the Strings lib or in a lib that depends on strings (like this plugin)?

Meaning it seems like Strings would only have knowledge of the strings (in this case, segments) that are being transformed with replacement patterns, and won't have any knowledge of a "complete path". technically the whole path could be a replacement pattern, but this seems to not be an ideal use case for Strings.

@waynedpj

I was just kidding around!

@jonschlinkert no worries. i realized you were kidding but also did not want to give you more work than my myriad posts already have ;)

@doowb
Owner

I thought I replied to this the other day... must have only done it in my mind ;)

but this seems to not be an ideal use case for Strings

Agreed. I'm going to merge in my changes with strings and after the permalink is calculated, we can have a flag to slugify the entire URL by doing something like...

permalink = _.map(permalink.split('/'), _str.slugify).join('/');

I tried passing options.slugify = true to the dates middleware on strings and it would remove all the / as @jonschlinkert mentioned above.

@waynedpj

@doowb OK, sounds good. i think that option to slugify the entire URL would be very useful. i would be happy to test it when ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.