linkifyjs icon indicating copy to clipboard operation
linkifyjs copied to clipboard

Phone number support

Open rlaferla opened this issue 8 years ago • 9 comments

Need (800) 555-1212 -> tel: url...

rlaferla avatar May 11 '16 18:05 rlaferla

+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.

atgillette avatar Jun 25 '16 21:06 atgillette

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;
  }
};

atgillette avatar Jun 26 '16 01:06 atgillette

+1 ,

Would be a huge improvement for mobile app !

sambegin avatar Sep 07 '16 20:09 sambegin

how can add above for my linkify-string.js simple javascript file i want to support for us phone number?

bilalizloo avatar Dec 08 '16 14:12 bilalizloo

+1 will phone number parsing be implemented?

chakafasano88 avatar Jun 10 '20 15:06 chakafasano88

@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 ^_^ ")

siwalikm avatar Oct 12 '20 14:10 siwalikm

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?

Noitidart avatar Apr 25 '22 06:04 Noitidart

+1 would need it for german and US phonenumbers

MrJustreborn avatar Sep 24 '22 07:09 MrJustreborn

+1 i need phone number support as well

nullaf avatar Mar 17 '23 14:03 nullaf