diff --git a/packages/message-parser/src/grammar.pegjs b/packages/message-parser/src/grammar.pegjs index 19ba2ba6ad..fe0f2f5fc1 100644 --- a/packages/message-parser/src/grammar.pegjs +++ b/packages/message-parser/src/grammar.pegjs @@ -28,6 +28,7 @@ task, tasks, unorderedList, + phoneChecker, } = require('./utils'); } @@ -151,7 +152,7 @@ Whitespace = w:$" "+ { return plain(w); } Escaped = "\\" t:$("*" / "_" / "~" / "`" / "#" / ".") { return plain(t); } -Any = !EndOfLine t:$. u:$URL? { return plain(t + u); } +Any = !EndOfLine t:$. p:$AutolinkedPhone? u:$URL? { return plain(t + p + u); } // = Line @@ -297,7 +298,7 @@ unicode return String.fromCharCode(parseInt(digits, 16)); } -AutolinkedPhone = p:Phone { return link('tel:' + p.number, plain(p.text)); } +AutolinkedPhone = p:Phone { return phoneChecker(p.text, p.number); } AutolinkedURL = u:URL { return link(u); } @@ -369,6 +370,9 @@ phoneNumber = p:phonePrefix "-" d:digits { return { text: p.text + '-' + d, number: p.number + d }; } + / p:phonePrefix d1:digits "-" d2:digits { + return { text: p.text + d1 + '-' + d2, number: p.number + d1 + d2 }; + } / p:phonePrefix d:digits { return { text: p.text + d, number: p.number + d }; } diff --git a/packages/message-parser/src/utils.ts b/packages/message-parser/src/utils.ts index 2f81cda29b..454cf9c6d6 100644 --- a/packages/message-parser/src/utils.ts +++ b/packages/message-parser/src/utils.ts @@ -193,3 +193,11 @@ export const inlineKatex = (content: string): InlineKaTeX => ({ type: 'INLINE_KATEX', value: content, }); + +export const phoneChecker = (text: string, number: string) => { + if (number.length < 5) { + return plain(text); + } + + return link(`tel:${number}`, plain(text)); +}; diff --git a/packages/message-parser/tests/phoneNumber.test.ts b/packages/message-parser/tests/phoneNumber.test.ts index 57c95dfc63..3d4b138302 100644 --- a/packages/message-parser/tests/phoneNumber.test.ts +++ b/packages/message-parser/tests/phoneNumber.test.ts @@ -26,6 +26,29 @@ test.each([ '[**here**](+(075)63546725)', [paragraph([link('tel:07563546725', bold([plain('here')]))])], ], + [ + '[**here**](+(075)63546725)', + [paragraph([link('tel:07563546725', bold([plain('here')]))])], + ], + [ + '+(11)99999-9999', + [paragraph([link('tel:11999999999', plain('+(11)99999-9999'))])], + ], + ['5 +51231', [paragraph([plain('5 '), link('tel:51231', plain('+51231'))])]], + [ + '5 +51231 5', + [paragraph([plain('5 '), link('tel:51231', plain('+51231')), plain(' 5')])], + ], + ['+(12)3-45', [paragraph([link('tel:12345', plain('+(12)3-45'))])]], + ['+1.599123', [paragraph([plain('+1.599123')])]], + ['1+1=2', [paragraph([plain('1+1=2')])]], + ['1+1=2 text', [paragraph([plain('1+1=2 text')])]], + ['+1000,00', [paragraph([plain('+1000,00')])]], + ['+ 1199999999', [paragraph([plain('+ 1199999999')])]], + ['+1234', [paragraph([plain('+1234')])]], + ['+(12)3-4', [paragraph([plain('+(12)3-4')])]], + ['+123-4', [paragraph([plain('+123-4')])]], + ['5+51231', [paragraph([plain('5+51231')])]], ])('parses %p', (input, output) => { expect(parse(input)).toMatchObject(output); });