|
1 | 1 |
|
2 | 2 | <script lang="ts"> |
3 | 3 | import { base } from "$app/paths"; |
4 | | - import { beforeNavigate, goto, onNavigate } from '$app/navigation'; |
5 | 4 | import type { RawChapter } from "./endpoints/toc/+server"; |
6 | 5 | import "../app.css"; |
7 | 6 |
|
8 | 7 | export let data; |
9 | 8 | export let toc: RawChapter[] = data.toc ?? []; |
10 | | -
|
11 | | - const library = import.meta.env.DOCS_LIBRARY || 'transformers'; |
12 | | - const version = import.meta.env.DOCS_VERSION || 'main'; |
13 | | - const lang = import.meta.env.DOCS_LANGUAGE || 'en'; |
14 | | -
|
15 | | - const redirectedPaths: Record<string, string> = {} |
16 | | -
|
17 | | - beforeNavigate(async ({ to, cancel }) => { |
18 | | - const pathname = to?.url.pathname ?? ""; |
19 | | - const search = to?.url.search ?? ""; |
20 | | - const hash = to?.url.hash ?? ""; |
21 | | - const path = pathname + search + hash; |
22 | | - if(/^\/(docs|learn)/.test(path)){ |
23 | | - const params = path.slice(1).split("/"); |
24 | | - const _docType = params.shift(); |
25 | | - const _library = params.shift(); |
26 | | - const isCourse = _docType === "learn"; |
27 | | - const versionRegex = isCourse ? /^(?:pr_\d+)$/ : /^(?:(master|main)|v[\d.]+(rc\d+)?|pr_\d+)$/; |
28 | | - const _version = versionRegex.test(params[0]) ? params.shift() : undefined; |
29 | | - const _lang = /^[a-z]{2}(-[A-Z]{2})?$/.test(params[0]) ? params.shift() : undefined; |
30 | | - const newChapterId = params.join("/"); |
31 | | -
|
32 | | - if (library === _library && (!_version || !_lang)){ |
33 | | - cancel(); |
34 | | - const redirectedPath = `/docs/${library}/${version}/${lang}/${newChapterId}`; |
35 | | - redirectedPaths[redirectedPath] = path; |
36 | | - await goto(redirectedPath); |
37 | | - return false; |
38 | | - } |
39 | | - } |
40 | | - }); |
41 | | -
|
42 | | - onNavigate(({ to }) => { |
43 | | - const pathname = to?.url.pathname ?? ""; |
44 | | - const search = to?.url.search ?? ""; |
45 | | - const hash = to?.url.hash ?? ""; |
46 | | - const path = pathname + search + hash; |
47 | | - if(redirectedPaths[path]){ |
48 | | - history.replaceState({}, "", redirectedPaths[path]) |
49 | | - } |
50 | | - }); |
51 | 9 | </script> |
52 | 10 |
|
53 | 11 | {#if !import.meta.env.DEV} |
|
0 commit comments