Skip to content
Open
Show file tree
Hide file tree
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
112 changes: 65 additions & 47 deletions src/Highlighter/LangSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,78 @@
'use client';

import { Select, type SelectProps } from 'antd';
import { memo, useMemo } from 'react';
import { bundledLanguagesInfo } from 'shiki';
import { memo, useEffect, useState } from 'react';

import { Flexbox } from '@/Flex';
import MaterialFileTypeIcon from '@/MaterialFileTypeIcon';
import Text from '@/Text';
import { stopPropagation } from '@/utils/dom';

export const LangSelect = memo<Omit<SelectProps, 'options'>>(({ ...rest }) => {
const options = useMemo(
() => [
{
aliases: ['text', 'txt'],
label: (
<Flexbox horizontal align={'center'} gap={4}>
<MaterialFileTypeIcon
fallbackUnknownType={false}
filename={`*.txt`}
size={18}
type={'file'}
variant={'raw'}
/>
<Text ellipsis fontSize={13}>
Plaintext
</Text>
</Flexbox>
),
value: 'plaintext',
},
...bundledLanguagesInfo.map((item) => ({
aliases: item.aliases,
label: (
<Flexbox horizontal align={'center'} gap={4}>
<MaterialFileTypeIcon
fallbackUnknownType={false}
filename={`*.${item?.aliases?.[0] || item.id}`}
size={18}
type={'file'}
variant={'raw'}
/>
<Text ellipsis fontSize={13}>
{item.name}
</Text>
</Flexbox>
),
title: (item.aliases || [item.id])
.filter(Boolean)
.map((item) => `*.${item}`)
.join(','),
value: item.id,
})),
],
[],
);
const [options, setOptions] = useState<SelectProps['options']>([
{
label: (
<Flexbox horizontal align={'center'} gap={4}>
<MaterialFileTypeIcon
fallbackUnknownType={false}
filename={`*.txt`}
size={18}
type={'file'}
variant={'raw'}
/>
<Text ellipsis fontSize={13}>
Plaintext
</Text>
</Flexbox>
),
value: 'plaintext',
},
]);

useEffect(() => {
import('shiki/langs').then(({ bundledLanguagesInfo }) => {
setOptions([
{
label: (
<Flexbox horizontal align={'center'} gap={4}>
<MaterialFileTypeIcon
fallbackUnknownType={false}
filename={`*.txt`}
size={18}
type={'file'}
variant={'raw'}
/>
<Text ellipsis fontSize={13}>
Plaintext
</Text>
</Flexbox>
),
value: 'plaintext',
},
...bundledLanguagesInfo.map((item) => ({
label: (
<Flexbox horizontal align={'center'} gap={4}>
<MaterialFileTypeIcon
fallbackUnknownType={false}
filename={`*.${item?.aliases?.[0] || item.id}`}
size={18}
type={'file'}
variant={'raw'}
/>
<Text ellipsis fontSize={13}>
{item.name}
</Text>
</Flexbox>
),
title: (item.aliases || [item.id])
.filter(Boolean)
.map((item) => `*.${item}`)
.join(','),
value: item.id,
})),
]);
});
}, []);

return (
<Select
Expand Down
19 changes: 18 additions & 1 deletion src/Highlighter/SyntaxHighlighter/StreamRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';

import { getTokenStyleObject } from '@shikijs/core';
import { cx } from 'antd-style';
import type { CSSProperties } from 'react';
import { memo } from 'react';
Expand All @@ -18,6 +17,24 @@ interface StreamRendererProps {
theme?: BuiltinTheme;
}

// FontStyle bitmask values from @shikijs/vscode-textmate
const FontStyle = { Bold: 2, Italic: 1, Strikethrough: 8, Underline: 4 } as const;

const getTokenStyleObject = (token: ThemedToken): Record<string, string> => {
const styles: Record<string, string> = {};
if (token.color) styles.color = token.color;
if (token.bgColor) styles['background-color'] = token.bgColor;
if (token.fontStyle) {
if (token.fontStyle & FontStyle.Italic) styles['font-style'] = 'italic';
if (token.fontStyle & FontStyle.Bold) styles['font-weight'] = 'bold';
const decorations: string[] = [];
if (token.fontStyle & FontStyle.Underline) decorations.push('underline');
if (token.fontStyle & FontStyle.Strikethrough) decorations.push('line-through');
if (decorations.length) styles['text-decoration'] = decorations.join(' ');
}
return styles;
};

const normalizeStyleKeys = (style: Record<string, string | number>): CSSProperties => {
const normalized: CSSProperties = {};
Object.entries(style).forEach(([key, value]) => {
Expand Down
40 changes: 6 additions & 34 deletions src/Highlighter/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { bundledLanguagesInfo, bundledThemesInfo } from 'shiki';

interface HighlighterThemeItem {
displayName: string;
id: string;
Expand All @@ -10,47 +8,21 @@ export const highlighterThemes: HighlighterThemeItem[] = [
displayName: 'Lobe Theme',
id: 'lobe-theme',
},
...bundledThemesInfo.map((item) => ({
displayName: item.displayName,
id: item.id,
})),
];

export const FALLBACK_LANG = 'plaintext';

export const getCodeLanguageByInput = (input: string): string => {
if (!input) {
return 'plaintext';
}
const inputLang = input.toLocaleLowerCase();

const matchLang = bundledLanguagesInfo.find(
(lang) => lang.id === inputLang || lang.aliases?.includes(inputLang),
);
return matchLang?.id || 'plaintext';
if (!input) return 'plaintext';
return input.toLocaleLowerCase();
Comment on lines 15 to +17
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore alias-to-id language normalization

This now returns the raw lowercased input instead of resolving aliases to canonical Shiki language IDs, so common fence aliases (for example js, ts, md, sh) can bypass normalization and fail to match the IDs the highlighter is loaded with, causing degraded or missing syntax highlighting paths that previously worked. The new helper file still contains the old resolution logic, which suggests this path was unintentionally simplified.

Useful? React with 👍 / 👎.

};

export const getCodeLanguageFilename = (input: string): string => {
if (!input) {
return 'Plaintext';
}
const inputLang = input.toLocaleLowerCase();

const matchLang = bundledLanguagesInfo.find(
(lang) => lang.id === inputLang || lang.aliases?.includes(inputLang),
);
const type = matchLang?.aliases?.[0] || matchLang?.id || 'txt';
return `*.${type}`;
if (!input) return 'Plaintext';
return `*.${input.toLocaleLowerCase()}`;
};

export const getCodeLanguageDisplayName = (input: string): string => {
if (!input) {
return 'Plaintext';
}
const inputLang = input.toLocaleLowerCase();

const matchLang = bundledLanguagesInfo.find(
(lang) => lang.id === inputLang || lang.aliases?.includes(inputLang),
);
return matchLang?.name || 'Plaintext';
if (!input) return 'Plaintext';
return input.charAt(0).toUpperCase() + input.slice(1);
};
43 changes: 43 additions & 0 deletions src/Highlighter/langUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { bundledLanguagesInfo } from 'shiki/langs';
import { bundledThemesInfo } from 'shiki/themes';

interface HighlighterThemeItem {
displayName: string;
id: string;
}

export const getAllHighlighterThemes = (): HighlighterThemeItem[] => [
{ displayName: 'Lobe Theme', id: 'lobe-theme' },
...bundledThemesInfo.map((item) => ({
displayName: item.displayName,
id: item.id,
})),
];

export const resolveLanguageByInput = (input: string): string => {
if (!input) return 'plaintext';
const inputLang = input.toLocaleLowerCase();
const matchLang = bundledLanguagesInfo.find(
(lang) => lang.id === inputLang || lang.aliases?.includes(inputLang),
);
return matchLang?.id || 'plaintext';
};

export const resolveLanguageFilename = (input: string): string => {
if (!input) return 'Plaintext';
const inputLang = input.toLocaleLowerCase();
const matchLang = bundledLanguagesInfo.find(
(lang) => lang.id === inputLang || lang.aliases?.includes(inputLang),
);
const type = matchLang?.aliases?.[0] || matchLang?.id || 'txt';
return `*.${type}`;
};

export const resolveLanguageDisplayName = (input: string): string => {
if (!input) return 'Plaintext';
const inputLang = input.toLocaleLowerCase();
const matchLang = bundledLanguagesInfo.find(
(lang) => lang.id === inputLang || lang.aliases?.includes(inputLang),
);
return matchLang?.name || 'Plaintext';
};
58 changes: 58 additions & 0 deletions src/HighlighterMinimal/SyntaxHighlighterMinimal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use client';

import { cx } from 'antd-style';
import { type CSSProperties, memo } from 'react';

import { variants } from '@/Highlighter/SyntaxHighlighter/style';

import { useMinimalHighlight } from './highlighter';

const escapeHtml = (str: string) =>
str
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&#039;');

export interface SyntaxHighlighterMinimalProps {
children: string;
className?: string;
language: string;
style?: CSSProperties;
variant?: 'filled' | 'outlined' | 'borderless';
}

const SyntaxHighlighterMinimal = memo<SyntaxHighlighterMinimalProps>(
({ children, className, language, style, variant = 'borderless' }) => {
const safeChildren = children ?? '';
const data = useMinimalHighlight(safeChildren, language);
const hasData = typeof data === 'string' && data.length > 0;

const shikiClassName = cx(
variants({ animated: false, shiki: true, showBackground: false, variant }),
className,
);
const fallbackClassName = cx(
variants({ animated: false, shiki: false, showBackground: false, variant }),
className,
);

return (
<div
className={hasData ? shikiClassName : fallbackClassName}
dir="ltr"
style={style}
dangerouslySetInnerHTML={{
__html: data || `<pre><code>${escapeHtml(safeChildren)}</code></pre>`,
}}
/>
);
},
(prevProps, nextProps) =>
prevProps.children === nextProps.children && prevProps.language === nextProps.language,
);

SyntaxHighlighterMinimal.displayName = 'SyntaxHighlighterMinimal';

export default SyntaxHighlighterMinimal;
Loading
Loading