Skip to content

Commit 6062700

Browse files
authored
fix(message-parser): Phone pattern (#900)
* fix phone pattern check * adding more test and fixing parser * fixing left char tight with phone number
1 parent 29dd497 commit 6062700

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

packages/message-parser/src/grammar.pegjs

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
task,
2929
tasks,
3030
unorderedList,
31+
phoneChecker,
3132
} = require('./utils');
3233
}
3334

@@ -151,7 +152,7 @@ Whitespace = w:$" "+ { return plain(w); }
151152

152153
Escaped = "\\" t:$("*" / "_" / "~" / "`" / "#" / ".") { return plain(t); }
153154

154-
Any = !EndOfLine t:$. u:$URL? { return plain(t + u); }
155+
Any = !EndOfLine t:$. p:$AutolinkedPhone? u:$URL? { return plain(t + p + u); }
155156

156157
// = Line
157158

@@ -297,7 +298,7 @@ unicode
297298
return String.fromCharCode(parseInt(digits, 16));
298299
}
299300

300-
AutolinkedPhone = p:Phone { return link('tel:' + p.number, plain(p.text)); }
301+
AutolinkedPhone = p:Phone { return phoneChecker(p.text, p.number); }
301302

302303
AutolinkedURL = u:URL { return link(u); }
303304

@@ -369,6 +370,9 @@ phoneNumber
369370
= p:phonePrefix "-" d:digits {
370371
return { text: p.text + '-' + d, number: p.number + d };
371372
}
373+
/ p:phonePrefix d1:digits "-" d2:digits {
374+
return { text: p.text + d1 + '-' + d2, number: p.number + d1 + d2 };
375+
}
372376
/ p:phonePrefix d:digits {
373377
return { text: p.text + d, number: p.number + d };
374378
}

packages/message-parser/src/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,11 @@ export const inlineKatex = (content: string): InlineKaTeX => ({
193193
type: 'INLINE_KATEX',
194194
value: content,
195195
});
196+
197+
export const phoneChecker = (text: string, number: string) => {
198+
if (number.length < 5) {
199+
return plain(text);
200+
}
201+
202+
return link(`tel:${number}`, plain(text));
203+
};

packages/message-parser/tests/phoneNumber.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ test.each([
2626
'[**here**](+(075)63546725)',
2727
[paragraph([link('tel:07563546725', bold([plain('here')]))])],
2828
],
29+
[
30+
'[**here**](+(075)63546725)',
31+
[paragraph([link('tel:07563546725', bold([plain('here')]))])],
32+
],
33+
[
34+
'+(11)99999-9999',
35+
[paragraph([link('tel:11999999999', plain('+(11)99999-9999'))])],
36+
],
37+
['5 +51231', [paragraph([plain('5 '), link('tel:51231', plain('+51231'))])]],
38+
[
39+
'5 +51231 5',
40+
[paragraph([plain('5 '), link('tel:51231', plain('+51231')), plain(' 5')])],
41+
],
42+
['+(12)3-45', [paragraph([link('tel:12345', plain('+(12)3-45'))])]],
43+
['+1.599123', [paragraph([plain('+1.599123')])]],
44+
['1+1=2', [paragraph([plain('1+1=2')])]],
45+
['1+1=2 text', [paragraph([plain('1+1=2 text')])]],
46+
['+1000,00', [paragraph([plain('+1000,00')])]],
47+
['+ 1199999999', [paragraph([plain('+ 1199999999')])]],
48+
['+1234', [paragraph([plain('+1234')])]],
49+
['+(12)3-4', [paragraph([plain('+(12)3-4')])]],
50+
['+123-4', [paragraph([plain('+123-4')])]],
51+
['5+51231', [paragraph([plain('5+51231')])]],
2952
])('parses %p', (input, output) => {
3053
expect(parse(input)).toMatchObject(output);
3154
});

0 commit comments

Comments
 (0)