this repo has no description
1import fs from 'fs';
2
3import regexSupplant from 'twitter-text/dist/lib/regexSupplant.js';
4import validDomain from 'twitter-text/dist/regexp/validDomain.js';
5import validPortNumber from 'twitter-text/dist/regexp/validPortNumber.js';
6import validUrlPath from 'twitter-text/dist/regexp/validUrlPath.js';
7import validUrlPrecedingChars from 'twitter-text/dist/regexp/validUrlPrecedingChars.js';
8import validUrlQueryChars from 'twitter-text/dist/regexp/validUrlQueryChars.js';
9import validUrlQueryEndingChars from 'twitter-text/dist/regexp/validUrlQueryEndingChars.js';
10
11// The difference with twitter-text's extractURL is that the protocol isn't
12// optional.
13
14const urlRegex = regexSupplant(
15 '(' + // $1 total match
16 '(#{validUrlPrecedingChars})' + // $2 Preceeding chracter
17 '(' + // $3 URL
18 '(https?:\\/\\/)' + // $4 Protocol (optional) <-- THIS IS THE DIFFERENCE, MISSING '?' AFTER PROTOCOL
19 '(#{validDomain})' + // $5 Domain(s)
20 '(?::(#{validPortNumber}))?' + // $6 Port number (optional)
21 '(\\/#{validUrlPath}*)?' + // $7 URL Path
22 '(\\?#{validUrlQueryChars}*#{validUrlQueryEndingChars})?' + // $8 Query String
23 ')' +
24 ')',
25 {
26 validUrlPrecedingChars,
27 validDomain,
28 validPortNumber,
29 validUrlPath,
30 validUrlQueryChars,
31 validUrlQueryEndingChars,
32 },
33 'gi',
34);
35
36const filePath = 'src/data/url-regex.json';
37fs.writeFile(
38 filePath,
39 JSON.stringify({
40 source: urlRegex.source,
41 flags: urlRegex.flags,
42 }),
43 (err) => {
44 if (err) {
45 console.error(err);
46 }
47 console.log(`Wrote ${filePath}`);
48 },
49);