Skip to content

Commit 8e768bd

Browse files
committed
fix url parse
1 parent 5de54cc commit 8e768bd

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

Parser.js

Lines changed: 26 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"license": "BSD-4-Clause",
99
"author": "SegmentFault",
10-
"version": "2.4.19",
10+
"version": "2.4.20",
1111
"scripts": {
1212
"test": "mocha",
1313
"build": "cake build"

src/Parser.coffee

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ class Parser
303303
text = text.replace /!\[((?:[^\]]|\\\]|\\\[)*?)\]\(((?:[^\)]|\\\)|\\\()+?)\)/g, (matches...) =>
304304
escaped = htmlspecialchars @escapeBracket matches[1]
305305
url = @escapeBracket matches[2]
306-
url = @cleanUrl url
307-
@makeHolder "<img src=\"#{url}\" alt=\"#{escaped}\" title=\"#{escaped}\">"
306+
[url, title] = @cleanUrl url, yes
307+
title = if not title? then escaped else " title=\"#{title}\""
308+
@makeHolder "<img src=\"#{url}\" alt=\"#{title}\" title=\"#{title}\">"
308309

309310
text = text.replace /!\[((?:[^\]]|\\\]|\\\[)*?)\]\[((?:[^\]]|\\\]|\\\[)+?)\]/g, (matches...) =>
310311
escaped = htmlspecialchars @escapeBracket matches[1]
@@ -317,8 +318,10 @@ class Parser
317318
text = text.replace /\[((?:[^\]]|\\\]|\\\[)+?)\]\(((?:[^\)]|\\\)|\\\()+?)\)/g, (matches...) =>
318319
escaped = @parseInline (@escapeBracket matches[1]), '', no, no
319320
url = @escapeBracket matches[2]
320-
url = @cleanUrl url
321-
@makeHolder "<a href=\"#{url}\">#{escaped}</a>"
321+
[url, title] = @cleanUrl url, yes
322+
title = if not title? then '' else " title=\"#{title}\""
323+
324+
@makeHolder "<a href=\"#{url}\"#{title}>#{escaped}</a>"
322325

323326
text = text.replace /\[((?:[^\]]|\\\]|\\\[)+?)\]\[((?:[^\]]|\\\]|\\\[)+?)\]/g, (matches...) =>
324327
escaped = @parseInline (@escapeBracket matches[1]), '', no, no
@@ -987,15 +990,24 @@ class Parser
987990
(@markLines lines, start).join "\n"
988991

989992

990-
cleanUrl: (url) ->
993+
cleanUrl: (url, parseTitle = false) ->
994+
title = null
995+
996+
if parseTitle
997+
pos = url.indexOf ' '
998+
999+
if pos > 0
1000+
title = htmlspecialchars trim (url.substring pos + 1), ' "\''
1001+
url = url.substring 0, pos
1002+
9911003
url = url.replace /["'<>\s]/g, ''
9921004

9931005
if !!(matches = url.match /^(mailto:)?[_a-z0-9-\.\+]+@[_\w-]+\.[a-z]{2,}$/i)
9941006
url = 'mailto:' + url if not matches[1]?
9951007

9961008
return '#' if (url.match /^\w+:/i) and not (url.match /^(https?|mailto):/i)
9971009

998-
url
1010+
if parseTitle then [url, title] else url
9991011

10001012

10011013
escapeBracket: (str) ->

0 commit comments

Comments
 (0)