Skip to content

Node.js based forum software built for the modern web

Explanations and simplifications

Peter Jaszkowiake3dd68e19cd0
diff --git a/public/src/admin/modules/search.js b/public/src/admin/modules/search.js
index 0a5d3dde56..c7f660149a 100644
--- a/public/src/admin/modules/search.js
+++ b/public/src/admin/modules/search.js
@@ -19,11 +19,16 @@ define(function () {
var title = params.title == null ? nsToTitle(namespace) : params.title;
var results = translations
+ // remove all lines without a match
.replace(new RegExp('^(?:(?!' + term + ').)*$', 'gmi'), '')
+ // get up to 25 characaters of context on both sides of the match
+ // and wrap the match in a `.search-match` element
.replace(
new RegExp('^[\\s\\S]*?(.{0,25})(' + term + ')(.{0,25})[\\s\\S]*?$', 'gmi'),
'...$1<span class="search-match">$2</span>$3...<br>'
- ).replace(/(\n ?)+/g, '\n');
+ )
+ // collapse whitespace
+ .replace(/(?:\n ?)+/g, '\n');
return '<li role="presentation" class="result">' +
'<a role= "menuitem" href= "' + RELATIVE_PATH + '/' + namespace + '" >' +
diff --git a/src/admin/search.js b/src/admin/search.js
index 985ea51a05..e97e8d177e 100644
--- a/src/admin/search.js
+++ b/src/admin/search.js
@@ -60,10 +60,42 @@ function getAdminNamespaces() {
var fallbackCache = {};
+function removeTranslatorPatterns(str) {
+ var len = str.len;
+ var cursor = 0;
+ var lastBreak = 0;
+ var level = 0;
+ var out = '';
+ var sub;
+
+ while (cursor < len) {
+ sub = str.slice(cursor, cursor + 2);
+ if (sub === '[[') {
+ if (level === 0) {
+ out += str.slice(lastBreak, cursor);
+ }
+ level += 1;
+ cursor += 2;
+ } else if (sub === ']]') {
+ level -= 1;
+ cursor += 2;
+ if (level === 0) {
+ lastBreak = cursor;
+ }
+ } else {
+ cursor += 1;
+ }
+ }
+ out += str.slice(lastBreak, cursor);
+ return out;
+}
+
function fallback(namespace) {
fallbackCache[namespace] = fallbackCache[namespace] ||
readFile(path.resolve('./public/templates/', namespace + '.tpl'))
.then(function (template) {
+ // reduce the template to just meaningful text
+ // remove scripts, etc and replace all tags with divs
var translations = sanitize(template, {
transformTags: {
'*': function () {
@@ -73,15 +105,20 @@ function fallback(namespace) {
}
}
})
- .replace(/(<div>)|(<\/div>)/g, '')
+ // remove all html tags, templating stuff, and translation strings
+ .replace(/(?:<div>)|(?:<\/div>)|(?:\{[^\{\}]*\})/g, '')
+ // collapse whitespace
.replace(/([\n\r]+ ?)+/g, '\n')
.replace(/[\t ]+/g, ' ');
+ translations = removeTranslatorPatterns(translations);
+
return {
namespace: namespace,
translations: translations,
};
});
+
return fallbackCache[namespace];
}
@@ -109,10 +146,6 @@ function initDict(language) {
})
.catch(function () {
return { namespace: namespace, translations: '' };
- })
- .then(function (params) {
- params.translations = params.translations.replace(/\{[^\{\}]*\}/g, '');
- return params;
});
}));
});