-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path404.html
145 lines (129 loc) · 2.93 KB
/
404.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
layout: quaxio
title: 404 Page Not Found
permalink: /404.html
---
<div class="index">
<h1>Sorry, the page you're trying to access doesn't exist.</h1>
<br>
<ul>
<li id="closestMatch" style="display: none"><p>Closest match: <a id="best"></a> (<span id="bestTitle"></span>).</p></li>
<li><p>Try going to the <a href="{{ site.baseurl }}/">homepage</a>.</p></li>
</ul>
</div>
<script>
// levenshtein from https://github.com/gustf/js-levenshtein
function levenshtein(a, b) {
function _min(d0, d1, d2, bx, ay) {
return d0 < d1 || d2 < d1
? d0 > d2
? d2 + 1
: d0 + 1
: bx === ay
? d1
: d1 + 1;
}
if (a === b) {
return 0;
}
if (a.length > b.length) {
var tmp = a;
a = b;
b = tmp;
}
var la = a.length;
var lb = b.length;
while (la > 0 && (a.charCodeAt(la - 1) === b.charCodeAt(lb - 1))) {
la--;
lb--;
}
var offset = 0;
while (offset < la && (a.charCodeAt(offset) === b.charCodeAt(offset))) {
offset++;
}
la -= offset;
lb -= offset;
if (la === 0 || lb < 3) {
return lb;
}
var x = 0;
var y;
var d0;
var d1;
var d2;
var d3;
var dd;
var dy;
var ay;
var bx0;
var bx1;
var bx2;
var bx3;
var vector = [];
for (y = 0; y < la; y++) {
vector.push(y + 1);
vector.push(a.charCodeAt(offset + y));
}
var len = vector.length - 1;
for (; x < lb - 3;) {
bx0 = b.charCodeAt(offset + (d0 = x));
bx1 = b.charCodeAt(offset + (d1 = x + 1));
bx2 = b.charCodeAt(offset + (d2 = x + 2));
bx3 = b.charCodeAt(offset + (d3 = x + 3));
dd = (x += 4);
for (y = 0; y < len; y += 2) {
dy = vector[y];
ay = vector[y + 1];
d0 = _min(dy, d0, d1, bx0, ay);
d1 = _min(d0, d1, d2, bx1, ay);
d2 = _min(d1, d2, d3, bx2, ay);
dd = _min(d2, d3, dd, bx3, ay);
vector[y] = dd;
d3 = d2;
d2 = d1;
d1 = d0;
d0 = dy;
}
}
for (; x < lb;) {
bx0 = b.charCodeAt(offset + (d0 = x));
dd = ++x;
for (y = 0; y < len; y += 2) {
dy = vector[y];
vector[y] = dd = _min(dy, d0, dd, bx0, vector[y + 1]);
d0 = dy;
}
}
return dd;
};
let urls = [
{% for post in site.posts %}
{ {% if post.permalink %}
url: {{ post.permalink | jsonify}},
{% elsif post.external %}
url: {{ post.external | jsonify}},
{% endif %}
title: {{ post.title | jsonify}}},
{% endfor %}
];
let current = new URL(document.location.href);
let bestScore = -1;
let bestUrl = null;
for (let i=0; i<urls.length; i++) {
let u = urls[i];
let d = levenshtein(u.url, current.pathname);
if ((bestScore == -1) || (d < bestScore)) {
bestScore = d;
bestUrl = u;
}
}
current.pathname = bestUrl;
best.innerText = bestUrl.url;
bestTitle.innerHTML = bestUrl.title;
if (bestUrl.url[bestUrl.url.length-1] == "/") {
best.href = bestUrl.url + current.search;
} else {
best.href = bestUrl.url + "/" + current.search;
}
closestMatch.style.display = "";
</script>