| 1 |
package parse |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"regexp" |
| 7 |
"slices" |
| 8 |
) |
| 9 |
|
| 10 |
// Partial regexp to match the beginning of URLs and email addresses. |
| 11 |
// The remainder of the matched URLs/emails is parsed manually. |
| 12 |
var urlRe = regexp.MustCompile( |
| 13 |
`([a-z]{2,8})://` + // URL start |
| 14 |
`|` + // or |
| 15 |
`(mailto:)?[[:alnum:]_+.~/-]*[[:alnum:]]@`, // email start |
| 16 |
) |
| 17 |
|
| 18 |
// HttpLinks searches a reader for a http link and returns a copy of the |
| 19 |
// reader and a slice with links. If isHtml is true, left angle brackets are |
| 20 |
// considered to always be right link delimiters. |
| 21 |
func HttpLinks(r io.Reader, isHtml bool) (io.Reader, []string) { |
| 22 |
buf, err := io.ReadAll(r) |
| 23 |
if err != nil { |
| 24 |
return r, nil |
| 25 |
} |
| 26 |
|
| 27 |
links := make(map[string]struct{}) |
| 28 |
b := buf |
| 29 |
match := urlRe.FindSubmatchIndex(b) |
| 30 |
for ; match != nil; match = urlRe.FindSubmatchIndex(b) { |
| 31 |
// Regular expressions do not really cut it here and we |
| 32 |
// need to detect opening/closing braces to handle |
| 33 |
// markdown link syntax. |
| 34 |
var paren, bracket, ltgt, scheme int |
| 35 |
var emitUrl bool |
| 36 |
i, j := match[0], match[1] |
| 37 |
b = b[i:] |
| 38 |
scheme = j - i |
| 39 |
j = scheme |
| 40 |
|
| 41 |
// "inline" email without a mailto: prefix - add some extra checks for those |
| 42 |
inlineEmail := len(match) > 4 && match[2] == -1 && match[4] == -1 |
| 43 |
|
| 44 |
for !emitUrl && j < len(b) && bytes.IndexByte(urichars, b[j]) != -1 { |
| 45 |
switch b[j] { |
| 46 |
case '[': |
| 47 |
bracket++ |
| 48 |
j++ |
| 49 |
case '(': |
| 50 |
paren++ |
| 51 |
j++ |
| 52 |
case '<': |
| 53 |
if isHtml { |
| 54 |
emitUrl = true |
| 55 |
} else { |
| 56 |
ltgt++ |
| 57 |
j++ |
| 58 |
} |
| 59 |
case ']': |
| 60 |
bracket-- |
| 61 |
if bracket < 0 { |
| 62 |
emitUrl = true |
| 63 |
} else { |
| 64 |
j++ |
| 65 |
} |
| 66 |
case ')': |
| 67 |
paren-- |
| 68 |
if paren < 0 { |
| 69 |
emitUrl = true |
| 70 |
} else { |
| 71 |
j++ |
| 72 |
} |
| 73 |
case '>': |
| 74 |
ltgt-- |
| 75 |
if ltgt < 0 { |
| 76 |
emitUrl = true |
| 77 |
} else { |
| 78 |
j++ |
| 79 |
} |
| 80 |
case '&': |
| 81 |
if inlineEmail { |
| 82 |
emitUrl = true |
| 83 |
} else { |
| 84 |
j++ |
| 85 |
} |
| 86 |
default: |
| 87 |
j++ |
| 88 |
} |
| 89 |
|
| 90 |
// we don't want those in inline emails |
| 91 |
if inlineEmail && (paren > 0 || ltgt > 0 || bracket > 0) { |
| 92 |
j-- |
| 93 |
emitUrl = true |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Heuristic to remove trailing characters that are |
| 98 |
// valid URL characters, but typically not at the end of |
| 99 |
// the URL |
| 100 |
for trim := true; trim && j > 0; { |
| 101 |
switch b[j-1] { |
| 102 |
case '.', ',', ':', ';', '?', '!', '"', '\'', '%': |
| 103 |
j-- |
| 104 |
default: |
| 105 |
trim = false |
| 106 |
} |
| 107 |
} |
| 108 |
if j == scheme { |
| 109 |
// Only an URL scheme, ignore. |
| 110 |
b = b[j:] |
| 111 |
continue |
| 112 |
} |
| 113 |
url := string(b[:j]) |
| 114 |
if inlineEmail { |
| 115 |
// Email address with missing mailto: scheme. Add it. |
| 116 |
url = "mailto:" + url |
| 117 |
} |
| 118 |
links[url] = struct{}{} |
| 119 |
b = b[j:] |
| 120 |
} |
| 121 |
|
| 122 |
results := make([]string, 0, len(links)) |
| 123 |
for link := range links { |
| 124 |
results = append(results, link) |
| 125 |
} |
| 126 |
slices.Sort(results) |
| 127 |
|
| 128 |
return bytes.NewReader(buf), results |
| 129 |
} |
| 130 |
|
| 131 |
var urichars = []byte( |
| 132 |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + |
| 133 |
"0123456789-_.,~:;/?#@!$&%*+=\"'<>()[]", |
| 134 |
) |