Skip to content

Commit ddca723

Browse files
Export everything + expose options for client to use
1 parent 85da49b commit ddca723

File tree

5 files changed

+34
-64
lines changed

5 files changed

+34
-64
lines changed

packages/mdx/src/client/rsc.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ import remarkGfm from 'remark-gfm';
55
import remarkMath from 'remark-math';
66
import remarkSmartypants from 'remark-smartypants';
77

8-
import { rehypeSyntaxHighlighting } from '../plugins/index.js';
8+
import { rehypeSyntaxHighlighting, RehypeSyntaxHighlightingOptions } from '../plugins/index.js';
99

1010
export async function MDXRemote({
1111
source,
1212
mdxOptions,
1313
scope,
1414
components,
1515
parseFrontmatter,
16+
syntaxHighlightingOptions,
1617
}: {
1718
source: string;
1819
mdxOptions?: SerializeOptions['mdxOptions'];
1920
scope?: SerializeOptions['scope'];
2021
components?: MDXComponents;
2122
parseFrontmatter?: SerializeOptions['parseFrontmatter'];
23+
syntaxHighlightingOptions?: RehypeSyntaxHighlightingOptions;
2224
}) {
2325
return (
2426
// @ts-expect-error Server Component
@@ -36,7 +38,7 @@ export async function MDXRemote({
3638
],
3739
rehypePlugins: [
3840
rehypeKatex,
39-
[rehypeSyntaxHighlighting, { ignoreMissing: true }],
41+
[rehypeSyntaxHighlighting, syntaxHighlightingOptions],
4042
...(mdxOptions?.rehypePlugins || []),
4143
],
4244
format: mdxOptions?.format || 'mdx',

packages/mdx/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './client/index.js';
22
export * from './server/index.js';
33
export * from './types/index.js';
4+
export * from './plugins/index.js';

packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ import {
1010
import type { Plugin } from 'unified';
1111
import { visit } from 'unist-util-visit';
1212

13-
import {
14-
BASE_LANGUAGES,
15-
DEFAULT_LANG_ALIASES,
16-
UNIQUE_LANGS,
17-
type ShikiLang,
18-
} from './shiki-constants.js';
13+
import { DEFAULT_LANG_ALIASES, UNIQUE_LANGS, type ShikiLang } from './shiki-constants.js';
1914

2015
const shikiColorReplacements: Partial<Record<BundledTheme, string | Record<string, string>>> = {
2116
'dark-plus': {
@@ -31,8 +26,6 @@ const shikiColorReplacements: Partial<Record<BundledTheme, string | Record<strin
3126
};
3227

3328
export type RehypeSyntaxHighlightingOptions = {
34-
ignoreMissing?: boolean;
35-
alias?: Record<string, ShikiLang>;
3629
theme?: BuiltinTheme;
3730
themes?: Record<'light' | 'dark', BuiltinTheme>;
3831
codeStyling?: 'dark' | 'system';
@@ -61,10 +54,17 @@ async function getHighlighter(): Promise<Highlighter> {
6154
export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?], Root, Root> = (
6255
options = {}
6356
) => {
64-
const languageAliases = { ...options.alias, ...DEFAULT_LANG_ALIASES };
65-
6657
return async (tree) => {
6758
const highlighter = await getHighlighter();
59+
if (options.theme) {
60+
await highlighter.loadTheme(options.theme);
61+
}
62+
if (options.themes) {
63+
await Promise.all([
64+
highlighter.loadTheme(options.themes.dark),
65+
highlighter.loadTheme(options.themes.light),
66+
]);
67+
}
6868

6969
visit(tree, 'element', (node, index, parent) => {
7070
const child = node.children[0];
@@ -90,9 +90,11 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
9090
}
9191

9292
let lang =
93-
getLanguage(node, languageAliases) ?? getLanguage(child, languageAliases) ?? 'text';
93+
getLanguage(node, DEFAULT_LANG_ALIASES) ??
94+
getLanguage(child, DEFAULT_LANG_ALIASES) ??
95+
'text';
9496

95-
if (!BASE_LANGUAGES.includes(lang)) {
97+
if (!UNIQUE_LANGS.includes(lang)) {
9698
highlighter.loadLanguage(lang as BundledLanguage);
9799
}
98100

@@ -104,8 +106,11 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
104106
const hast = highlighter.codeToHast(code, {
105107
lang: lang ?? 'text',
106108
themes: {
107-
light: 'github-light-default',
108-
dark: 'dark-plus',
109+
light:
110+
options.themes?.light ??
111+
options.theme ??
112+
(options.codeStyling === 'dark' ? 'dark-plus' : 'github-light-default'),
113+
dark: options.themes?.dark ?? options.theme ?? 'dark-plus',
109114
},
110115
colorReplacements: shikiColorReplacements,
111116
tabindex: false,
@@ -154,7 +159,7 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
154159
}
155160
parent.children.splice(index, 1, codeElement);
156161
} catch (err) {
157-
if (options.ignoreMissing && /Unknown language/.test((err as Error).message)) {
162+
if (err instanceof Error && /Unknown language/.test(err.message)) {
158163
return;
159164
}
160165
throw err;
@@ -208,3 +213,5 @@ function getLinesToHighlight(node: Element, maxLines: number): number[] {
208213

209214
return Array.from(lineNumbers).sort((a, b) => a - b);
210215
}
216+
217+
export { UNIQUE_LANGS, ShikiLang, DEFAULT_LANG_ALIASES };

packages/mdx/src/plugins/rehype/shiki-constants.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -318,37 +318,3 @@ export const DEFAULT_LANG_ALIASES: Record<string, ShikiLang> = {
318318
};
319319

320320
export const UNIQUE_LANGS = Array.from(new Set(Object.values(DEFAULT_LANG_ALIASES)));
321-
322-
export const BASE_LANGUAGES = [
323-
'bash',
324-
'blade',
325-
'c',
326-
'css',
327-
'c#',
328-
'c++',
329-
'dart',
330-
'diff',
331-
'go',
332-
'html',
333-
'java',
334-
'javascript',
335-
'jsx',
336-
'json',
337-
'kotlin',
338-
'log',
339-
'lua',
340-
'markdown',
341-
'mdx',
342-
'php',
343-
'powershell',
344-
'python',
345-
'ruby',
346-
'rust',
347-
'solidity',
348-
'swift',
349-
'terraform',
350-
'text',
351-
'typescript',
352-
'tsx',
353-
'yaml',
354-
];

packages/mdx/src/server/index.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import remarkGfm from 'remark-gfm';
55
import remarkMath from 'remark-math';
66
import remarkSmartypants from 'remark-smartypants';
77

8-
import { rehypeSyntaxHighlighting } from '../plugins/index.js';
8+
import { rehypeSyntaxHighlighting, RehypeSyntaxHighlightingOptions } from '../plugins/index.js';
99
import type { SerializeOptions } from '../types/index.js';
1010

1111
export const serialize = async ({
1212
source,
1313
mdxOptions,
1414
scope,
1515
parseFrontmatter = true,
16+
syntaxHighlightingOptions,
1617
}: {
1718
source: string;
1819
mdxOptions?: SerializeOptions['mdxOptions'];
1920
scope?: SerializeOptions['scope'];
2021
parseFrontmatter?: SerializeOptions['parseFrontmatter'];
22+
syntaxHighlightingOptions?: RehypeSyntaxHighlightingOptions;
2123
}) => {
2224
try {
2325
return await baseSerialize({
@@ -33,12 +35,7 @@ export const serialize = async ({
3335
],
3436
rehypePlugins: [
3537
rehypeKatex,
36-
[
37-
rehypeSyntaxHighlighting,
38-
{
39-
ignoreMissing: true,
40-
},
41-
],
38+
[rehypeSyntaxHighlighting, syntaxHighlightingOptions],
4239
...(mdxOptions?.rehypePlugins || []),
4340
],
4441
format: mdxOptions?.format || 'mdx',
@@ -59,11 +56,13 @@ export const rscSerialize = async ({
5956
mdxOptions,
6057
scope,
6158
parseFrontmatter = true,
59+
syntaxHighlightingOptions,
6260
}: {
6361
source: string;
6462
mdxOptions?: SerializeOptions['mdxOptions'];
6563
scope?: SerializeOptions['scope'];
6664
parseFrontmatter?: SerializeOptions['parseFrontmatter'];
65+
syntaxHighlightingOptions?: RehypeSyntaxHighlightingOptions;
6766
}) => {
6867
try {
6968
return await evaluate({
@@ -79,12 +78,7 @@ export const rscSerialize = async ({
7978
],
8079
rehypePlugins: [
8180
rehypeKatex,
82-
[
83-
rehypeSyntaxHighlighting,
84-
{
85-
ignoreMissing: true,
86-
},
87-
],
81+
[rehypeSyntaxHighlighting, syntaxHighlightingOptions],
8882
...(mdxOptions?.rehypePlugins || []),
8983
],
9084
format: mdxOptions?.format || 'mdx',

0 commit comments

Comments
 (0)