Skip to content

Commit a35391b

Browse files
authored
Merge pull request #412 from shairez:pr-fix-mdx-examples
feat(docs): reduce duplicate code examples
2 parents 2048395 + ef85b0c commit a35391b

31 files changed

+688
-806
lines changed

apps/website/.eslintrc.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"extends": [
3+
"../../.eslintrc.json",
34
"eslint:recommended",
45
"plugin:@typescript-eslint/recommended",
5-
"plugin:qwik/recommended",
6-
"../../.eslintrc.json"
6+
"plugin:qwik/recommended"
77
],
88
"parser": "@typescript-eslint/parser",
99
"parserOptions": {
@@ -12,9 +12,7 @@
1212
"sourceType": "module",
1313
"ecmaFeatures": {
1414
"jsx": true
15-
},
16-
"parser": "@typescript-eslint/parser",
17-
"tsconfigRootDir": "__dirname"
15+
}
1816
},
1917
"plugins": ["@typescript-eslint"],
2018
"ignorePatterns": ["!**/*"],
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { createContextId } from '@builder.io/qwik';
22
import { AppState } from './app-state.type';
33

4-
export const ROOT_STORE_CONTEXT_ID = createContextId<AppState>('app-state-context-id');
4+
export const APP_STATE_CONTEXT_ID = createContextId<AppState>('app-state-context-id');

apps/website/src/_state/app-state.type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { NoSerialize } from '@builder.io/qwik';
2+
import type { Highlighter } from 'shiki';
3+
14
export interface AppState {
25
mode: 'light' | 'dark';
6+
highlighter?: NoSerialize<Highlighter>;
37
isSidebarOpened: boolean;
48
featureFlags?: {
59
showFluffy?: boolean;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useContext } from '@builder.io/qwik';
2+
import { APP_STATE_CONTEXT_ID } from './app-state-context-id';
3+
4+
export const useAppState = () => {
5+
const appState = useContext(APP_STATE_CONTEXT_ID);
6+
return appState;
7+
};

apps/website/src/_state/use-root-store.ts

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

apps/website/src/global.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
--color-ring: 263 84% 25%;
3636

3737
--border-radius: 0.375rem;
38+
39+
--shiki-color-text: theme('colors.white');
40+
--shiki-token-constant: theme('colors.emerald.300');
41+
--shiki-token-string: theme('colors.emerald.300');
42+
--shiki-token-comment: theme('colors.zinc.500');
43+
--shiki-token-keyword: theme('colors.sky.300');
44+
--shiki-token-parameter: theme('colors.pink.300');
45+
--shiki-token-function: theme('colors.violet.300');
46+
--shiki-token-string-expression: theme('colors.emerald.300');
47+
--shiki-token-punctuation: theme('colors.zinc.200');
3848
}
3949

4050
.dark {

apps/website/src/root.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
} from '@builder.io/qwik-city';
1313
import { RouterHead } from './routes/_components/router-head/router-head';
1414

15+
import { APP_STATE_CONTEXT_ID } from './_state/app-state-context-id';
1516
import { AppState } from './_state/app-state.type';
16-
import { ROOT_STORE_CONTEXT_ID } from './_state/root-store-context-id';
1717
import { THEME_STORAGE_KEY, useCSSTheme } from './_state/use-css-theme';
1818
import { OLD_APP_STATE_CONTEXT_ID } from './constants';
1919
import globalStyles from './global.css?inline';
@@ -28,22 +28,22 @@ export default component$(() => {
2828
*/
2929
useStyles$(globalStyles);
3030

31-
const rootStore = useStore<AppState>({
31+
const appState = useStore<AppState>({
3232
mode: 'light',
3333
isSidebarOpened: false,
3434
featureFlags: {
3535
showFluffy: import.meta.env.DEV,
3636
},
3737
});
3838

39-
useContextProvider(ROOT_STORE_CONTEXT_ID, rootStore);
39+
useContextProvider(APP_STATE_CONTEXT_ID, appState);
4040

41-
useVisibleTask$(() => {
41+
useVisibleTask$(async () => {
4242
const userStoredTheme = localStorage.getItem(THEME_STORAGE_KEY);
4343
if (userStoredTheme) {
44-
rootStore.mode = userStoredTheme === 'dark' ? 'dark' : 'light';
44+
appState.mode = userStoredTheme === 'dark' ? 'dark' : 'light';
4545
} else {
46-
rootStore.mode = window.matchMedia('(prefers-color-scheme: dark)').matches
46+
appState.mode = window.matchMedia('(prefers-color-scheme: dark)').matches
4747
? 'dark'
4848
: 'light';
4949
}
@@ -55,7 +55,7 @@ export default component$(() => {
5555
});
5656
useContextProvider(OLD_APP_STATE_CONTEXT_ID, state);
5757

58-
useCSSTheme(rootStore);
58+
useCSSTheme(appState);
5959

6060
return (
6161
<QwikCityProvider>
@@ -67,7 +67,7 @@ export default component$(() => {
6767
<body
6868
lang="en"
6969
class={{
70-
'overflow-y-hidden': rootStore.isSidebarOpened,
70+
'overflow-y-hidden': appState.isSidebarOpened,
7171
}}
7272
>
7373
<RouterOutlet />

apps/website/src/routes/_components/header/header.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { $, component$, useComputed$ } from '@builder.io/qwik';
22
// eslint-disable-next-line @nx/enforce-module-boundaries
33
import { version as headlessVersion } from '../../../../../../packages/kit-headless/package.json';
44
// eslint-disable-next-line @nx/enforce-module-boundaries
5+
import { useLocation } from '@builder.io/qwik-city';
56
import { KitName } from 'apps/website/src/_state/kit-name.type';
6-
import { useRootStore } from 'apps/website/src/_state/use-root-store';
7+
import { useAppState } from 'apps/website/src/_state/use-app-state';
78
import { version as fluffyVersion } from '../../../../../../packages/kit-fluffy/package.json';
89
import { useSelectedKit } from '../../docs/use-selected-kit';
910
import { CloseIcon } from '../icons/CloseIcon';
@@ -12,7 +13,6 @@ import { MenuIcon } from '../icons/MenuIcon';
1213
import { MoonIcon } from '../icons/MoonIcon';
1314
import { SunIcon } from '../icons/SunIcon';
1415
import { Logo } from '../icons/logo';
15-
import { useLocation } from '@builder.io/qwik-city';
1616

1717
export interface HeaderProps {
1818
showVersion?: boolean;
@@ -21,7 +21,7 @@ export interface HeaderProps {
2121

2222
export default component$(
2323
({ showVersion = false, showBottomBorder = false }: HeaderProps) => {
24-
const rootStore = useRootStore();
24+
const rootStore = useAppState();
2525
const selectedKitSig = useSelectedKit();
2626
const location = useLocation();
2727

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { getHighlighter, type Highlighter } from 'shiki';
2+
3+
let highlighter: Highlighter;
4+
export async function getOrCreateHighlighter() {
5+
if (highlighter) {
6+
return highlighter;
7+
}
8+
highlighter = await getHighlighter({ theme: 'css-variables' });
9+
return highlighter;
10+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { QwikIntrinsicElements, component$, useSignal, useTask$ } from '@builder.io/qwik';
2+
import { OmitSignalClass } from '@qwik-ui/type-utils';
3+
import { getOrCreateHighlighter } from './get-or-create-highlighter';
4+
5+
export type HighlightProps = OmitSignalClass<QwikIntrinsicElements['pre']> & {
6+
code: string;
7+
splitCommentStart?: string;
8+
splitCommentEnd?: string;
9+
};
10+
11+
export const Highlight = component$(
12+
({
13+
code,
14+
splitCommentStart = '{/* start */}',
15+
splitCommentEnd = '{/* end */}',
16+
...props
17+
}: HighlightProps) => {
18+
const codeSig = useSignal('');
19+
20+
useTask$(async function createHighlightedCode() {
21+
const highlighter = await getOrCreateHighlighter();
22+
let modifiedCode: string = code;
23+
24+
let partsOfCode = modifiedCode.split(splitCommentStart);
25+
if (partsOfCode.length > 1) {
26+
modifiedCode = partsOfCode[1];
27+
}
28+
29+
partsOfCode = modifiedCode.split(splitCommentEnd);
30+
if (partsOfCode.length > 1) {
31+
modifiedCode = partsOfCode[0];
32+
}
33+
34+
codeSig.value = highlighter.codeToHtml(modifiedCode, { lang: 'tsx' });
35+
});
36+
37+
return (
38+
<pre
39+
{...props}
40+
class={[
41+
'theme-atom-one-dark shadow-3xl tab-size relative h-full max-w-full overflow-hidden text-sm',
42+
props.class,
43+
]}
44+
>
45+
<code dangerouslySetInnerHTML={codeSig.value} />
46+
</pre>
47+
);
48+
},
49+
);

0 commit comments

Comments
 (0)