Skip to content

Commit ca6fa64

Browse files
committed
Adapt links with current language; make dev server support URL params
This ensures that after navigating to another page the current language is still active
1 parent 1da757b commit ca6fa64

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

README_AUTHORS.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ in the root folder of this project once for the setup, then to actually run the
3030
npm start
3131
```
3232

33-
Then, open http://localhost:1337/README.md in your browser.
34-
35-
Alternatively, and even easier, run
36-
37-
```sh
38-
npm run watch
39-
```
40-
41-
And the browser will automatically open (on port 3000) and automatically reload on every saved change.
33+
Then, open http://localhost:1337/README.md in your browser and the browser will automatically open (on port 3000 or the next free port) and automatically reload on every saved change.
4234

4335

4436
## Writing *one* document which covers both JavaScript and TypeScript without duplication

assets/js/custom.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,27 @@ function replaceFileExtensions(lang) {
208208
}
209209
}
210210

211+
function updatePrevNextLinks(lang) {
212+
const links = document.querySelectorAll('a[href$="README.md"], a[href$="/"], a[href$="README.md?lang=js"], a[href$="/?lang=js"]');
213+
links.forEach(link => {
214+
// Only handle relative links (not starting with http://, https://, or //)
215+
if (/^(https?:)?\/\//.test(link.getAttribute("href"))) {
216+
return;
217+
}
218+
219+
let url = new URL(link.href, window.location.origin);
220+
221+
// Remove any existing lang param
222+
url.searchParams.delete("lang");
223+
224+
if (lang === "js") {
225+
url.searchParams.set("lang", "js");
226+
}
227+
228+
link.href = url.pathname + url.search + url.hash;
229+
});
230+
}
231+
211232
// dynamic overall language switching
212233

213234
function addLanguageSwitchButtons() {
@@ -236,6 +257,7 @@ function switchLanguage(newLang) {
236257
replaceFileExtensions(lang);
237258
resetCodeCoupleButtons();
238259
updateAllCodeCouples(lang);
260+
updatePrevNextLinks(lang);
239261
}
240262

241263
function updateAllCodeCouples(globalLang) {
@@ -255,5 +277,6 @@ document.addEventListener("DOMContentLoaded", (event) => {
255277
replaceFileExtensions(lang);
256278
addLanguageSwitchButtons();
257279
updateAllCodeCouples(lang);
280+
updatePrevNextLinks(lang);
258281
});
259282
});

tools/dev-server/server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ app.use("/node_modules", express.static(join(cwd, "node_modules")));
4444

4545
app.use(async (req, res, next) => {
4646
let file, url;
47-
if (req.url.endsWith("/")) {
47+
const reqUrlWithoutParams = req.url.split("?")[0];
48+
if (reqUrlWithoutParams.endsWith("/")) {
4849
for (const index of ["index.md", "README.md"]) {
49-
url = `${req.url}${index}`;
50+
url = `${reqUrlWithoutParams}${index}`;
5051
file = join(cwd, url);
5152
if (existsSync(file) && statSync(file).isFile()) {
5253
break;
@@ -55,7 +56,7 @@ app.use(async (req, res, next) => {
5556
}
5657
}
5758
} else {
58-
file = join(cwd, req.url);
59+
file = join(cwd, reqUrlWithoutParams);
5960
if (!(existsSync(file) && statSync(file).isFile())) {
6061
file = undefined;
6162
}
@@ -65,7 +66,7 @@ app.use(async (req, res, next) => {
6566
const bodyContent = await convertMarkdown(md);
6667
const templateFn = await getTemplate();
6768
// get title as first line in the md file which starts with hashes, which indicates it is a title of some kind
68-
const title = md.match(/^##* (.+)$/m)?.[1] || req.url;
69+
const title = md.match(/^##* (.+)$/m)?.[1] || reqUrlWithoutParams;
6970
const html = templateFn({ title, bodyContent });
7071
res.send(html);
7172
} else if (file) {

0 commit comments

Comments
 (0)