Skip to content

Commit ad479cb

Browse files
committed
fix(extension): strip trailing punctuation exposed by an unmatched ')' in linkify
Since c52d4b3 the unmatched-')' trim ran once after the trailing punctuation loop, so punctuation exposed by that trim was kept in the autolink: (https://example.com/path.) linked https://example.com/path. Fold the ')' and '&entity;' handling into the trailing loop so the trims repeat until the link end is stable, matching cmark-gfm's autolink extension. Fixes #580
1 parent e3e8a53 commit ad479cb

2 files changed

Lines changed: 44 additions & 19 deletions

File tree

‎extension/_test/linkify.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,23 @@ https://example.org/).
208208
<p><a href="https://example.org/">https://example.org/</a>).</p>
209209
<p>(<a href="https://example.org/path_(x)">https://example.org/path_(x)</a>).</p>
210210
//= = = = = = = = = = = = = = = = = = = = = = = =//
211+
212+
213+
21: Trailing punctuation exposed by an unmatched ')' is stripped
214+
//- - - - - - - - -//
215+
(https://example.com/path.)
216+
217+
(https://example.com/path,)
218+
219+
(www.example.com/x.)
220+
221+
(https://example.com/a&amp;).
222+
223+
(https://example.org/path_(x).)
224+
//- - - - - - - - -//
225+
<p>(<a href="https://example.com/path">https://example.com/path</a>.)</p>
226+
<p>(<a href="https://example.com/path">https://example.com/path</a>,)</p>
227+
<p>(<a href="http://www.example.com/x">www.example.com/x</a>.)</p>
228+
<p>(<a href="https://example.com/a">https://example.com/a</a>&amp;).</p>
229+
<p>(<a href="https://example.org/path_(x)">https://example.org/path_(x)</a>.)</p>
230+
//= = = = = = = = = = = = = = = = = = = = = = = =//

‎extension/linkify.go‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -244,31 +244,36 @@ func (s *linkifyParser) Parse(parent ast.Node, block text.Reader, pc parser.Cont
244244
s := segment.WithStop(segment.Start + 1)
245245
ast.MergeOrAppendTextSegment(parent, s)
246246
}
247-
i := m[1] - 1
248-
for ; i > 0; i-- {
249-
c := line[i]
250-
switch c {
247+
opening := bytes.Count(line[:m[1]], []byte{'('})
248+
closing := bytes.Count(line[:m[1]], []byte{')'})
249+
i := m[1]
250+
for i > 1 {
251+
switch line[i-1] {
251252
case '?', '!', '.', ',', ':', '*', '_', '~':
253+
i--
254+
case ')':
255+
if closing <= opening {
256+
goto endfor
257+
}
258+
closing--
259+
i--
260+
case ';':
261+
j := i - 2
262+
for ; j > 0; j-- {
263+
if util.IsAlphaNumeric(line[j]) {
264+
continue
265+
}
266+
break
267+
}
268+
if j == i-2 || line[j] != '&' {
269+
goto endfor
270+
}
271+
i = j
252272
default:
253273
goto endfor
254274
}
255275
}
256276
endfor:
257-
i++
258-
if line[i-1] == ')' {
259-
i -= max(0, bytes.Count(line[:i], []byte{')'})-bytes.Count(line[:i], []byte{'('}))
260-
} else if line[i-1] == ';' {
261-
j := i - 2
262-
for ; j >= m[0]; j-- {
263-
if util.IsAlphaNumeric(line[j]) {
264-
continue
265-
}
266-
break
267-
}
268-
if j != i-2 && line[j] == '&' {
269-
i = j
270-
}
271-
}
272277
consumes += i
273278
block.Advance(consumes)
274279
n := ast.NewTextSegment(text.NewSegment(start, start+i))

0 commit comments

Comments
 (0)