diff --git a/lib/index.coffee b/lib/index.coffee index 5ae5607..b7dc248 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -1,3 +1,4 @@ +_ = require 'lodash' fm = require 'front-matter' marked = require 'marked' yaml = require 'js-yaml' @@ -246,6 +247,7 @@ module.exports = (dirtyMarkdown, options = {}) -> url: value.href title: value.title or null ) + links = _.sortBy(links, ['name', 'url']) html = marked.parser(ast) diff --git a/test/expected/marked-readme.md b/test/expected/marked-readme.md index 75983cd..766834e 100644 --- a/test/expected/marked-readme.md +++ b/test/expected/marked-readme.md @@ -336,10 +336,10 @@ Copyright (c) 2011-2014, Christopher Jeffrey. (MIT License) See LICENSE for more info. +[badge]: http://badge.fury.io/js/marked +[breaks]: https://help.github.com/articles/github-flavored-markdown#newlines [gfm]: https://help.github.com/articles/github-flavored-markdown [gfmf]: http://github.github.com/github-flavored-markdown/ -[pygmentize]: https://github.com/rvagg/node-pygmentize-bundled [highlight]: https://github.com/isagalaev/highlight.js -[badge]: http://badge.fury.io/js/marked +[pygmentize]: https://github.com/rvagg/node-pygmentize-bundled [tables]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#wiki-tables -[breaks]: https://help.github.com/articles/github-flavored-markdown#newlines diff --git a/test/index.coffee b/test/index.coffee index 5a63c1e..cf9cead 100644 --- a/test/index.coffee +++ b/test/index.coffee @@ -467,6 +467,65 @@ describe 'inline grammar', -> '' ) + it 'should handle reference links', -> + tidyMdSnippet(''' + [NPM version][npm-url] + + [npm-url]: https://npmjs.org/package/npms-analyzer + ''').should.equal(''' + [NPM version][npm-url] + + [npm-url]: https://npmjs.org/package/npms-analyzer + ''') + + it 'should handle shorthand reference links', -> + tidyMdSnippet(''' + [npm-url] + + [npm-url]: https://npmjs.org/package/npms-analyzer + ''').should.equal(''' + [npm-url] + + [npm-url]: https://npmjs.org/package/npms-analyzer + ''') + + it 'should convert links to shorthand reference style', -> + tidyMdSnippet(''' + [npm-url](https://npmjs.org/package/npms-analyzer) + + [npm-url]: https://npmjs.org/package/npms-analyzer + ''').should.equal(''' + [npm-url] + + [npm-url]: https://npmjs.org/package/npms-analyzer + ''') + + it 'should convert links to shorthand reference style', -> + tidyMdSnippet(''' + [npm-url](https://npmjs.org/package/npms-analyzer) + + [npm-url]: https://npmjs.org/package/npms-analyzer + ''').should.equal(''' + [npm-url] + + [npm-url]: https://npmjs.org/package/npms-analyzer + ''') + + it 'should sort reference style links', -> + tidyMdSnippet(''' + Visit [npm] or maybe [David DM][david-dm] or even [Travis] + + [npm]: https://npmjs.org/package/npms-analyzer + [david-dm]: https://david-dm.org/npms-io/npms-analyzer + [travis]: https://travis-ci.org/npms-io/npms-analyzer + ''').should.equal(''' + Visit [npm] or maybe [David DM][david-dm] or even [Travis] + + [david-dm]: https://david-dm.org/npms-io/npms-analyzer + [npm]: https://npmjs.org/package/npms-analyzer + [travis]: https://travis-ci.org/npms-io/npms-analyzer + ''') + it 'should handle images', -> tidyMdSnippet('![text](image.jpg)').should.equal('![text](image.jpg)') tidyMdSnippet('![text]( image.jpg )').should.equal('![text](image.jpg)')