gh-md-urls
v1.1.1
Published
find and resolve URLs in GitHub Markdown
Downloads
15
Maintainers
Readme
gh-md-urls
Finds image and link URLs from a GitHub markdown source and optionally resolves relative paths to an absolute GitHub URL.
Install
npm install gh-md-urls --save
Example
Take the following readme.md
file:
## header
![image](images/image.png)
Hello [google](http://google.com)!
A [section](#relative) link.
Now running the this on the above source:
var getUrls = require('gh-md-urls')
var fs = require('fs')
var src = fs.readFileSync(__dirname + '/readme.md', 'utf8')
var urls = getUrls(src, {
repository: 'https://github.com/mattdesl/gh-md-urls'
})
Result of urls
:
[
{ type: 'image',
url: 'https://raw.githubusercontent.com/mattdesl/gh-md-urls/master/images/image.png',
alt: 'image'
}, {
type: 'link',
url: 'http://google.com',
text: 'google'
}, {
type: 'link',
url: 'https://github.com/mattdesl/gh-md-urls#relative',
text: 'section'
}
]
Usage
urls = getUrls(markdown, [opt])
Returns a list of URL nodes representing all Markdown links in the source String or File markdown
. Options:
repository
(String) if set, resolves all relative paths to absolute paths based on this repository URLraw
(Boolean) if true, all links are resolved to theirraw.githubusercontent.com/
link instead of a blobbranch
(String) the branch to resolve to, default masterbaseUrl
(String) if set, prepends hash fragments like#some-section
with the given base URL (non-string values default torepository
option)
HTML image/anchors are not parsed.
Nodes
"image"
The node is an image: ![image](foo/image.png)
{
type: 'image',
url: 'foo/image.png',
alt: 'image'
}
"link"
(text)
The node is a text link: [foo](#blah)
{
type: 'link',
text: 'foo',
url: '#blah'
}
"link"
(image)
The node is a link containing an image node: [![title](image.png)](#foo)
{
type: 'link',
url: '#foo',
image: {
type: 'image'
url: 'image.png',
alt: 'title'
}
}
"reference"
The node is a reference/definition/footer:
[Some text][1] and stuff.
[1]: http://google.com/
{
type: 'reference',
text: 'Some text',
identifier: '1',
url: 'http://google.com/'
}
License
MIT, see LICENSE.md for details.