| 1 | package websource |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // Mentions finds the web sources in a person's own words (Research spec |
| 10 | // §2 step 2): every http or https URL as typed, and every bare domain (an |
| 11 | // optional path after it) as https://<domain>/, each made canonical by |
| 12 | // Parse, in the order written and once each. Anything Parse refuses is |
| 13 | // left out, and so are e-mail addresses, parts of other URLs, names whose |
| 14 | // last label is a common file extension (README.md, main.go, Next.js) and |
| 15 | // abbreviations (e.g., i.e.). |
| 16 | func Mentions(text string) []URL { |
| 17 | type found struct { |
| 18 | at int |
| 19 | raw string |
| 20 | } |
| 21 | var all []found |
| 22 | // Full URLs first; their text is then blanked so no part of one is |
| 23 | // read again as a bare domain. |
| 24 | rest := []byte(text) |
| 25 | for _, m := range fullURL.FindAllStringIndex(text, -1) { |
| 26 | all = append(all, found{m[0], trimTrailing(text[m[0]:m[1]])}) |
| 27 | for i := m[0]; i < m[1]; i++ { |
| 28 | rest[i] = ' ' |
| 29 | } |
| 30 | } |
| 31 | plain := string(rest) |
| 32 | for _, m := range bareDomain.FindAllStringIndex(plain, -1) { |
| 33 | if m[0] > 0 && strings.ContainsRune("@/_:.-", rune(plain[m[0]-1])) || m[1] < len(plain) && plain[m[1]] == '@' { |
| 34 | continue |
| 35 | } |
| 36 | raw := trimTrailing(plain[m[0]:m[1]]) |
| 37 | host, path, _ := strings.Cut(raw, "/") |
| 38 | labels := strings.Split(strings.ToLower(strings.TrimSuffix(host, ".")), ".") |
| 39 | last := labels[len(labels)-1] |
| 40 | if len(last) < 2 || slices.Contains(fileExtensions, last) { |
| 41 | continue |
| 42 | } |
| 43 | all = append(all, found{m[0], "https://" + host + "/" + path}) |
| 44 | } |
| 45 | slices.SortStableFunc(all, func(a, b found) int { return a.at - b.at }) |
| 46 | var out []URL |
| 47 | seen := map[string]bool{} |
| 48 | for _, f := range all { |
| 49 | u, err := Parse(f.raw) |
| 50 | if err != nil || seen[u.String()] { |
| 51 | continue |
| 52 | } |
| 53 | seen[u.String()] = true |
| 54 | out = append(out, u) |
| 55 | } |
| 56 | return out |
| 57 | } |
| 58 | |
| 59 | var ( |
| 60 | fullURL = regexp.MustCompile(`(?i)\bhttps?://[^\s<>"'\x60]+`) |
| 61 | bareDomain = regexp.MustCompile(`[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z][A-Za-z0-9-]*(?:/[^\s<>"'\x60]*)?`) |
| 62 | ) |
| 63 | |
| 64 | // trimTrailing drops the punctuation that ends a sentence or a bracket |
| 65 | // around a URL. |
| 66 | func trimTrailing(s string) string { |
| 67 | return strings.TrimRight(s, ".,;:!?)]}>'\"") |
| 68 | } |
| 69 | |
| 70 | // fileExtensions are last labels that name a file far more often than a |
| 71 | // site; a source with one is taken only when typed with its scheme. |
| 72 | var fileExtensions = []string{"md", "mdx", "txt", "rst", "go", "mod", "sum", "sh", "py", "rs", "js", "jsx", "ts", "tsx", "mjs", "cjs", "json", "yaml", "yml", "toml", "ini", "cfg", "conf", "env", "lock", "html", "htm", "css", "scss", "rb", "php", "sql", "db", "sqlite", "png", "jpg", "jpeg", "gif", "svg", "webp", "ico", "pdf", "zip", "gz", "tar", "xml", "csv", "log", "swift", "kt", "java", "c", "h", "cpp", "hpp", "vue", "svelte", "lua", "pl", "ex", "exs", "hs", "ml", "dart", "plist", "tf", "hcl", "nix", "bak", "tmp", "so", "dll", "exe", "bin", "out", "ipynb", "gradle"} |
| 73 | |