-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathparse-text.js
More file actions
182 lines (158 loc) · 4.41 KB
/
parse-text.js
File metadata and controls
182 lines (158 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* global CONFIG */
import {
arrows,
combine,
emails,
foreignMentions,
hashtags,
LINK,
links,
mentions,
withTexts,
} from 'social-text-tokenizer';
import { linkHref } from 'social-text-tokenizer/prettifiers';
import { makeToken, reTokenizer, wordAdjacentChars } from 'social-text-tokenizer/utils';
import { withCharsAfter, withCharsBefore, withFilters } from 'social-text-tokenizer/filters';
import { checkboxParser } from './initial-checkbox';
import { SPOILER_END, SPOILER_START, spoilerTags, validateSpoilerTags } from './spoiler-tokens';
const {
textFormatter: { tldList, foreignMentionServices },
siteDomains,
} = CONFIG;
const shortLinkRe = /\/[a-z\d-]{3,35}\/[\da-f]{6,10}(?:#[\da-f]{4,6})?/gi;
const shortLinkExactRe = /^\/[a-z\d-]{3,35}\/[\da-f]{6,10}(?:#[\da-f]{4,6})?$/i;
/**
* @param {string} text
* @param {string[]} localDomains
* @returns {boolean}
*/
export function isLocalLink(text, localDomains = siteDomains) {
let url;
try {
url = new URL(linkHref(text));
} catch {
return false;
}
const p = localDomains.indexOf(url.host);
if (p === -1) {
return false;
} else if (p === 0) {
// First domain in localDomains list is the domain of main site.
// These links are always local.
return true;
}
// Other domains in localDomains list are alternative frontends or mirrors.
// Such links should be treated as remote if they lead to the domain root.
return url.pathname + url.search !== '/';
}
/**
* @param {string} text - Part of URL without origin (i.e. /path?query#hash)
* @returns {boolean}
*/
export function isShortLink(text) {
return shortLinkExactRe.test(text);
}
export function trimOrigin(text) {
let url;
try {
url = new URL(linkHref(text));
} catch {
return text;
}
return url.pathname + url.search + url.hash;
}
export function arrowsLevel(text) {
if (/\d/.test(text)) {
return Number(text.replace(/\D/g, ''));
}
return text.length;
}
const tldRe = [...tldList]
// Putting the longest TLDs first to capture them before their substrings.
// Example pairs: DEV / DE, PLACE / PL
.sort((a, b) => {
if (a.length > b.length) {
return -1;
} else if (a.length < b.length) {
return 1;
} else if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
return 0;
})
.join('|');
export const LINE_BREAK = 'LINE_BREAK';
export const PARAGRAPH_BREAK = 'PARAGRAPH_BREAK';
export const REDDIT_LINK = 'REDDIT_LINK';
export const SHORT_LINK = 'SHORT_LINK';
const redditLinks = withFilters(
reTokenizer(/\/?r\/[A-Za-z\d]\w{1,20}/g, makeToken(REDDIT_LINK)),
withCharsBefore(wordAdjacentChars.withoutChars('/')),
withCharsAfter(wordAdjacentChars.withoutChars('/')),
);
export function redditLinkHref(text) {
if (!text.startsWith('/')) {
text = `/${text}`;
}
return `https://www.reddit.com${text}`;
}
const shortLinks = withFilters(
reTokenizer(shortLinkRe, makeToken(SHORT_LINK)),
withCharsBefore(wordAdjacentChars.withoutChars('/')),
withCharsAfter(wordAdjacentChars.withoutChars('/')),
);
export const lineBreaks = reTokenizer(/[^\S\n]*\n\s*/g, (offset, text) => {
if (text.indexOf('\n') === text.lastIndexOf('\n')) {
return makeToken(LINE_BREAK)(offset, text);
}
return makeToken(PARAGRAPH_BREAK)(offset, text);
});
export const parseText = withTexts(
validateSpoilerTags(
combine(
hashtags(),
emails(),
mentions(),
foreignMentions(),
links({ tldRe }),
arrows(/\u2191+|\^([1-9]\d*|\^*)/g),
shortLinks,
redditLinks,
spoilerTags,
checkboxParser,
lineBreaks,
),
),
);
export function getFirstLinkToEmbed(text) {
let isInSpoiler = false;
const firstLink = parseText(text).find((token) => {
if (token.type === SPOILER_START) {
isInSpoiler = true;
}
if (token.type === SPOILER_END) {
isInSpoiler = false;
}
if (isInSpoiler || token.type !== LINK) {
return false;
}
return (
!isLocalLink(token.text) &&
/^https?:\/\//i.test(token.text) &&
text.charAt(token.offset - 1) !== '!'
);
});
return firstLink ? linkHref(firstLink.text) : undefined;
}
export const shortCodeToService = {};
for (const srv of foreignMentionServices) {
for (const code of srv.shortCodes) {
shortCodeToService[code] = srv;
}
}
export function getTextAlign(char) {
const persianRegex = /[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF]/g;
return persianRegex.test(char);
}