-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3180 from quantified-uncertainty/style-updates-ap…
…ril-2024 New Model+Variable Card Layouts
- Loading branch information
Showing
21 changed files
with
496 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { FC, HTMLAttributes, useEffect, useState } from "react"; | ||
import { | ||
type BundledLanguage, | ||
bundledLanguages, | ||
getHighlighter, | ||
type Highlighter, | ||
type LanguageRegistration, | ||
} from "shikiji"; | ||
|
||
// Import assertion here would be nice, but storybook doesn't support "assert" syntax yet, only "with" syntax, | ||
// and our import sorter auto-replaces with the newer "assert" syntax. | ||
// This will be fixed in storybook eventually, https://github.com/storybookjs/storybook/issues/23599 | ||
import squiggleGrammar from "@quri/squiggle-textmate-grammar/dist/squiggle.tmLanguage.json"; | ||
|
||
type SupportedLanguage = BundledLanguage | "squiggle"; | ||
|
||
let _shiki: Highlighter; // cached singleton | ||
type Theme = "vitesse-light" | "github-light"; | ||
|
||
async function codeToHtml(params: { | ||
code: string; | ||
language: SupportedLanguage; | ||
theme?: Theme; | ||
}) { | ||
const _theme = params.theme || "vitesse-light"; | ||
if (!_shiki) { | ||
_shiki = await getHighlighter({ | ||
themes: ["vitesse-light", "github-light"], | ||
langs: [ | ||
{ | ||
name: "squiggle", | ||
...squiggleGrammar, | ||
} as unknown as LanguageRegistration, // shikiji requires repository.$self and repository.$base that we don't have | ||
], | ||
}); | ||
} | ||
if ( | ||
params.language !== "squiggle" && | ||
!_shiki.getLoadedLanguages().includes(params.language) | ||
) { | ||
await _shiki.loadLanguage(params.language); | ||
await _shiki.loadLanguage(params.language); // somehow repeating it twice fixes the loading issue | ||
} | ||
|
||
return _shiki.codeToHtml(params.code, { | ||
theme: _theme, // TODO - write a custom theme that would match Codemirror styles | ||
lang: params.language, | ||
}); | ||
} | ||
|
||
function isSupportedLanguage(language: string): language is BundledLanguage { | ||
return language === "squiggle" || language in bundledLanguages; | ||
} | ||
|
||
export const CodeSyntaxHighlighter: FC< | ||
{ children: string; language: string; theme?: Theme } & Omit< | ||
HTMLAttributes<HTMLElement>, | ||
"children" | ||
> | ||
> = ({ children, language, theme, ...rest }) => { | ||
const [html, setHtml] = useState(children); | ||
|
||
// Syntax-highlighted blocks will start unstyled, that's fine. | ||
useEffect(() => { | ||
(async () => { | ||
if (isSupportedLanguage(language)) { | ||
setHtml(await codeToHtml({ code: children, language, theme })); | ||
} | ||
})(); | ||
}); | ||
|
||
return ( | ||
<pre | ||
className="*:!bg-inherit" // shiki themes add background color, so we have to override it | ||
dangerouslySetInnerHTML={{ __html: html }} | ||
{...rest} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/components/src/stories/CodeSyntaxHighlighter.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { CodeSyntaxHighlighter } from "../lib/CodeSyntaxHighlighter.js"; | ||
|
||
/** | ||
* The number shower is a simple component to display a number. | ||
* It uses the symbols "K", "M", "B", and "T", to represent thousands, millions, billions, and trillions. Outside of that range, it uses scientific notation. | ||
*/ | ||
const meta = { | ||
component: CodeSyntaxHighlighter, | ||
} satisfies Meta<typeof CodeSyntaxHighlighter>; | ||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Squiggle: Story = { | ||
name: "Squiggle Code", | ||
|
||
args: { | ||
language: "squiggle", | ||
children: `/* This is a comment */ | ||
const foo = "bar"; | ||
normal(5, 1) | ||
normal({ p5: 4, p95: 10 }) | ||
normal({ p10: 5, p95: 9 }) | ||
normal({ p25: 5, p75: 9 }) | ||
normal({ mean: 5, stdev: 2 }) | ||
normal(5 to 10, normal(3, 2)) | ||
normal({ mean: uniform(5, 9), stdev: 3 }) | ||
`, | ||
}, | ||
}; | ||
|
||
export const JS: Story = { | ||
name: "Javascript", | ||
|
||
args: { | ||
language: "javascript", | ||
children: `const meta = { | ||
component: CodeSyntaxHighlighter, | ||
} satisfies Meta<typeof CodeSyntaxHighlighter>; | ||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
`, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.