From ad901e6755e36bef2c1bf7defc332891f1487d94 Mon Sep 17 00:00:00 2001
From: John Thiltges <jthiltges2@unl.edu>
Date: Fri, 14 Dec 2018 09:46:49 -0600
Subject: [PATCH] Horrible JavaScript search redirector, mk 2

Turns out that no magic is needed. Just dump JS into markdown.
---
 content/redirector.md        | 76 +++++++++++++++++++++++++++++++++++-
 static/js/docs-redirector.js | 69 --------------------------------
 2 files changed, 75 insertions(+), 70 deletions(-)
 delete mode 100644 static/js/docs-redirector.js

diff --git a/content/redirector.md b/content/redirector.md
index 75fe28e6..fafeec82 100644
--- a/content/redirector.md
+++ b/content/redirector.md
@@ -2,4 +2,78 @@
 title: "Redirector"
 ---
 
-<script>{{- readFile "static/js/docs-redirector.js" | safeJS -}}</script>
+<script>
+
+// Redirector for hcc-docs links
+// Search for URL parameter 'q' and redirect to top match
+
+var lunrIndex;
+
+function getQueryVariable(variable) {
+    var query = window.location.search.substring(1);
+    var vars = query.split('&');
+
+    for (var i = 0; i < vars.length; i++) {
+        var pair = vars[i].split('=');
+
+        if (pair[0] === variable) {
+            return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
+        }
+    }
+}
+
+// Initialize lunrjs using our generated index file
+function initLunr() {
+    // First retrieve the index file
+    return $.getJSON(baseurl + "/index.json")
+        .done(function(index) {
+            pagesIndex = index;
+            // Set up lunrjs by declaring the fields we use
+            // Also provide their boost level for the ranking
+            lunrIndex = new lunr.Index
+            lunrIndex.ref("uri");
+            lunrIndex.field('title', {
+                boost: 15
+            });
+            lunrIndex.field('tags', {
+                boost: 10
+            });
+            lunrIndex.field("content", {
+                boost: 5
+            });
+
+            // Feed lunr with each file and let lunr actually index them
+            pagesIndex.forEach(function(page) {
+                lunrIndex.add(page);
+            });
+            lunrIndex.pipeline.remove(lunrIndex.stemmer)
+        })
+        .fail(function(jqxhr, textStatus, error) {
+            var err = textStatus + ", " + error;
+            console.error("Error getting Hugo index file:", err);
+        });
+}
+
+function search(query) {
+    // Find the item in our index corresponding to the lunr one to have more info
+    return lunrIndex.search(query).map(function(result) {
+        return pagesIndex.filter(function(page) {
+            return page.uri === result.ref;
+        })[0];
+    });
+}
+
+initLunr().then(function() {
+    var searchTerm = getQueryVariable('q');
+    // Replace non-word chars with space. lunr doesn't like quotes.
+    searchTerm = searchTerm.replace(/[\W_]+/g," ");
+    var results = search(searchTerm);
+
+    if (!results.length) {
+        window.location = baseurl;
+    } else {
+        window.location = results[0].uri;
+    }
+});
+
+</script>
diff --git a/static/js/docs-redirector.js b/static/js/docs-redirector.js
deleted file mode 100644
index 27d0da9b..00000000
--- a/static/js/docs-redirector.js
+++ /dev/null
@@ -1,69 +0,0 @@
-// Redirector for hcc-docs links
-// Search for URL parameter 'q' and redirect to top match
-
-var lunrIndex;
-
-function getQueryVariable(variable) {
-    var query = window.location.search.substring(1);
-    var vars = query.split('&');
-
-    for (var i = 0; i < vars.length; i++) {
-        var pair = vars[i].split('=');
-
-        if (pair[0] === variable) {
-            return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
-        }
-    }
-}
-
-// Initialize lunrjs using our generated index file
-function initLunr() {
-    // First retrieve the index file
-    return $.getJSON(baseurl + "/index.json")
-        .done(function(index) {
-            pagesIndex = index;
-            // Set up lunrjs by declaring the fields we use
-            // Also provide their boost level for the ranking
-            lunrIndex = new lunr.Index
-            lunrIndex.ref("uri");
-            lunrIndex.field('title', {
-                boost: 15
-            });
-            lunrIndex.field('tags', {
-                boost: 10
-            });
-            lunrIndex.field("content", {
-                boost: 5
-            });
-
-            // Feed lunr with each file and let lunr actually index them
-            pagesIndex.forEach(function(page) {
-                lunrIndex.add(page);
-            });
-            lunrIndex.pipeline.remove(lunrIndex.stemmer)
-        })
-        .fail(function(jqxhr, textStatus, error) {
-            var err = textStatus + ", " + error;
-            console.error("Error getting Hugo index file:", err);
-        });
-}
-
-function search(query) {
-    // Find the item in our index corresponding to the lunr one to have more info
-    return lunrIndex.search(query).map(function(result) {
-        return pagesIndex.filter(function(page) {
-            return page.uri === result.ref;
-        })[0];
-    });
-}
-
-initLunr().then(function() {
-    var searchTerm = getQueryVariable('q');
-    var results = search(searchTerm);
-
-    if (!results.length) {
-        window.location = baseurl;
-    } else {
-        window.location = results[0].uri;
-    }
-});
-- 
GitLab