[jQuery] Re: Plugin to link words/phrases
@Renato Formato: Very nice. Perhaps DOM tree walking would be better than my blanket regex over HTML... Thanks.
[jQuery] Re: Plugin to link words/phrases
Michael Edmondson ha scritto: I am working on writing a plugin that, given a list of words/phrases, will link text. That sounds so ... less than spectacular. The concept is similar to those in-text ads, except without the popups/bubbles. (Even less spectacular-sounding.) I was wondering if there already existed a plugin for this or similar functionality. I was also wondering if it would be of interest to anyone else. you can give a look at the SearchHighlight plugin (http://www.jquery.info/spip.php?article50). It is search related but using the keys option can do what you want. Ciao Renato
[jQuery] Re: Plugin to link words/phrases
My latest attempt tries to handle if the "unlink" file is missing and uses a linkTopicsOnce setting: (function($) { $.fn.extend({ linktopics: function( settings ) { var self = this; this.settings = $.extend({}, $.linktopics.defaults, settings); this.topics = null; this.trimTopics = function () { for ( var i = 0; i < self.topics.length; i++ ) { var allowed = true; for ( var j = 0, numStopLinks = self.stopLinks.length; j < numStopLinks; j++ ) { if ( self.stopLinks[j] == self.topics[i].topic ) { allowed = false; break; } } if ( ! allowed ) { void( self.topics.splice( i, 1 ) ); } } }; this.setupRegexes = function () { for ( var i = 0, numTopics = self.topics.length; i < numTopics; i+ + ) { self.topics[i].linked = false; self.topics[i].strings.sort(function(a,b) { // longer strings come first if ( a.length > b.length ) { return -1; } else if ( a.length < b.length ) { return 1; } else { return 0; } }); self.topics[i].regexes = new Array(self.topics[i].strings.length); for ( var j = 0, numStrings = self.topics[i].strings.length; j < numStrings; j++ ) { self.topics[i].regexes[j] = new RegExp( "(^|\\W)(" + self.topics[i].strings[j].replace( /([\[\]\(\)\.\*\$\^\?\+\\])/g, '\\ $1' ) + ")((?!\\w)(?![^<]*(?:>|<\\/a)))" ); } } }; this.linkHtml = function () { self.each(function() { for ( var i = 0, numTopics = self.topics.length; i < numTopics; i+ + ) { if ( self.topics[i].linked == false ) { for ( var j = 0, numRegexes = self.topics[i].regexes.length; j < numRegexes; j++ ) { if ( $(this).html().search( self.topics[i].regexes[j] ) != -1 ) { $(this).html( $ (this).html().replace( self.topics[i].regexes[j], '$1$2<\/a>$3' ) ); if ( self.settings.linkTopicsOnce ) { self.topics[i].linked = true; break; } } } } } }); }; $.getJSON( self.settings.topicLinkJsonUrl, function(json) { if ( typeof json.topics == "object" ) { self.topics = json.topics; self.stopLinks = new Array(); $.ajax({ type: "GET", url: self.settings.topicUnlinkJsonUrl, dataType: "json", success: function(json){ if ( typeof json.stoplinks == "object" ) { self.stopLinks = json.stoplinks; }
[jQuery] Re: Plugin to link words/phrases
What happens if there is other HTML -- be it other spans or links -- in the paragraph?
[jQuery] Re: Plugin to link words/phrases
what my plugin does is change now is the time for all good programmer ... to now is the time for all good programmer ... if good is in the hash to be hooked... then you can get all your span.hookedto do whatever you want. On 6/3/07, Rhapidophyllum <[EMAIL PROTECTED]> wrote: Could you be more specific on what you mean by 'link text'? That could be interpreted a number of different ways. On Jun 1, 2007, at 11:11 PM, Michael Edmondson wrote: > > I am working on writing a plugin that, given a list of words/phrases, > will link text. > > That sounds so ... less than spectacular. The concept is similar to > those in-text ads, except without the popups/bubbles. (Even less > spectacular-sounding.) > > I was wondering if there already existed a plugin for this or similar > functionality. > > I was also wondering if it would be of interest to anyone else. > -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ
[jQuery] Re: Plugin to link words/phrases
Could you be more specific on what you mean by 'link text'? That could be interpreted a number of different ways. On Jun 1, 2007, at 11:11 PM, Michael Edmondson wrote: I am working on writing a plugin that, given a list of words/phrases, will link text. That sounds so ... less than spectacular. The concept is similar to those in-text ads, except without the popups/bubbles. (Even less spectacular-sounding.) I was wondering if there already existed a plugin for this or similar functionality. I was also wondering if it would be of interest to anyone else.
[jQuery] Re: Plugin to link words/phrases
This hasn't been strenuously tested yet... What little I've put together so far is here: http://cosmicforge.com/topics/ This is my first time using jQuery, and so I'm attempting to make a proper plugin instead of just random functions. The functionality is very focused as written -- it purposefully only links the first occurrence found, but I suppose that part could be made optional -- oh, and it doesn't do any error checking, and it doesn't work if the JSON files are missing, so there's plenty of work still to be done. And for those of you who just want to look at code... jQuery(function($) { $("div.storytext > p").add("div.storytext > ol > li").add("div.storytext > ul > li").linktopics(); }); A single (object) param to be passed to linktopics: { topicLinkJsonUrl: "URL to JSON file with topic data", topicUnlinkJsonUrl: "URL to JSON file with list of topics NOT to link" } JSON for topics to be linked: { "topics": [ { "topic": "jQuery", "link": "http://jquery.com/";, "strings": [ "jQuery", "Javascript framework", "John Resig" ] }, ... } JSON for topics NOT to be linked: { "stoplinks": [ "Michael Edmondson" ] } /** * jquery.linktopics.js */ (function($) { $.fn.extend({ linktopics: function( settings ) { this.settings = $.extend({}, $.linktopics.defaults, settings); this.topics = null; var self = this; $.getJSON( self.settings.topicLinkJsonUrl, function(json) { if ( typeof json.topics == "object" ) { self.topics = json.topics; self.stopLinks = new Array(); $.getJSON( self.settings.topicUnlinkJsonUrl, function(json) { if ( typeof json.stoplinks == "object" ) { self.stopLinks = json.stoplinks; } for ( var i = 0; i < self.topics.length; i++ ) { var allowed = true; for ( var j = 0, numStopLinks = self.stopLinks.length; j < numStopLinks; j++ ) { if ( self.stopLinks[j] == self.topics[i].topic ) { allowed = false; break; } } if ( ! allowed ) { void( self.topics.splice( i, 1 ) ); } } for ( var i = 0, numTopics = self.topics.length; i < numTopics; i ++ ) { self.topics[i].linked = false; self.topics[i].strings.sort(function(a,b) { // longer strings come first if ( a.length > b.length ) { return -1; } else if ( a.length < b.length ) { return 1; } else { return 0; } }); self.topics[i].regexes = new Array(self.topics[i].strings.length); for ( var j = 0, numStrings = self.topics[i].strings.length; j < numStrings; j++ ) { self.topics[i].regexes[j] = new RegExp( "(^|\\W)(" + self.topics[i].strings[j].replace( /([\[\]\(\)\.\*\$\^\?\+\\])/g, '\\ $1' ) + ")((?!\\w)(?![^<]*(?:>|<\\/a)))" ); } }
[jQuery] Re: Plugin to link words/phrases
take a look at http://jpassoc.com/js/jquery-kidsbook.js as used in http://jpassoc.com/junior/story/ On 6/1/07, Michael Edmondson <[EMAIL PROTECTED]> wrote: I am working on writing a plugin that, given a list of words/phrases, will link text. That sounds so ... less than spectacular. The concept is similar to those in-text ads, except without the popups/bubbles. (Even less spectacular-sounding.) I was wondering if there already existed a plugin for this or similar functionality. I was also wondering if it would be of interest to anyone else. -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ