Skip to content

Extended browse method to support optional file path #28

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

Merged
merged 5 commits into from
Mar 7, 2018

Conversation

neocotic
Copy link
Contributor

@neocotic neocotic commented Oct 2, 2017

I want to be able to generate URLs for non-raw files. This ability will be useful for links to formatted docs and/or syntax highlighted source files. Personally, I'm looking for the former; I want to generate URLs for viewing docs hosted on GitHub (or anywhere) based on file paths relative to the directory of a cloned Git repository. However, I believe that this ability will also benefit others.

I've included support for all existing services. Gists were the only tricky one as there doesn't appear to be a URL for viewing a single Gist file in the browser, only single raw files. However, the URL generated for Gists does work but it has to use a hash fragment, which required some regexp replacing on the path as non-[a-zA-Z0-9_] characters are replaced with hyphens and the / character is not allowed at all (which makes sense since Gists can only have a flat directory structure) and also transformed to lower case. While doing this work, I've also added the ability to specify hash fragments for files. The fragment option is supported by all except Gists, as they're already using the hash fragment for the file path itself.

I've tried to avoid introducing a breaking change by simply extending the existing browse method to accept a path as an optional first parameter. Basically, allowing browse to be used as follows:

info.browse(opts)
//=> "https://github.com/npm/hosted-git-info/tree/v1.2.0"
info.browse('package.json', opts)
//=> "https://github.com/npm/hosted-git-info/tree/v1.2.0/package.json"
info.browse('README.md', 'Supported hosts', opts)
//=> "https://github.com/npm/hosted-git-info/tree/v1.2.0/REAMDE.md#supported-hosts"

I've also updated the documentation for browse in README.md, but I don't know if you want me to expand further on the optional aspect of the path and fragment parameters.

Copy link
Contributor

@zkat zkat left a comment

Choose a reason for hiding this comment

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

LGTM! Hope this is useful to you!

@zkat zkat merged commit cd5e5bb into npm:master Mar 7, 2018
@neocotic neocotic deleted the browsefile branch March 8, 2018 09:48
@neocotic neocotic restored the browsefile branch March 8, 2018 10:08
@neocotic
Copy link
Contributor Author

neocotic commented Mar 8, 2018

@zkat Thanks for this! It's a great help. I always like to avoid GitHub branch dependencies in my npm packages, where possible :)

@neocotic neocotic deleted the browsefile branch May 17, 2022 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants