Skip to content

Commit f2ad85a

Browse files
committed
Fix error-index redirect to work with back button.
1 parent e94fab6 commit f2ad85a

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/tools/error_index_generator/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ fn add_rust_attribute_on_codeblock(explanation: &str) -> String {
9898

9999
fn render_html(output_path: &Path) -> Result<(), Box<dyn Error>> {
100100
let mut introduction = format!(
101-
"<script src='redirect.js'></script>
102-
# Rust error codes index
101+
"# Rust error codes index
103102
104103
This page lists all the error codes emitted by the Rust compiler.
105104
@@ -149,7 +148,12 @@ This page lists all the error codes emitted by the Rust compiler.
149148
book.book.sections.push(BookItem::Chapter(chapter));
150149
book.build()?;
151150

152-
// We can't put this content into another file, otherwise `mbdbook` will also put it into the
151+
// The error-index used to be generated manually (without mdbook), and the
152+
// index was located at the top level. Now that it is generated with
153+
// mdbook, error-index.html has moved to error_codes/error-index.html.
154+
// This adds a redirect so that old links go to the new location.
155+
//
156+
// We can't put this content into another file, otherwise `mdbook` will also put it into the
153157
// output directory, making a duplicate.
154158
fs::write(
155159
output_path.join("error-index.html"),
@@ -163,7 +167,6 @@ This page lists all the error codes emitted by the Rust compiler.
163167
</head>
164168
<body>
165169
<div>If you are not automatically redirected to the error code index, please <a id="index-link" href="./error_codes/error-index.html">here</a>.
166-
<script>document.getElementById("index-link").click()</script>
167170
</body>
168171
</html>"#,
169172
)?;

src/tools/error_index_generator/redirect.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
let code = window.location.hash.replace(/^#/, '');
44
// We have to make sure this pattern matches to avoid inadvertently creating an
55
// open redirect.
6-
if (!/^E[0-9]+$/.test(code)) {
6+
if (/^E[0-9]+$/.test(code)) {
7+
window.location.replace('./error_codes/' + code + '.html');
78
return;
89
}
9-
if (window.location.pathname.indexOf("/error_codes/") !== -1) {
10-
// We're not at the top level, so we don't prepend with "./error_codes/".
11-
window.location = './' + code + '.html';
12-
} else {
13-
window.location = './error_codes/' + code + '.html';
14-
}
1510
}
11+
window.location.replace('./error_codes/error-index.html');
1612
})()

0 commit comments

Comments
 (0)