From 6f71870755ef4b5dfda6b0ce5ea82a7380e8bdd0 Mon Sep 17 00:00:00 2001 From: Leon Yu Date: Sat, 20 Feb 2016 03:12:25 -0500 Subject: [PATCH] basic autolinker --- autolinker/autolinker-tests.ts | 153 +++++++++++++++++++++++++++++++++ autolinker/autolinker.d.ts | 45 ++++++++++ 2 files changed, 198 insertions(+) create mode 100644 autolinker/autolinker-tests.ts create mode 100644 autolinker/autolinker.d.ts diff --git a/autolinker/autolinker-tests.ts b/autolinker/autolinker-tests.ts new file mode 100644 index 000000000..c25bedbbc --- /dev/null +++ b/autolinker/autolinker-tests.ts @@ -0,0 +1,153 @@ +/// + +import AutolinkerCJS = require('autolinker'); + +() => { + let linkedText1 = Autolinker.link( "Check out google.com" ); + + new Autolinker(); + let autolinker1 = new Autolinker( { className: "myLink" } ); + let textToAutoLink = 'text'; + let linkedText2 = autolinker1.link( textToAutoLink ); + + let linkedText3 = Autolinker.link( "Check out google.com", { className: "myLink" } ); + let linkedText4 = Autolinker.link( "Check out google.com", { newWindow: false } ); + let linkedText5 = Autolinker.link( "http://www.yahoo.com/some/long/path/to/a/file", { truncate: 25, newWindow: false } ); + let myTextEl = document.getElementById( 'text' ); + myTextEl.innerHTML = Autolinker.link( myTextEl.innerHTML ); + let autolinker2 = new Autolinker( { newWindow: false, truncate: 25 } ); + + autolinker2.link( "Check out http://www.yahoo.com/some/long/path/to/a/file" ); + // Produces: "Check out yahoo.com/some/long/pat.." + + autolinker2.link( "Go to www.google.com" ); + // Produces: "Go to google.com" + + let input = "..."; // string with URLs, Email Addresses, Twitter Handles, and Hashtags + + let linkedText6 = Autolinker.link( input, { + replaceFn : function( autolinker, match ) { + console.log( "href = ", match.getAnchorHref() ); + console.log( "text = ", match.getAnchorText() ); + + switch( match.getType() ) { + case 'url' : + console.log( "url: ", match.getUrl() ); + + if( match.getUrl().indexOf( 'mysite.com' ) === -1 ) { + let tag = autolinker.getTagBuilder().build( match ); // returns an `Autolinker.HtmlTag` instance, which provides mutator methods for easy changes + tag.setAttr( 'rel', 'nofollow' ); + tag.addClass( 'external-link' ); + + return tag; + + } else { + return true; // let Autolinker perform its normal anchor tag replacement + } + + case 'email' : + let email = match.getEmail(); + console.log( "email: ", email ); + + if( email === "my@own.address" ) { + return false; // don't auto-link this particular email address; leave as-is + } else { + return; // no return value will have Autolinker perform its normal anchor tag replacement (same as returning `true`) + } + + case 'phone' : + let phoneNumber = match.getPhoneNumber(); + console.log( phoneNumber ); + + return '' + phoneNumber + ''; + + case 'twitter' : + let twitterHandle = match.getTwitterHandle(); + console.log( twitterHandle ); + + return '' + twitterHandle + ''; + + case 'hashtag' : + let hashtag = match.getHashtag(); + console.log( hashtag ); + + return '' + hashtag + ''; + } + } + } ); +} + +() => { + let linkedText1 = AutolinkerCJS.link( "Check out google.com" ); + + new AutolinkerCJS(); + let autolinker1 = new AutolinkerCJS( { className: "myLink" } ); + let textToAutoLink = 'text'; + let linkedText2 = autolinker1.link( textToAutoLink ); + + let linkedText3 = AutolinkerCJS.link( "Check out google.com", { className: "myLink" } ); + let linkedText4 = AutolinkerCJS.link( "Check out google.com", { newWindow: false } ); + let linkedText5 = AutolinkerCJS.link( "http://www.yahoo.com/some/long/path/to/a/file", { truncate: 25, newWindow: false } ); + let myTextEl = document.getElementById( 'text' ); + myTextEl.innerHTML = AutolinkerCJS.link( myTextEl.innerHTML ); + let autolinker2 = new AutolinkerCJS( { newWindow: false, truncate: 25 } ); + + autolinker2.link( "Check out http://www.yahoo.com/some/long/path/to/a/file" ); + // Produces: "Check out yahoo.com/some/long/pat.." + + autolinker2.link( "Go to www.google.com" ); + // Produces: "Go to google.com" + + let input = "..."; // string with URLs, Email Addresses, Twitter Handles, and Hashtags + + let linkedText6 = AutolinkerCJS.link( input, { + replaceFn : function( autolinker, match ) { + console.log( "href = ", match.getAnchorHref() ); + console.log( "text = ", match.getAnchorText() ); + + switch( match.getType() ) { + case 'url' : + console.log( "url: ", match.getUrl() ); + + if( match.getUrl().indexOf( 'mysite.com' ) === -1 ) { + let tag = autolinker.getTagBuilder().build( match ); // returns an `AutolinkerCJS.HtmlTag` instance, which provides mutator methods for easy changes + tag.setAttr( 'rel', 'nofollow' ); + tag.addClass( 'external-link' ); + + return tag; + + } else { + return true; // let AutolinkerCJS perform its normal anchor tag replacement + } + + case 'email' : + let email = match.getEmail(); + console.log( "email: ", email ); + + if( email === "my@own.address" ) { + return false; // don't auto-link this particular email address; leave as-is + } else { + return; // no return value will have AutolinkerCJS perform its normal anchor tag replacement (same as returning `true`) + } + + case 'phone' : + let phoneNumber = match.getPhoneNumber(); + console.log( phoneNumber ); + + return '' + phoneNumber + ''; + + case 'twitter' : + let twitterHandle = match.getTwitterHandle(); + console.log( twitterHandle ); + + return '' + twitterHandle + ''; + + case 'hashtag' : + let hashtag = match.getHashtag(); + console.log( hashtag ); + + return '' + hashtag + ''; + } + } + } ); +} diff --git a/autolinker/autolinker.d.ts b/autolinker/autolinker.d.ts new file mode 100644 index 000000000..577f670a2 --- /dev/null +++ b/autolinker/autolinker.d.ts @@ -0,0 +1,45 @@ +// Type definitions for autolinker v0.24.0 +// Project: https://github.com/gregjacobs/Autolinker.js +// Definitions by: Leon Yu +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace __Autolinker { + interface ConfigOptions { + className?: string; + email?: boolean; + hashtag?: boolean | string; + newWindow?: boolean; + phone?: boolean; + replaceFn?: (autolinker: Autolinker, match: any) => string; + stripPrefix?: boolean; + truncate?: number | { length?: number; location?: string; }; + twitter?: boolean; + urls?: boolean | { schemeMatches: boolean; wwwMatches: boolean; tldMatches: boolean; } + } + + interface Autolinker { + getTagBuilder(): any; + /** + * Automatically links URLs, Email addresses, Phone numbers, Twitter handles, and Hashtags found in the given chunk of HTML. Does not link URLs found within HTML tags. + */ + link(textOrHtml: string): string; + /** + * Parses the input textOrHtml looking for URLs, email addresses, phone numbers, username handles, and hashtags (depending on the configuration of the Autolinker instance), and returns an array of Autolinker.match.Match objects describing those matches. + */ + parse(textOrHtml: string): any[]; + } + + interface Static { + new(cfg?: ConfigOptions): Autolinker; + /** + * Automatically links URLs, Email addresses, Phone Numbers, Twitter handles, and Hashtags found in the given chunk of HTML. Does not link URLs found within HTML tags. + */ + link(textOrHtml: string, options?: ConfigOptions): string + } +} + +declare var Autolinker: __Autolinker.Static; + +declare module "autolinker" { + export = Autolinker; +}