linkifyjs
linkifyjs copied to clipboard
Phone number support
Need (800) 555-1212 -> tel: url...
+1
I'm using linkify for a mobile app and people are creating messages with phone numbers. This would be a huge plus for me.
Hey @rlaferla,
I created a class that creates anchors for US phone numbers:
'use strict';
// node_module classes
var phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
var PNF = require('google-libphonenumber').PhoneNumberFormat;
var PNT = require('google-libphonenumber').PhoneNumberType;
var usPhoneRegex = /(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?/gm;
module.exports = {
telify: function(htmlText) {
var newText = htmlText.replace(usPhoneRegex, function(match) {
var phoneObject = phoneUtil.parse(match, 'US');
var displayNumber = phoneUtil.format(phoneObject, PNF.NATIONAL);
var telNumber = phoneUtil.format(phoneObject, PNF.E164);
var anchorText = '<a href="tel:' + telNumber + '">' + displayNumber + '</a>';
return anchorText;
});
return newText;
}
};
+1 ,
Would be a huge improvement for mobile app !
how can add above for my linkify-string.js simple javascript file i want to support for us phone number?
+1 will phone number parsing be implemented?
@nfrasser I'm willing to code this feature and raise a PR if you could help me figure out the scope of this. As linkify promotes it's a zero dependency plugin, would it be fine to use external library like google's libphonenumber for the number recognition part?
Until there's a support, this is my workaround which works fine with multiple numbers. Hope you guys find it useful.
import { findPhoneNumbersInText } from 'libphonenumber-js'
telify = (origText) => {
let newText = origText
const getPhoneNum = findPhoneNumbersInText(newText);
if (newText?.length) {
getPhoneNum.forEach((match) => {
const pattern = origText.substring(match.startsAt, match.endsAt);
newText = newText.replace( pattern, `<a href="tel:${match.number.number}")>${pattern}</a>`);
});
}
return newText;
}
// usage > telify("Hey! you can call me at +1 (501) 234-5678 or in my local no# +91 50123-45678 ^_^ ")
libphonenumber-js
I'm trying to write a plugin out of this, but not able to figure it out. Is there any guides on writing custom plugin?
+1 would need it for german and US phonenumbers
+1 i need phone number support as well