Skip to content

Commit 24abbb0

Browse files
committed
vimhelp: Add redirect page which allows us to serve a permanent link
This way, even if a tag changes location later, the redirect will always be able to find it and redirect to the correct location. Only issue is that it does require JavaScript to work. The way it works is by embedding the tags.json directly in the page, and then the JS can just simply do a check to see if the tag match.
1 parent 416109d commit 24abbb0

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

vimhelp/scripts/h2h.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def main():
7070
"--output-tags-json",
7171
"-J",
7272
action="store_true",
73-
help="Output a tags.json file, used for client-side search (when not using online version)",
73+
help="Output tags.json file and client-side tags redirect/search, useful for static page",
7474
)
7575
parser.add_argument(
7676
"--profile", "-P", action="store_true", help="Profile performance"
@@ -153,6 +153,13 @@ def run(args):
153153
if args.out_dir is not None:
154154
with (args.out_dir / out_filename).open("w") as f:
155155
f.write(json)
156+
print("Generating redirect.html...")
157+
redirect_html = h2h.to_redirect_html()
158+
out_filename = "redirect.html"
159+
if args.out_dir is not None:
160+
with (args.out_dir / out_filename).open("w") as f:
161+
f.write(prelude)
162+
f.write(redirect_html)
156163

157164
if args.out_dir is not None:
158165
print("Symlinking static files...")

vimhelp/static/vimhelp-redirect.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
var tagname = new URL(document.URL).searchParams.get('tag')
4+
if (tagname && window.vimtags) {
5+
var redirectUrl = window.vimtags[tagname];
6+
if (redirectUrl) {
7+
window.location = redirectUrl;
8+
}
9+
}

vimhelp/templates/page.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
<link rel="stylesheet" href="{{static_dir}}vimhelp-v5.css" type="text/css">
3030
<noscript><link rel="stylesheet" href="{{static_dir}}noscript.css" type="text/css"></noscript>
31+
{% if redirect %}
32+
<script>window.vimtags = {{tags_json|safe}}</script>
33+
<script src="{{static_dir}}vimhelp-redirect.js"></script>
34+
{% endif %}
3135
<script defer src="{{static_dir}}vimhelp-v5.js"></script>
3236
{# MacVim extension: Offline light/dark mode selection using JavaScript #}
3337
{% if project.name == 'MacVim' %}
@@ -119,6 +123,10 @@ <h1>{{project.name}} help files</h1>
119123
<pre>
120124
{{content}}
121125
</pre>
126+
{% if redirect %}
127+
<noscript>Redirect requires JavaScript to work.</noscript>
128+
Could not find tag.
129+
{% endif %}
122130
</div>
123131
</main>
124132
<p>{{sitenavi}}</p>

vimhelp/vimhelp/vimh2h.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,34 @@ def gen_tags_json(self):
240240
return json.dumps(obj, sort_keys=True)
241241
return out
242242

243+
def to_redirect_html(self):
244+
tags_json = self.gen_tags_json()
245+
246+
content = ""
247+
248+
filename = "redirect"
249+
static_dir = "/" if self._mode == "online" else ""
250+
helptxt = "./" if self._mode == "online" else "index.html"
251+
sidebar_headings = None
252+
253+
current_time = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%SZ")
254+
255+
return flask.render_template(
256+
"page.html",
257+
redirect=True,
258+
tags_json=tags_json,
259+
mode=self._mode,
260+
project=self._project,
261+
version=self._version,
262+
filename=filename,
263+
static_dir=static_dir,
264+
helptxt=helptxt,
265+
content=content,
266+
sidebar_headings=sidebar_headings,
267+
current_time=current_time,
268+
commit=self._commit,
269+
)
270+
243271
def to_html(self, filename, contents):
244272
is_help_txt = filename == "help.txt"
245273
lines = [line.rstrip("\r\n") for line in RE_NEWLINE.split(contents)]

0 commit comments

Comments
 (0)