#462 fix, remove leading and trailing dashes from urlized slug. includes test changes
authorJoel Scoble <joel.scoble@outlook.com>
Thu, 2 Oct 2014 22:25:52 +0000 (17:25 -0500)
committerspf13 <steve.francia@gmail.com>
Tue, 7 Oct 2014 21:05:11 +0000 (17:05 -0400)
hugolib/page_test.go
hugolib/permalinks.go

index b8383f5027226ec9b4da74a687c0d23e8d4dda2d..67c1270f322d770253c01882f3146eaee86aabcc 100644 (file)
@@ -46,7 +46,7 @@ Leading
     "Development",
     "VIM"
 ],
-"slug": "spf13-vim-3-0-release-and-new-website"
+"slug": "-spf13-vim-3-0-release-and-new-website-"
 }
 
 Content of the file goes Here
@@ -566,13 +566,13 @@ func TestLayoutOverride(t *testing.T) {
 }
 
 func TestSliceToLower(t *testing.T) {
-       tests := []struct{
-               value []string
+       tests := []struct {
+               value    []string
                expected []string
        }{
-               {[]string{"a","b","c"}, []string{"a", "b", "c"}},
-                {[]string{"a","B","c"}, []string{"a", "b", "c"}},
-                {[]string{"A","B","C"}, []string{"a", "b", "c"}},
+               {[]string{"a", "b", "c"}, []string{"a", "b", "c"}},
+               {[]string{"a", "B", "c"}, []string{"a", "b", "c"}},
+               {[]string{"A", "B", "C"}, []string{"a", "b", "c"}},
        }
 
        for _, test := range tests {
index ba0842cfbbb564a740f14ca6baac46cd679accff..24e3334bbde9ac6a6beaf1af0bc6e7b07dcf03f7 100644 (file)
@@ -128,6 +128,14 @@ func pageToPermalinkFilename(p *Page, _ string) (string, error) {
 // if the page has a slug, return the slug, else return the title
 func pageToPermalinkSlugElseTitle(p *Page, a string) (string, error) {
        if p.Slug != "" {
+               // Don't start or end with a -
+               if strings.HasPrefix(p.Slug, "-") {
+                       p.Slug = p.Slug[1:len(p.Slug)]
+               }
+
+               if strings.HasSuffix(p.Slug, "-") {
+                       p.Slug = p.Slug[0 : len(p.Slug)-1]
+               }
                return p.Slug, nil
        }
        return pageToPermalinkTitle(p, a)