Skip to content

Commit c65365d

Browse files
authored
docs: fix the website after the move to Starlight (#1377)
1 parent 6b6c188 commit c65365d

34 files changed

Lines changed: 232 additions & 74 deletions

‎website/astro.config.js‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { defineConfig } from 'astro/config'
22
import { unified } from '@astrojs/markdown-remark'
33
import starlight from '@astrojs/starlight'
44
import llmsTxt from 'starlight-llms-txt'
5-
import { rehypeTrailingSlash } from './rehype.js'
5+
import {
6+
rehypeExternalLinks,
7+
rehypeTrailingSlash
8+
} from './rehype.js'
69

710
const isProduction = process.env.NODE_ENV === 'production'
811
const chartTags = [
@@ -23,9 +26,30 @@ export default defineConfig({
2326
trailingSlash: 'always',
2427
markdown: {
2528
processor: unified({
26-
rehypePlugins: [rehypeTrailingSlash]
29+
// Docusaurus kept straight quotes and dots, and error messages are quoted as they are
30+
smartypants: false,
31+
rehypePlugins: [rehypeTrailingSlash, rehypeExternalLinks]
2732
})
2833
},
34+
vite: {
35+
plugins: [
36+
{
37+
name: 'sandbox-index',
38+
// Unlike GitHub Pages, the dev server does not answer a directory of `public` with its index.html
39+
configureServer(server) {
40+
server.middlewares.use((req, _res, next) => {
41+
const [path, query] = (req.url ?? '').split('?')
42+
43+
if (path.startsWith('/sandboxes/') && path.endsWith('/')) {
44+
req.url = `${path}index.html${query === undefined ? '' : `?${query}`}`
45+
}
46+
47+
next()
48+
})
49+
}
50+
}
51+
]
52+
},
2953
// Pages of the Docusaurus website that have no counterpart
3054
redirects: {
3155
'/search': '/',
@@ -68,13 +92,19 @@ export default defineConfig({
6892
label: 'GitHub',
6993
icon: 'github',
7094
href: 'https://github.com/reactchartjs/react-chartjs-2'
95+
},
96+
{
97+
label: 'Questions in GitHub Discussions',
98+
icon: 'comment-alt',
99+
href: 'https://github.com/reactchartjs/react-chartjs-2/discussions/categories/q-a'
71100
}
72101
],
73102
editLink: {
74103
baseUrl: 'https://github.com/reactchartjs/react-chartjs-2/edit/master/website/'
75104
},
76105
components: {
77-
Sidebar: './src/components/Sidebar.astro'
106+
PageSidebar: './src/components/PageSidebar.astro',
107+
Footer: './src/components/Footer.astro'
78108
},
79109
plugins: [
80110
llmsTxt({

‎website/rehype.js‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
const SITE = 'https://react-chartjs-2.js.org'
2+
3+
/**
4+
* Calls the callback for every link of the tree, including the links inside JSX of MDX files.
5+
* @param {import('hast').Parent} node - The node to walk.
6+
* @param {(link: import('hast').Element) => void} callback - What to do with a link.
7+
*/
8+
function visitLinks(node, callback) {
9+
for (const child of node.children ?? []) {
10+
if (child.type === 'element' && child.tagName === 'a') {
11+
callback(child)
12+
}
13+
14+
if ('children' in child) {
15+
visitLinks(child, callback)
16+
}
17+
}
18+
}
19+
120
/**
221
* Appends the trailing slash to internal links of pages, so they lead straight to the page
322
* instead of a redirect: `/docs/migration-to-v4#tree-shaking` becomes `/docs/migration-to-v4/#tree-shaking`.
@@ -19,25 +38,27 @@ function addTrailingSlash(href) {
1938
return `${pathname}/${rest}`
2039
}
2140

22-
/**
23-
* @param {import('hast').Parent} node - The node to walk.
24-
*/
25-
function visitLinks(node) {
26-
for (const child of node.children ?? []) {
27-
if (child.type === 'element') {
28-
if (child.tagName === 'a') {
29-
const href = addTrailingSlash(child.properties?.href)
30-
31-
if (href) {
32-
child.properties.href = href
33-
}
34-
}
41+
export function rehypeTrailingSlash() {
42+
return tree => visitLinks(tree, (link) => {
43+
const href = addTrailingSlash(link.properties?.href)
3544

36-
visitLinks(child)
45+
if (href) {
46+
link.properties.href = href
3747
}
38-
}
48+
})
3949
}
4050

41-
export function rehypeTrailingSlash() {
42-
return visitLinks
51+
/**
52+
* Opens links to other sites in a new tab, as the Docusaurus website did.
53+
* @returns {(tree: import('hast').Root) => void} The transformer of the page.
54+
*/
55+
export function rehypeExternalLinks() {
56+
return tree => visitLinks(tree, (link) => {
57+
const { href } = link.properties ?? {}
58+
59+
if (typeof href === 'string' && /^https?:\/\//.test(href) && !href.startsWith(SITE)) {
60+
link.properties.target = '_blank'
61+
link.properties.rel = ['noopener', 'noreferrer']
62+
}
63+
})
4364
}

‎website/src/components/Bwndw.astro‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
interface Props {
3+
/** The id of the block, unique on the page. */
4+
id: string
5+
/** Whether the block is for wide screens, which show the right sidebar, or for the others. */
6+
wide: boolean
7+
class?: string
8+
}
9+
10+
const { id, wide, class: className } = Astro.props
11+
---
12+
13+
<div class:list={['rc2-bwndw', className]} data-wide={wide ? '' : undefined}>
14+
<div
15+
id={id}
16+
class='flat horizontal'
17+
data-ea-publisher='react-chartjs-2jsorg'
18+
data-ea-type='image'
19+
></div>
20+
</div>
21+
<script is:inline>
22+
// A block that does not fit the screen goes before the script that fills the blocks loads
23+
(() => {
24+
const block = document.currentScript.previousElementSibling
25+
26+
if (matchMedia('(min-width: 72rem)').matches !== block.hasAttribute('data-wide')) {
27+
block.remove()
28+
}
29+
})()
30+
</script>
31+
32+
<script>
33+
// The blocks follow the color theme of the website
34+
function syncTheme() {
35+
const dark = document.documentElement.dataset['theme'] === 'dark'
36+
37+
document.querySelectorAll('.rc2-bwndw > [data-ea-publisher]').forEach((block) => {
38+
block.classList.toggle('dark', dark)
39+
})
40+
}
41+
42+
syncTheme()
43+
new MutationObserver(syncTheme).observe(document.documentElement, {
44+
attributes: true,
45+
attributeFilter: ['data-theme']
46+
})
47+
</script>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
import Default from '@astrojs/starlight/components/Footer.astro'
3+
import Bwndw from './Bwndw.astro'
4+
5+
// The pages with the right sidebar, where the other block is
6+
const { toc } = Astro.locals.starlightRoute
7+
---
8+
9+
{toc && (
10+
<>
11+
<Bwndw id='bwndw-main' wide={false} class='rc2-bwndw-main lg:sl-hidden' />
12+
{/* After both blocks, so the one that does not fit the screen is gone when it runs */}
13+
<script is:inline async src='https://media.ethicalads.io/media/client/ethicalads.min.js'></script>
14+
</>
15+
)}
16+
<Default />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
import Default from '@astrojs/starlight/components/PageSidebar.astro'
3+
import Bwndw from './Bwndw.astro'
4+
5+
const [title, ...headings] = Astro.locals.starlightRoute.toc?.items ?? []
6+
// A table of contents with the page title alone has nothing to navigate
7+
const hasContents = !(title?.children.length === 0 && headings.length === 0)
8+
---
9+
10+
{hasContents ? <Default /> : <div class='rc2-no-toc' hidden></div>}
11+
<Bwndw id='bwndw' wide={true} class='rc2-bwndw-aside sl-hidden lg:sl-block' />

‎website/src/components/PropsTable.astro‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ if (!doc) {
4646
function formatType(type: string) {
4747
return type
4848
.replace(/ \| undefined$/, '')
49+
.replace(/ChartJSOrUndefined<([^>]+)>/, 'Chart<$1>')
4950
.replace(/^_DeepPartialObject<CoreChartOptions<([^>]+)>[\s\S]*$/, 'ChartOptions<$1>')
5051
}
5152
@@ -78,7 +79,7 @@ const props = Object.values(doc.props).map(({
7879
{prop.see && (
7980
<>
8081
{' '}
81-
<a href={prop.see}>See more</a>
82+
<a href={prop.see} target='_blank' rel='noopener noreferrer'>See more</a>
8283
</>
8384
)}
8485
</p>

‎website/src/components/Sidebar.astro‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎website/src/content/docs/components/bubble.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "Bubble"
33
description: "Description of Bubble component from react-chartjs-2."
4+
next: false
45
---
56

67
import PropsTable from '../../../components/PropsTable.astro'

‎website/src/content/docs/components/chart.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "Chart"
33
description: "Description of Chart component from react-chartjs-2."
4+
prev: Components
45
---
56

67
import PropsTable from '../../../components/PropsTable.astro'

‎website/src/content/docs/components/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "Components"
33
description: "List of react-chartjs-2 components."
4+
prev: false
45
---
56

67
Every component renders a `<canvas>` and creates a Chart.js chart on it. Typed components set the chart type for you; `Chart` takes it as the `type` prop.

0 commit comments

Comments
 (0)