skagedal
skagedal

Reputation: 2411

In Jekyll, how can I programmatically modify permalinks for pages?

In a Jekyll site with many pages (not blog posts), I want to tweak the permalink of each page programatically. I tried a Generator plugin, something like:

module MySite
  class MySiteGenerator < Jekyll::Generator
    def generate(site)
      site.pages.each do |page|
        page.data['permalink'] = '/foo' + page.url
        # real world manipulation of course more complicated
      end
    end
  end
end

But although this would run and set the page.data['permalink'] field, the output was still the same.

Is there something I'm doing wrong, or is there a different way entirely of doing this? Thanks!

Upvotes: 1

Views: 405

Answers (1)

David Jacquel
David Jacquel

Reputation: 52829

It can be easier to override the page class with something like this :

module Jekyll
  class Page
    alias orig_permalink permalink
    def permalink
      permalink    = orig_permalink
      newPermalink = "foo/#{permalink}"
    end
  end
end

Not tested.

Upvotes: 1

Related Questions