Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
HCC docs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Holland Computing Center
HCC docs
Commits
6721d8ba
Commit
6721d8ba
authored
6 years ago
by
John Thiltges
Browse files
Options
Downloads
Patches
Plain Diff
Yet another attempt at escaping JavaScript
parent
9f52e9b2
No related branches found
No related tags found
1 merge request
!40
Horrible JavaScript search redirector, mk 1b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
content/redirector.md
+1
-72
1 addition, 72 deletions
content/redirector.md
static/js/docs-redirector.js
+69
-0
69 additions, 0 deletions
static/js/docs-redirector.js
with
70 additions
and
72 deletions
content/redirector.md
+
1
−
72
View file @
6721d8ba
...
...
@@ -2,75 +2,4 @@
title
:
"
Redirector"
---
{{
`<script>
// 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;
}
});
</script>`
| safeJS }}
<script>
{{- readFile "static/js/docs-redirector.js" | safeJS -}}
</script>
This diff is collapsed.
Click to expand it.
static/js/docs-redirector.js
0 → 100644
+
69
−
0
View file @
6721d8ba
// 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
;
}
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment