|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Devnull\Data; |
| 4 | + |
| 5 | +/** |
| 6 | + * A URL in a commit's prose, shown as the web's LinkifiedText shows it: a commit |
| 7 | + * on dev/null as its author and short hash, a GitHub repository, issue or pull |
| 8 | + * request as GitHub writes it, and anything else shortened the way X does. |
| 9 | + * |
| 10 | + * The rules are the web's, from linkified-text.tsx and app/Services/Urls.php. |
| 11 | + * A commit opens in the app, and everything else in the browser. |
| 12 | + */ |
| 13 | +final readonly class BodyLink |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Top-level domains a bare domain like "nativephp.com" may end with. It's |
| 17 | + * an allowlist because most file extensions are real TLDs too, so the ones |
| 18 | + * that read like code or files (sh, py, rs, md, ...) are left out. |
| 19 | + */ |
| 20 | + private const array TopLevelDomains = [ |
| 21 | + 'com', 'org', 'net', 'edu', 'gov', 'mil', 'info', 'biz', 'io', 'dev', 'app', 'co', 'ai', 'me', 'xyz', 'tech', |
| 22 | + 'gg', 'tv', 'fm', 'ly', 'to', 'is', 'blog', 'cloud', 'site', 'online', 'design', 'studio', 'tools', 'software', |
| 23 | + 'codes', 'wiki', 'news', 'website', 'club', 'ninja', 'rocks', 'academy', 'guide', 'social', 'digital', 'agency', |
| 24 | + 'fyi', 'lol', 'live', |
| 25 | + 'uk', 'de', 'fr', 'nl', 'es', 'it', 'pt', 'br', 'ca', 'au', 'jp', 'ch', 'se', 'no', 'dk', 'fi', 'be', 'at', 'ie', |
| 26 | + 'nz', 'us', 'eu', 'cz', 'sk', 'hu', 'ro', 'bg', 'gr', 'hr', 'si', 'ee', 'lv', 'lt', 'ru', 'ua', 'tr', 'il', 'za', |
| 27 | + 'mx', 'ar', 'kr', 'cn', 'tw', 'hk', 'sg', 'vn', 'th', |
| 28 | + ]; |
| 29 | + |
| 30 | + /** |
| 31 | + * First path segments that are GitHub's own pages rather than an owner. |
| 32 | + */ |
| 33 | + private const array GitHubPages = [ |
| 34 | + 'about', 'apps', 'codespaces', 'collections', 'customer-stories', 'enterprise', 'events', 'explore', 'features', |
| 35 | + 'issues', 'login', 'marketplace', 'new', 'notifications', 'orgs', 'organizations', 'pricing', 'pulls', 'search', |
| 36 | + 'security', 'settings', 'site', 'sponsors', 'topics', 'trending', 'users', |
| 37 | + ]; |
| 38 | + |
| 39 | + /** |
| 40 | + * A commit on dev/null with nothing after its id. |
| 41 | + */ |
| 42 | + private const string CommitLink = '/^https?:\/\/(?:www\.)?dev-null\.sh\/@([A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38})\/commits\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i'; |
| 43 | + |
| 44 | + /** |
| 45 | + * A GitHub repository, issue or pull request with nothing after its name or number. |
| 46 | + */ |
| 47 | + private const string GitHubLink = '/^https?:\/\/(?:www\.)?github\.com\/([A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38})\/([A-Za-z0-9._-]{1,100})(?:\/(issues|pull)\/([1-9][0-9]{0,9}))?$/i'; |
| 48 | + |
| 49 | + private const int MaxLength = 2048; |
| 50 | + |
| 51 | + private const int MaxShortenedPathLength = 20; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param string $href Where it goes: the URL, with https:// before a bare domain. |
| 55 | + * @param string $shown What it shows. |
| 56 | + * @param string $prefix What it shows muted before that: a commit's author or a repository's owner, and "/". |
| 57 | + * @param string|null $commit The id of the dev/null commit it opens in the app. |
| 58 | + */ |
| 59 | + private function __construct(public string $href, public string $shown, public string $prefix = '', public ?string $commit = null) {} |
| 60 | + |
| 61 | + /** |
| 62 | + * Every link in some prose, in order, with where it starts and how long it |
| 63 | + * is as written, in code points. Punctuation that ends the sentence rather |
| 64 | + * than the URL stays prose. |
| 65 | + * |
| 66 | + * @return list<array{0: int, 1: int, 2: self}> |
| 67 | + */ |
| 68 | + public static function in(string $prose): array |
| 69 | + { |
| 70 | + preg_match_all(self::pattern(), $prose, $matches, PREG_OFFSET_CAPTURE); |
| 71 | + |
| 72 | + $links = []; |
| 73 | + |
| 74 | + foreach ($matches[0] as [$candidate, $byte]) { |
| 75 | + $written = self::withoutTrailingPunctuation($candidate); |
| 76 | + $href = preg_match('/^(https?)(:\/\/.*)$/is', $written, $scheme) === 1 ? strtolower($scheme[1]).$scheme[2] : 'https://'.$written; |
| 77 | + |
| 78 | + if (str_ends_with($href, '://') || mb_strlen($href) > self::MaxLength) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $links[] = [mb_strlen(substr($prose, 0, $byte)), mb_strlen($written), self::to($written, $href)]; |
| 83 | + } |
| 84 | + |
| 85 | + return $links; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Whether it shows as a name and a short hash or number rather than as a URL. |
| 90 | + */ |
| 91 | + public function isNamed(): bool |
| 92 | + { |
| 93 | + return $this->prefix !== ''; |
| 94 | + } |
| 95 | + |
| 96 | + private static function to(string $written, string $href): self |
| 97 | + { |
| 98 | + if (preg_match(self::CommitLink, $written, $commit) === 1) { |
| 99 | + $id = strtolower($commit[2]); |
| 100 | + |
| 101 | + return new self($href, substr(str_replace('-', '', $id), -7), $commit[1].'/', $id); |
| 102 | + } |
| 103 | + |
| 104 | + if (preg_match(self::GitHubLink, $href, $gitHub) === 1 && ! in_array(strtolower($gitHub[1]), self::GitHubPages, true)) { |
| 105 | + return new self($href, $gitHub[2].(isset($gitHub[4]) ? '#'.$gitHub[4] : ''), $gitHub[1].'/'); |
| 106 | + } |
| 107 | + |
| 108 | + return new self($href, self::shorten($written)); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Like X: without its scheme or "www.", with the whole host, and at most 20 |
| 113 | + * characters after it, the last an ellipsis when it's cut. A cut never splits |
| 114 | + * a percent-encoded character. |
| 115 | + */ |
| 116 | + private static function shorten(string $url): string |
| 117 | + { |
| 118 | + $url = (string) preg_replace('/^(?:https?:\/\/)?(?:www\.(?=[^\/?#]*\.))?/i', '', $url); |
| 119 | + $host = (string) preg_replace('/[\/?#].*$/su', '', $url); |
| 120 | + $path = mb_substr($url, mb_strlen($host)); |
| 121 | + |
| 122 | + if (mb_strlen($path) <= self::MaxShortenedPathLength) { |
| 123 | + return $url; |
| 124 | + } |
| 125 | + |
| 126 | + $kept = (string) preg_replace('/%[0-9A-Fa-f]?$/', '', mb_substr($path, 0, self::MaxShortenedPathLength - 1)); |
| 127 | + |
| 128 | + return $host.$kept.'…'; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * A URL with a scheme, or a bare domain: ASCII labels ending in an allowed |
| 133 | + * TLD, then maybe a path, query or fragment. A bare domain can't follow a |
| 134 | + * letter, digit, "_", ".", "/", "@" or "-", its TLD is all lowercase or all |
| 135 | + * uppercase, and it ends the word without a "(" or another label after it. |
| 136 | + */ |
| 137 | + private static function pattern(): string |
| 138 | + { |
| 139 | + $topLevelDomains = implode('|', [...self::TopLevelDomains, ...array_map(strtoupper(...), self::TopLevelDomains)]); |
| 140 | + |
| 141 | + return '/\b[Hh][Tt][Tt][Pp][Ss]?:\/\/[^\s<>]+' |
| 142 | + .'|(?<![\p{L}\p{N}_.\/@-])(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+(?:'.$topLevelDomains.')' |
| 143 | + .'(?![\p{L}\p{N}_(-]|\.[A-Za-z0-9])(?:[\/?#][^\s<>]*)?/u'; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Strips punctuation that ends a sentence rather than the URL, keeping |
| 148 | + * closing brackets that pair with one inside it. |
| 149 | + */ |
| 150 | + private static function withoutTrailingPunctuation(string $url): string |
| 151 | + { |
| 152 | + do { |
| 153 | + $previous = $url; |
| 154 | + $url = mb_rtrim($url, '.,;:!?\'"'); |
| 155 | + |
| 156 | + foreach (['(' => ')', '[' => ']'] as $open => $close) { |
| 157 | + if (str_ends_with($url, $close) && mb_substr_count($url, $open) < mb_substr_count($url, $close)) { |
| 158 | + $url = mb_substr($url, 0, -1); |
| 159 | + } |
| 160 | + } |
| 161 | + } while ($url !== $previous); |
| 162 | + |
| 163 | + return $url; |
| 164 | + } |
| 165 | +} |
0 commit comments