1.1.1 • Published 9 years ago

gh-md-urls v1.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

gh-md-urls

unstable

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

NPM

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 URL
  • raw (Boolean) if true, all links are resolved to their raw.githubusercontent.com/ link instead of a blob
  • branch (String) the branch to resolve to, default master
  • baseUrl (String) if set, prepends hash fragments like #some-section with the given base URL (non-string values default to repository 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.