This plug-in simulates how a wiki links between pages. By specifying the root URL of the wiki, terms in normal HTML can link to the corresponding wiki page. It also supports the use of aliases for terms.
/** * @example $(function() { $("p.info").wiki({wikiRoot: "http://www.example-url.com/wiki/"}); } * @desc Converts any text between [[..]] into a link to a wiki page */ (function($) { $.fn.extend({ wiki: function(options) { var settings = $.extend({}, $.fn.wiki.defaults, options); var regex = /\[\[[^(\[\[)]+?\]\]/g; return this.each(function() { $this = $(this); var html = $this.html(); var matches = html.match(regex); if (matches) { jQuery.each(matches, function(i, m) { var s = m.replace(/[\[\]]/g, ""); var label = s; s = s.replace(/\<.+?\>/g, ""); var link = s.toLowerCase(); var alt = s; if (m.indexOf("|") != -1) { var arr = s.split("|"); link = arr[1].toLowerCase(); label = arr[0]; alt = arr[1]; } alt = alt.replace(/'/g, "'"); link = settings.wikiRoot + link.replace(/\W/g, settings.nonWordChar); html = html.replace(new RegExp(m.replace(/(\W)/g, "\\$1"), "g"), "<a href='" + link + "' target='" + settings.target + "' alt='" + alt + "' title='" + settings.titlePrefix + alt + ( settings.wikiClass ? "' class='" + settings.wikiClass : "" ) + "'>" + label + "</a>" ); }); $this.html(html); } }); } }); $.fn.wiki.defaults = { wikiRoot: "#", target: "_self", titlePrefix: "", nonWordChar: "_", wikiClass: null }; })(jQuery);