Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phone number support #133

Open
rlaferla opened this issue May 11, 2016 · 9 comments
Open

Phone number support #133

rlaferla opened this issue May 11, 2016 · 9 comments
Milestone

Comments

@rlaferla
Copy link

rlaferla commented May 11, 2016

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

@atgillette
Copy link

+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
Copy link

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

@sambegin
Copy link

sambegin commented Sep 7, 2016

+1 ,

Would be a huge improvement for mobile app !

@bilalizloo
Copy link

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

@chakafasano88
Copy link

+1 will phone number parsing be implemented?

@siwalikm
Copy link

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

@Noitidart
Copy link

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?

@MrJustreborn
Copy link

+1
would need it for german and US phonenumbers

@nullaf
Copy link

nullaf commented Mar 17, 2023

+1
i need phone number support as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants