Skip to content
Merged
36 changes: 33 additions & 3 deletions website/astro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { defineConfig } from 'astro/config'
import { unified } from '@astrojs/markdown-remark'
import starlight from '@astrojs/starlight'
import llmsTxt from 'starlight-llms-txt'
import { rehypeTrailingSlash } from './rehype.js'
import {
rehypeExternalLinks,
rehypeTrailingSlash
} from './rehype.js'

const isProduction = process.env.NODE_ENV === 'production'
const chartTags = [
Expand All @@ -23,9 +26,30 @@ export default defineConfig({
trailingSlash: 'always',
markdown: {
processor: unified({
rehypePlugins: [rehypeTrailingSlash]
// Docusaurus kept straight quotes and dots, and error messages are quoted as they are
smartypants: false,
rehypePlugins: [rehypeTrailingSlash, rehypeExternalLinks]
})
},
vite: {
plugins: [
{
name: 'sandbox-index',
// Unlike GitHub Pages, the dev server does not answer a directory of `public` with its index.html
configureServer(server) {
server.middlewares.use((req, _res, next) => {
const [path, query] = (req.url ?? '').split('?')

if (path.startsWith('/sandboxes/') && path.endsWith('/')) {
req.url = `${path}index.html${query === undefined ? '' : `?${query}`}`
}

next()
})
}
}
]
},
// Pages of the Docusaurus website that have no counterpart
redirects: {
'/search': '/',
Expand Down Expand Up @@ -68,13 +92,19 @@ export default defineConfig({
label: 'GitHub',
icon: 'github',
href: 'https://github.com/reactchartjs/react-chartjs-2'
},
{
label: 'Questions in GitHub Discussions',
icon: 'comment-alt',
href: 'https://github.com/reactchartjs/react-chartjs-2/discussions/categories/q-a'
}
],
editLink: {
baseUrl: 'https://github.com/reactchartjs/react-chartjs-2/edit/master/website/'
},
components: {
Sidebar: './src/components/Sidebar.astro'
PageSidebar: './src/components/PageSidebar.astro',
Footer: './src/components/Footer.astro'
},
plugins: [
llmsTxt({
Expand Down
55 changes: 38 additions & 17 deletions website/rehype.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
const SITE = 'https://react-chartjs-2.js.org'

/**
* Calls the callback for every link of the tree, including the links inside JSX of MDX files.
* @param {import('hast').Parent} node - The node to walk.
* @param {(link: import('hast').Element) => void} callback - What to do with a link.
*/
function visitLinks(node, callback) {
for (const child of node.children ?? []) {
if (child.type === 'element' && child.tagName === 'a') {
callback(child)
}

if ('children' in child) {
visitLinks(child, callback)
}
}
}

/**
* Appends the trailing slash to internal links of pages, so they lead straight to the page
* instead of a redirect: `/docs/migration-to-v4#tree-shaking` becomes `/docs/migration-to-v4/#tree-shaking`.
Expand All @@ -19,25 +38,27 @@ function addTrailingSlash(href) {
return `${pathname}/${rest}`
}

/**
* @param {import('hast').Parent} node - The node to walk.
*/
function visitLinks(node) {
for (const child of node.children ?? []) {
if (child.type === 'element') {
if (child.tagName === 'a') {
const href = addTrailingSlash(child.properties?.href)

if (href) {
child.properties.href = href
}
}
export function rehypeTrailingSlash() {
return tree => visitLinks(tree, (link) => {
const href = addTrailingSlash(link.properties?.href)

visitLinks(child)
if (href) {
link.properties.href = href
}
}
})
}

export function rehypeTrailingSlash() {
return visitLinks
/**
* Opens links to other sites in a new tab, as the Docusaurus website did.
* @returns {(tree: import('hast').Root) => void} The transformer of the page.
*/
export function rehypeExternalLinks() {
return tree => visitLinks(tree, (link) => {
const { href } = link.properties ?? {}

if (typeof href === 'string' && /^https?:\/\//.test(href) && !href.startsWith(SITE)) {
link.properties.target = '_blank'
link.properties.rel = ['noopener', 'noreferrer']
}
})
}
47 changes: 47 additions & 0 deletions website/src/components/Bwndw.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
interface Props {
/** The id of the block, unique on the page. */
id: string
/** Whether the block is for wide screens, which show the right sidebar, or for the others. */
wide: boolean
class?: string
}
const { id, wide, class: className } = Astro.props
---

<div class:list={['rc2-bwndw', className]} data-wide={wide ? '' : undefined}>
<div
id={id}
class='flat horizontal'
data-ea-publisher='react-chartjs-2jsorg'
data-ea-type='image'
></div>
</div>
<script is:inline>
// A block that does not fit the screen goes before the script that fills the blocks loads
(() => {
const block = document.currentScript.previousElementSibling

if (matchMedia('(min-width: 72rem)').matches !== block.hasAttribute('data-wide')) {
block.remove()
}
})()
</script>

<script>
// The blocks follow the color theme of the website
function syncTheme() {
const dark = document.documentElement.dataset['theme'] === 'dark'

document.querySelectorAll('.rc2-bwndw > [data-ea-publisher]').forEach((block) => {
block.classList.toggle('dark', dark)
})
}

syncTheme()
new MutationObserver(syncTheme).observe(document.documentElement, {
attributes: true,
attributeFilter: ['data-theme']
})
</script>
16 changes: 16 additions & 0 deletions website/src/components/Footer.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
import Default from '@astrojs/starlight/components/Footer.astro'
import Bwndw from './Bwndw.astro'
// The pages with the right sidebar, where the other block is
const { toc } = Astro.locals.starlightRoute
---

{toc && (
<>
<Bwndw id='bwndw-main' wide={false} class='rc2-bwndw-main lg:sl-hidden' />
{/* After both blocks, so the one that does not fit the screen is gone when it runs */}
<script is:inline async src='https://media.ethicalads.io/media/client/ethicalads.min.js'></script>
</>
)}
<Default />
11 changes: 11 additions & 0 deletions website/src/components/PageSidebar.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Default from '@astrojs/starlight/components/PageSidebar.astro'
import Bwndw from './Bwndw.astro'
const [title, ...headings] = Astro.locals.starlightRoute.toc?.items ?? []
// A table of contents with the page title alone has nothing to navigate
const hasContents = !(title?.children.length === 0 && headings.length === 0)
---

{hasContents ? <Default /> : <div class='rc2-no-toc' hidden></div>}
<Bwndw id='bwndw' wide={true} class='rc2-bwndw-aside sl-hidden lg:sl-block' />
3 changes: 2 additions & 1 deletion website/src/components/PropsTable.astro
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if (!doc) {
function formatType(type: string) {
return type
.replace(/ \| undefined$/, '')
.replace(/ChartJSOrUndefined<([^>]+)>/, 'Chart<$1>')
.replace(/^_DeepPartialObject<CoreChartOptions<([^>]+)>[\s\S]*$/, 'ChartOptions<$1>')
}
Expand Down Expand Up @@ -78,7 +79,7 @@ const props = Object.values(doc.props).map(({
{prop.see && (
<>
{' '}
<a href={prop.see}>See more</a>
<a href={prop.see} target='_blank' rel='noopener noreferrer'>See more</a>
</>
)}
</p>
Expand Down
27 changes: 0 additions & 27 deletions website/src/components/Sidebar.astro

This file was deleted.

1 change: 1 addition & 0 deletions website/src/content/docs/components/bubble.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: "Bubble"
description: "Description of Bubble component from react-chartjs-2."
next: false
---

import PropsTable from '../../../components/PropsTable.astro'
Expand Down
1 change: 1 addition & 0 deletions website/src/content/docs/components/chart.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: "Chart"
description: "Description of Chart component from react-chartjs-2."
prev: Components
---

import PropsTable from '../../../components/PropsTable.astro'
Expand Down
1 change: 1 addition & 0 deletions website/src/content/docs/components/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: "Components"
description: "List of react-chartjs-2 components."
prev: false
---

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.
Expand Down
5 changes: 3 additions & 2 deletions website/src/content/docs/docs/chartjs-v2.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: "Using with Chart.js v2"
description: "Using react-chartjs-2 with Chart.js v2"
next: false
---

import { Tabs, TabItem } from '@astrojs/starlight/components'
Expand Down Expand Up @@ -35,7 +36,7 @@ Also, please consider upgrading your app to [Chart.js v3](#upgrading-to-chartjs-

## Upgrading to Chart.js v3

First, upgrade packages. You'll need to install Chart.js v3 and the latest version of this library:
First, upgrade packages. You'll need to install Chart.js v3 and a compatible version of this library:

<Tabs syncKey='pkg'>
<TabItem label='pnpm' icon='pnpm'>
Expand All @@ -61,4 +62,4 @@ First, upgrade packages. You'll need to install Chart.js v3 and the latest versi
</TabItem>
</Tabs>

Then, please follow the [this guide](/docs/migration-to-v4).
Then, please follow [this guide](/docs/migration-to-v4).
4 changes: 2 additions & 2 deletions website/src/content/docs/docs/chartjs-v3.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Also, please consider upgrading your app to [Chart.js v4](#upgrading-to-chartjs-

## Upgrading to Chart.js v4

First, upgrade packages. You'll need to install Chart.js v3 and the latest version of this library:
First, upgrade packages. You'll need to install Chart.js v4 and a compatible version of this library:

<Tabs syncKey='pkg'>
<TabItem label='pnpm' icon='pnpm'>
Expand All @@ -61,4 +61,4 @@ First, upgrade packages. You'll need to install Chart.js v3 and the latest versi
</TabItem>
</Tabs>

Then, please follow the [this guide](/docs/migration-to-v5).
Then, please follow [this guide](/docs/migration-to-v5).
2 changes: 1 addition & 1 deletion website/src/content/docs/examples/area-chart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ description: "Example of area chart in react-chartjs-2."

import Example from '../../../components/Example.astro'

Example of area chart in react-chartjs-2.
Example of area chart with the [`<Line>`](/components/line/) component.

<Example sandbox='line/area' />
2 changes: 1 addition & 1 deletion website/src/content/docs/examples/bubble-chart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ description: "Example of bubble chart in react-chartjs-2."

import Example from '../../../components/Example.astro'

Example of bubble chart in react-chartjs-2.
Example of bubble chart with the [`<Bubble>`](/components/bubble/) component.

<Example sandbox='bubble/default' />
2 changes: 1 addition & 1 deletion website/src/content/docs/examples/chart-events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ description: "Example of events in react-chartjs-2."

import Example from '../../../components/Example.astro'

Example of events in react-chartjs-2.
Example of events with the [`<Chart>`](/components/chart/) component.

<Example sandbox='chart/events' />
6 changes: 4 additions & 2 deletions website/src/content/docs/examples/chart-ref.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
---
title: "Chart Ref"
title: "Get Chart Ref"
sidebar:
label: "Chart Ref"
description: "Example of getting ref in react-chartjs-2."
---

import Example from '../../../components/Example.astro'

Example of getting ref in react-chartjs-2.
Example of getting ref with the [`<Chart>`](/components/chart/) component.

<Example sandbox='chart/ref' />
2 changes: 1 addition & 1 deletion website/src/content/docs/examples/doughnut-chart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ description: "Example of doughnut chart in react-chartjs-2."

import Example from '../../../components/Example.astro'

Example of doughnut chart in react-chartjs-2.
Example of doughnut chart with the [`<Doughnut>`](/components/doughnut/) component.

<Example sandbox='doughnut/default' />
3 changes: 2 additions & 1 deletion website/src/content/docs/examples/gradient-chart.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
title: "Gradient Chart"
description: "Example of creating gradient chart in react-chartjs-2."
next: false
---

import Example from '../../../components/Example.astro'

Example of creating gradient chart in react-chartjs-2.
Example of creating gradient chart with the [`<Chart>`](/components/chart/) component.

<Example sandbox='chart/canvas' />
2 changes: 1 addition & 1 deletion website/src/content/docs/examples/grouped-bar-chart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ description: "Example of grouped bar chart in react-chartjs-2."

import Example from '../../../components/Example.astro'

Example of grouped bar chart in react-chartjs-2.
Example of grouped bar chart with the [`<Bar>`](/components/bar/) component.

<Example sandbox='bar/grouped' />
2 changes: 1 addition & 1 deletion website/src/content/docs/examples/horizontal-bar-chart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ description: "Example of horizontal bar chart in react-chartjs-2."

import Example from '../../../components/Example.astro'

Example of horizontal bar chart in react-chartjs-2.
Example of horizontal bar chart with the [`<Bar>`](/components/bar/) component.

<Example sandbox='bar/horizontal' />
Loading
Loading