Skip to content

Commit 7da3c0c

Browse files
committed
use redirect in layout.ts instead of ugly DOM href surgery to forward readme links to GH repo
update Footer
1 parent e5a2a5c commit 7da3c0c

12 files changed

+43
-52
lines changed

examples/mp_bimodal_e_form.ipynb

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@
5858
"fields = [\"material_id\", \"formula_pretty\", e_form_col, \"energy_type\"]\n",
5959
"\n",
6060
"e_form_all_mp = MPRester(use_document_model=False).thermo.search(\n",
61-
" fields=fields, thermo_types=None\n",
61+
" fields=fields,\n",
62+
" # thermo_types=None,\n",
63+
" thermo_types=[\"GGA_GGA+U_R2SCAN\"],\n",
64+
" chunk_size=200,\n",
6265
")\n",
6366
"\n",
6467
"df_e_form = pd.DataFrame(e_form_all_mp).set_index(\"material_id\")\n",

readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ pip install pymatviz
2626

2727
## API Docs
2828

29-
See <https://janosh.github.io/pymatviz/api>.
29+
See the [/api] page.
30+
31+
[/api]: https://janosh.github.io/pymatviz/api
3032

3133
## Usage
3234

site/package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
},
1717
"devDependencies": {
1818
"@iconify/svelte": "^3.0.1",
19-
"@sveltejs/adapter-static": "^1.0.2",
20-
"@sveltejs/kit": "^1.0.13",
19+
"@sveltejs/adapter-static": "^1.0.4",
20+
"@sveltejs/kit": "^1.1.1",
2121
"@sveltejs/vite-plugin-svelte": "^2.0.2",
22-
"@typescript-eslint/eslint-plugin": "^5.48.1",
23-
"@typescript-eslint/parser": "^5.48.1",
24-
"eslint": "^8.31.0",
22+
"@typescript-eslint/eslint-plugin": "^5.48.2",
23+
"@typescript-eslint/parser": "^5.48.2",
24+
"eslint": "^8.32.0",
2525
"eslint-plugin-svelte3": "^4.0.0",
2626
"hastscript": "^7.2.0",
2727
"highlight.js": "^11.7.0",
2828
"mdsvex": "^0.10.6",
29-
"prettier": "^2.8.2",
29+
"prettier": "^2.8.3",
3030
"prettier-plugin-svelte": "^2.9.0",
3131
"rehype-autolink-headings": "^6.1.1",
3232
"rehype-slug": "^5.1.0",
3333
"svelte": "^3.55.1",
3434
"svelte-check": "^3.0.2",
3535
"svelte-preprocess": "^5.0.0",
3636
"svelte-toc": "^0.5.2",
37-
"svelte-zoo": "^0.2.0",
37+
"svelte-zoo": "^0.2.2",
3838
"svelte2tsx": "^0.6.0",
3939
"tslib": "^2.4.1",
4040
"typescript": "^4.9.4",

site/src/app.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
--toc-min-width: 16em;
1515
--toc-active-bg: darkcyan;
1616

17-
--ghc-color: var(--night);
18-
--ghc-bg: white;
17+
--zoo-github-corner-color: var(--night);
18+
--zoo-github-corner-bg: white;
1919
}
2020
body {
2121
background: var(--night);

site/src/lib/Footer.svelte

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
</script>
55

66
<footer>
7-
Maintained by Janosh Riebesell
7+
Questions/feedback?
8+
<a href="{repository}/issues"><Icon icon="octicon:mark-github" inline /></a>
89
<a href="mailto:[email protected]"><Icon icon="mdi:email" inline /></a>
9-
on
10-
<a href={repository}><Icon icon="octicon:mark-github" inline /></a>
1110
</footer>
1211

1312
<style>

site/src/routes/+error.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { page } from '$app/stores'
3-
import { homepage, name } from '../../package.json'
3+
import { homepage, name } from '$site/package.json'
44
55
let online: boolean
66
</script>

site/src/routes/+layout.svelte

-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,13 @@
22
import { page } from '$app/stores'
33
import Footer from '$lib/Footer.svelte'
44
import { repository } from '$site/package.json'
5-
import { onMount } from 'svelte'
65
import Toc from 'svelte-toc'
76
import { GitHubCorner } from 'svelte-zoo'
87
import '../app.css'
98
109
$: headingSelector = `main :is(${
1110
$page.url.pathname === `/api` ? `h1, h2, h3, h4` : `h2`
1211
}):not(.toc-exclude)`
13-
14-
const site_url = `https://janosh.github.io/pymatviz`
15-
onMount(() => {
16-
for (const link of [
17-
...document.querySelectorAll(`a[href^='${site_url}']`),
18-
] as HTMLAnchorElement[]) {
19-
link.href = link.href.replace(site_url, ``)
20-
link.text = link.text.replace(site_url, ``)
21-
}
22-
})
2312
</script>
2413

2514
<Toc {headingSelector} breakpoint={1250} warnOnEmpty={false} />

site/src/routes/+layout.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
import { redirect } from '@sveltejs/kit'
2+
import type { LayoutLoad } from './$types'
3+
14
export const prerender = true
5+
6+
export const load: LayoutLoad = ({ url }) => {
7+
if (url.pathname.match(`^/(examples|pymatviz)`)) {
8+
const gh_file_url = `https://github.com/janosh/pymatviz/blob/main/${url.pathname}`
9+
throw redirect(307, gh_file_url)
10+
}
11+
}

site/src/routes/+page.svelte

-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
<script lang="ts">
22
import Readme from '$root/readme.md'
3-
import { repository } from '$site/package.json'
4-
import { onMount } from 'svelte'
5-
6-
onMount(() => {
7-
for (const link of [
8-
...document.querySelectorAll(`a:has(code)`),
9-
] as HTMLAnchorElement[]) {
10-
const path = link.href.split(`/`).slice(-2).join(`/`)
11-
link.href = `${repository}/blob/main/${path}`
12-
}
13-
})
143
</script>
154

165
<main>

site/src/routes/api/+page.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<h1 class="toc-exclude">API</h1>
2-
31
<main>
2+
<h1 class="toc-exclude">API</h1>
3+
44
{#each Object.values(import.meta.glob(`./*.md`, { eager: true })) as file}
55
<svelte:component this={file?.default} />
66
{/each}

site/svelte.config.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ const rehypePlugins = [
2222
],
2323
]
2424

25+
const { default: pkg } = await import(`./package.json`, {
26+
assert: { type: `json` },
27+
})
28+
2529
/** @type {import('@sveltejs/kit').Config} */
2630
export default {
2731
extensions: [`.svelte`, `.svx`, `.md`],
2832

2933
preprocess: [
30-
preprocess(),
34+
// replace readme links to docs with site-internal links
35+
// (which don't require browser navigation)
36+
preprocess({ replace: [[pkg.homepage, ``]] }),
3137
mdsvex({
3238
rehypePlugins,
3339
extensions: [`.svx`, `.md`],
@@ -37,8 +43,10 @@ export default {
3743
kit: {
3844
adapter: adapter(),
3945

40-
prerender: {
41-
handleHttpError: `warn`,
46+
alias: {
47+
$src: `./src`,
48+
$site: `.`,
49+
$root: `..`,
4250
},
4351
},
4452
}

site/vite.config.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import { sveltekit } from '@sveltejs/kit/vite'
2-
import { resolve } from 'path'
32
import type { UserConfig } from 'vite'
43

54
const vite_config: UserConfig = {
65
plugins: [sveltekit()],
76

8-
resolve: {
9-
alias: {
10-
$src: resolve(`./src`),
11-
$site: resolve(`.`),
12-
$root: resolve(`..`),
13-
},
14-
},
15-
167
server: {
17-
fs: { allow: [`../..`] }, // needed to import from $root
8+
fs: { allow: [`..`] }, // needed to import from $root
189
port: 3000,
1910
},
2011

0 commit comments

Comments
 (0)