Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow placeholders in permalinks #3031

Merged
merged 3 commits into from
Oct 29, 2014

Conversation

alfredxing
Copy link
Member

Allow the regular placeholders (basename, path, ext) in permalinks. This is useful for setting permalink rules in frontmatter defaults (for example if I want my SCSS files in /assets/scss/ to output to /assets/css/).

Changes will only break functionality for people currently using :path, :ext, :basename, etc. in their permalinks. Permalinks without any of these special symbols will remain the same.

Ref. #2475, #1712.

@placeholders.inject(@permalink) do |result, token|
break result if result.index(':').nil?
result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This smells of duplication!

@parkr
Copy link
Member

parkr commented Oct 27, 2014

👍

@alfredxing
Copy link
Member Author

That should be better.

#
# Returns the _unsanitizied_ String URL
def generate_url
@placeholders.inject(@template) do |result, token|
@placeholders.inject(@permalink || @template) do |result, token|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's restructure this. #generate_url should take a parameter – a template – that it uses as the first argument to #inject. That way we can have #generated_permalink and #generated_url use and memoize the permalink and template, respectively.

def to_s
  generated_permalink || generated_url
end

def generated_permalink
  (@generated_permalink ||= generate_url(@permalink)) if @permalink
end

def generated_url
  @generated_url ||= generate_url(@template)
end

def generate_url(template)
  @placeholders.inject(template) do |result, token|
    # etc...
  end
end

@alfredxing
Copy link
Member Author

Done!

@parkr parkr merged commit 8dcf7a6 into jekyll:master Oct 29, 2014
parkr added a commit that referenced this pull request Oct 29, 2014
@parkr
Copy link
Member

parkr commented Oct 29, 2014

/cc @benbalter – would love your 👀

@kleinfreund
Copy link

In the 2.5.0 post what is :name front matter permalinks referring to? Atleast there is nothing on the permalink page regarding this.

@parkr
Copy link
Member

parkr commented Nov 11, 2014

what is :name front matter permalinks referring to

Depends. For Page, nothing. For Post, nothing. For Document, it's a slugified version of the file basename without the extension.

We can use title or something more universal in the release post if you prefer?

@jekyll jekyll locked and limited conversation to collaborators Feb 27, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants