Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coerce renderer for framework pages #269

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use client';

import { useEffect, useState, type FC } from 'react';
import { usePathname } from 'next/navigation';
import { type CodeSnippetsProps } from '@repo/utils';
import { CodeSnippetsWrapper } from '@repo/ui';
import { InfoIcon } from '@storybook/icons';
Expand Down Expand Up @@ -31,18 +32,44 @@ const Error = () => {
);
};

/**
* For the framework docs pages, we coerce the active renderer to the relevant one for that framework.
* We do this _without_ affecting the actual activeRenderer value.
*/
const getActiveRenderer = (activeRendererIn: string | null, pathname: string) => {
// eslint-disable-next-line prefer-named-capture-group -- TODO: Enable via TS config changes
const matches = /\/docs\/get-started\/frameworks\/(.*)/.exec(pathname);

if (!matches) return activeRendererIn;

const framework = matches[1];
const frameworkOrRendererPortion = framework.replace(/-(?:vite|webpack5)/, '') as keyof typeof map;

const map = {
'nextjs': 'react',
'react-native-web': 'react',
'sveltekit': 'svelte',
'vue3': 'vue',
};

return map[frameworkOrRendererPortion] ?? frameworkOrRendererPortion;
}

export const CodeSnippetsClient: FC<CodeSnippetsClientProps> = ({
content,
}) => {
const {
activeRenderer,
activeRenderer: activeRendererIn,
activeLanguage,
activePackageManager,
setLanguage,
setPackageManager,
} = useDocs();
const [activeTab, setActiveTab] = useState<Tab['id'] | null>(null);

const pathname = usePathname();
const activeRenderer = getActiveRenderer(activeRendererIn, pathname);

// Get filters - If preformatted text, we don't need filters
const filters = getFilters({ content: content ?? [], activeRenderer });

Expand Down