var escape = require('escape-html'); // Selector to ignore var ANNOTATION_IGNORE = '.no-glossary,code,pre,a,script,h1,h2,h3,h4,h5,h6'; function pregQuote( str ) { return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1'); } function replaceText($, el, search, replace, text_only ) { return $(el).each(function(){ var node = this.firstChild, val, new_val, // Elements to be removed at the end. remove = []; // Only continue if firstChild exists. if ( node ) { // Loop over all childNodes. while (node) { // Only process text nodes. if ( node.nodeType === 3 ) { // The original node value. val = node.nodeValue; // The new value. new_val = val.replace( search, replace ); // Only replace text if the new value is actually different! if ( new_val !== val ) { if ( !text_only && /} * @param {String} glossaryFilePath * @param {HTMLDom} $ */ function annotateText(entries, glossaryFilePath, $) { entries.forEach(function(entry) { var entryId = entry.getID(); var name = entry.getName(); var description = entry.getDescription(); var searchRegex = new RegExp( '\\b(' + pregQuote(name.toLowerCase()) + ')\\b' , 'gi' ); $('*').each(function() { var $this = $(this); if ( $this.is(ANNOTATION_IGNORE) || $this.parents(ANNOTATION_IGNORE).length > 0 ) return; replaceText($, this, searchRegex, function(match) { return '' + match + ''; }); }); }); } module.exports = annotateText;