Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@benbalter
Copy link
Contributor

This PR exposes a github_edit_link tag that generates links to edit the current page on GitHub.

To generate a link

<p>This site is open source. {% github_edit_link "Improve this page" %}</p>

Produces:

<p>This site is open source. <a href="https://github.com/benbalter/jekyll-edit-link/edit/master/README.md">Improve this page</a></p>

To generate a path

If you'd prefer to build your own link, simply don't pass link text

<p>This site is open source. <a href="{% github_edit_link %}">Improve this page</a></p>

Produces:

<p>This site is open source. <a href="https://github.com/benbalter/jekyll-edit-link/edit/master/README.md">Improve this page</a></p>

In order to add the tag, I made to related changes:

  • Broke the various spec helpers into spec/spec_helpers/foo_helper.rb to keep spec/spec_helpers.rb more managable

  • Broke the README into individual documents, and moved to the docs/ folder since adding the edit tag docs made the README a bit unwieldy.

@benbalter benbalter self-assigned this Aug 17, 2017
@benbalter benbalter requested a review from parkr August 17, 2017 15:21
Copy link
Member

@parkr parkr left a comment

Choose a reason for hiding this comment

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

Sweet! Just a handful of comments here. 😄

# Returns a symbol representing the instance method
def self.def_hash_delegator(hash_method, key, method, default = nil)
define_method(method) do
hash = send(hash_method)
Copy link
Member

Choose a reason for hiding this comment

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

Can we use public_send here instead of send?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we did, we'd have to make site, site_github, source, and page public.

end

def remove_leading_slash(part)
part.sub(%r!\A/!, "")
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about part.start_with?("/") ? part[1..-1] : part?

>> path = "/lol"
=> "/lol"
>> Benchmark.ips do |x|
?> x.report ("sub") { path.sub(%r!\A/!, "") }
>> x.report ("[1..-1]") { path.start_with?("/") ? path[1..-1] : path }
>> x.compare!
>> end
Warming up --------------------------------------
                 sub   134.174k i/100ms
             [1..-1]   185.434k i/100ms
Calculating -------------------------------------
                 sub      1.995M (± 5.5%) i/s -     10.063M in   5.058157s
             [1..-1]      3.626M (± 6.7%) i/s -     18.173M in   5.035788s

Comparison:
             [1..-1]:  3626032.7 i/s
                 sub:  1995248.9 i/s - 1.82x  slower

def parts_normalized
@parts_normalized ||= parts.map.with_index do |part, index|
part = remove_leading_slash(part.to_s)
part = ensure_trailing_slash(part) unless index == parts.length - 1
Copy link
Member

Choose a reason for hiding this comment

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

What does this unless mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the url is https://github.com/foo/bar/edit/master/page.md, we don't want the last element page.md to be given a trailing slash (and can't rely on the extension, because the repo might be, foo.js).

Copy link
Member

Choose a reason for hiding this comment

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

Could you leave a comment in the code to that effect or give it a tiny helper method which explains that?

end

def link
"<a href=\"#{uri.normalize}\">#{link_text}</a>"
Copy link
Member

Choose a reason for hiding this comment

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

You call uri.normalize but you return a "" in an error state. I don't see a String#normalize, so normalize will have to be called in uri instead of here.

docs/README.md Outdated

## What it does

* Propegates the `site.github` namespace with [repository metadata](https://help.github.com/articles/repository-metadata-on-github-pages/)
Copy link
Member

Choose a reason for hiding this comment

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

Propagates

?


### 2. `~/.netrc`

If you prefer to use the good ol' `~/.netrc` file, just make sure the `netrc` gem is bundled and run `jekyll` like normal. So if I were to add it, I'd add `gem 'netrc'` to my `Gemfile`, run `bundle install`, then run `bundle exec jekyll build`. The `machine` directive should be `api.github.com`.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe I should include an example...

machine api.github.com
    login github-username
    password 123abc-your-token

@@ -0,0 +1,29 @@

Copy link
Member

Choose a reason for hiding this comment

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

empty space!

}
end

def make_page(options = {})
Copy link
Member

Choose a reason for hiding this comment

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

why not call this data or page_data if it's only every being used as page.data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's used both in the fixture helper and in the edit link tag spec to create the context, which needs the page object, not the page data.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I meant the argument. s/options/data/. Sorry for the confusion!

end

def make_page(options = {})
page = Jekyll::Page.new site, config_defaults["source"], "", "page.md"
Copy link
Member

Choose a reason for hiding this comment

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

parens around the 4 arguments to .new would help readability here 👀


def make_page(options = {})
page = Jekyll::Page.new site, config_defaults["source"], "", "page.md"
page.data = options
Copy link
Member

Choose a reason for hiding this comment

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

you could use .tap

Jekyll::Page.new(site, config_defaults["source"], "", "page.md").tap { |page| page.data = data }

returns the Page object

@benbalter
Copy link
Contributor Author

@parkr thanks for the feedback. I believe I've implemented your suggestions and answered your questions to the best of my ability. Mind taking another look? 😄

@benbalter
Copy link
Contributor Author

@jekyllbot: merge

@jekyllbot jekyllbot merged commit e195c17 into master Aug 28, 2017
@jekyllbot jekyllbot deleted the github-edit-link branch August 28, 2017 17:33
jekyllbot added a commit that referenced this pull request Aug 28, 2017
@DirtyF
Copy link
Member

DirtyF commented Aug 28, 2017

I host my blog on Netllify. My default branch is master, my _config.ymlalso has branch:masterand here is the link that gets generated when testing on my machine:

`https://github.com/DirtyF/frank.taillandier.me/edit/gh-pages/_posts/2017-05-21-sept-ans-de-sud-web.md``

It has gh-pages branch instead of master 🤔

@benbalter
Copy link
Contributor Author

@DirtyF I believe the following should work in your config:

github:
  source:
    branch: master

@DirtyF
Copy link
Member

DirtyF commented Aug 29, 2017

I hoped so but something is wrong:

GitHub API is returning master

$ http --body https://api.github.com/repos/DirtyF/frank.taillandier.me/branches
[
    {
        "commit": {
            "sha": "15a70597f9aee29fcb4c7ae354489b0b560230e8",
            "url": "https://api.github.com/repos/DirtyF/frank.taillandier.me/commits/15a70597f9aee29fcb4c7ae354489b0b560230e8"
        },
        "name": "master"
    }
]

Whether {{ site.github.source.branch }} is returning gh-pages. 😲

@benbalter
Copy link
Contributor Author

The tag uses site.github.source.branch which comes from https://github.com/jekyll/github-metadata/blob/master/lib/jekyll-github-metadata/metadata_drop.rb#L61, which itself is delegated to https://github.com/jekyll/github-metadata/blob/master/lib/jekyll-github-metadata/repository.rb#L125-L127 which will either be the source as returned from the pages endpoint, or if it can't hit that endpoint, gh-pages for user pages.

If site.github is a hash, we merge the drop (the logic above) with the user-supplied value. It could be that Jekyll's deep_merge_hashes isn't recursive enough, but in theory, if you supply site.github.source.branch it should prefer that over the calculated value.

@DirtyF
Copy link
Member

DirtyF commented Aug 29, 2017

Thanks for taking the time to explain how this works under the hood. 🔧

in theory, if you supply site.github.source.branch it should prefer that over the calculated value.

Well, as I said, In my case, it returns the default value gh-pages, even with the values manually added in the _config.yml.

@benbalter
Copy link
Contributor Author

@DirtyF You found a 🐛 🐞! Fix in progress via #110.

@jekyll jekyll locked and limited conversation to collaborators Apr 28, 2019
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.

5 participants