Skip to content

Commit

Permalink
implement link conversion and tests
Browse files Browse the repository at this point in the history
closes #21
  • Loading branch information
notslang committed Nov 9, 2015
1 parent 3c61bc9 commit 826d9f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/tidy-inline-markdown.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ tidyInlineMarkdown = (token) ->
.replace CODE_REGEX, (m, code) -> delimitCode(code, '`')
.replace IMG_REGEX, (m, url='', alt='', title) ->
if title?
url += " \"#{title.replace /\\|"/g, (m) -> "\\#{m}"}\""
title = title.replace /\\|"/g, (m) -> "\\#{m}"
url += " \"#{title}\""
return "![#{alt}](#{url})"
.replace LINK_REGEX, (m, url='', title, text='') ->
if title?
url += " \"#{title.replace /\\|"/g, (m) -> "\\#{m}"}\""
return "[#{text}](#{url})"
title = title.replace /\\|"/g, (m) -> "\\#{m}"
url += " \"#{title}\""

if url is text and url isnt ''
return "<#{url}>"
else
return "[#{text}](#{url})"

token.text = htmlEntities.decode(token.text)
return token
Expand Down
10 changes: 10 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ describe 'inline grammar', ->
tidyMdSnippet('[](#anchor)').should.equal('[](#anchor)')
tidyMdSnippet('[]()').should.equal('[]()')
tidyMdSnippet('[](#anchor "Title")').should.equal('[](#anchor "Title")')
tidyMdSnippet(
'https://github.com/slang800/tidy-markdown'
).should.equal(
'<https://github.com/slang800/tidy-markdown>'
)
tidyMdSnippet(
'<https://github.com/slang800/tidy-markdown>'
).should.equal(
'<https://github.com/slang800/tidy-markdown>'
)

it 'should handle images', ->
tidyMdSnippet('![text](image.jpg)').should.equal('![text](image.jpg)')
Expand Down

1 comment on commit 826d9f4

@rugk
Copy link

@rugk rugk commented on 826d9f4 Nov 9, 2015

Choose a reason for hiding this comment

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

👍

Please sign in to comment.