Skip to content

Extended browse method to support optional file path#28

Merged
zkat merged 5 commits intonpm:masterfrom
neocotic:browsefile
Mar 7, 2018
Merged

Extended browse method to support optional file path#28
zkat merged 5 commits intonpm:masterfrom
neocotic:browsefile

Conversation

@neocotic
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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