gregjacobs / Autolinker.js

Utility to Automatically Link URLs, Email Addresses, Phone Numbers, Twitter handles, and Hashtags in a given block of text/HTML
MIT License
1.48k stars 238 forks source link

Is there any way to add link to special text? #211

Closed xiaoxin01 closed 7 years ago

xiaoxin01 commented 7 years ago

I have a faq library. Each of faq has a id like faqxxxxx. Any of them may reference of each other.

What I want is that if an faq id exist in the faq content, it would become to a link with the site url prefix. It means if my site url is http://a.com, the faq id will change to

<a herf='http://a.com/faqxxxxx'>faqxxxxx</a>

Is this possible?

gregjacobs commented 7 years ago

Hey @xiaoxin01. What does your faq content look like exactly?

gregjacobs commented 7 years ago

Is it all on one big page btw? Or multiple pages?

xiaoxin01 commented 7 years ago

Hi @gregjacobs,

FAQ Content is HTML Based and each faq has it's separate page, eg. http://labs.mediatek.com/zh-cn/faq/FAQ19879

gregjacobs commented 7 years ago

Ok, I don't think autolinker will be able to help you then. It wouldn't know that faqs are supposed to be linked, and it also wouldn't know which faqs exist within your system.

You could always do a simple text replacement though, either server side or client side. Something like this might work client side (although it doesn't take into account if a given faq id exists or not):

htmlOfPage = htmlOfPage.replace( /faq(\d+)/, function( match, faqId ) {
    var url = 'http://mySite.com/faq/' + faqId;
    return '<a href="' + url + '">' + match + '</a>';
} );

Not sure if that helps or not but hope it does!

xiaoxin01 commented 7 years ago

@gregjacobs I will try this, thank you~