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

[v4] initialize transformerTwoslash only 1 time outside of loader #3697

Merged
merged 3 commits into from
Nov 12, 2024
Merged
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
7 changes: 7 additions & 0 deletions .changeset/flat-zoos-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'nextra-theme-blog': patch
'nextra-theme-docs': patch
'nextra': patch
---

initialize `transformerTwoslash` only 1 time outside of loader
4 changes: 2 additions & 2 deletions packages/nextra/src/server/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import remarkMath from 'remark-math'
import remarkReadingTime from 'remark-reading-time'
import remarkSmartypants from 'remark-smartypants'
import type { Pluggable, Plugin } from 'unified'
import type { LoaderOptions } from '../types.js'
import type { LoaderOptions, NextraConfig } from '../types.js'
import { CWD, MARKDOWN_URL_EXTENSION_RE } from './constants.js'
import {
recmaRewriteFunctionBody,
Expand Down Expand Up @@ -46,7 +46,7 @@ const cachedCompilerForFormat: Record<
Processor
> = Object.create(null)

type MdxOptions = LoaderOptions['mdxOptions'] &
type MdxOptions = NextraConfig['mdxOptions'] &
Pick<ProcessorOptions, 'jsx' | 'outputFormat' | 'providerImportSource'>

type CompileMdxOptions = Pick<
Expand Down
16 changes: 9 additions & 7 deletions packages/nextra/src/server/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const repository = await (async () => {
// repository.path() returns the `/path/to/repo/.git`, we need the parent directory of it
const GIT_ROOT = repository ? path.join(repository.path(), '..') : ''

const DEFAULT_TRANSFORMERS = transformerTwoslash({
renderer: twoslashRenderer(),
explicitTrigger: true
})

export async function loader(
this: LoaderContext<LoaderOptions>,
source: string
Expand Down Expand Up @@ -92,7 +97,7 @@ export async function loader(
const { pageMap, mdxPages } = convertToPageMap({
filePaths,
// Remove forward slash
basePath: contentDirBasePath!.slice(1),
basePath: contentDirBasePath.slice(1),
locale
})
const rawJs = await convertPageMapToJs({ pageMap, mdxPages })
Expand All @@ -119,13 +124,10 @@ export async function loader(
format: 'detect',
providerImportSource: 'next-mdx-import-source-file',
rehypePrettyCodeOptions: {
...mdxOptions?.rehypePrettyCodeOptions,
...mdxOptions.rehypePrettyCodeOptions,
transformers: [
transformerTwoslash({
renderer: twoslashRenderer(),
explicitTrigger: true
}),
...(mdxOptions?.rehypePrettyCodeOptions?.transformers || [])
DEFAULT_TRANSFORMERS,
...(mdxOptions.rehypePrettyCodeOptions.transformers || [])
]
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/nextra/src/server/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export const nextraConfigSchema = z.strictObject({
remarkPlugins: z.custom<ProcessorOptions['remarkPlugins']>(),
recmaPlugins: z.custom<ProcessorOptions['recmaPlugins']>(),
format: z.enum(['detect', 'mdx', 'md']).optional(),
rehypePrettyCodeOptions: z.custom<RehypePrettyCodeOptions>().optional()
rehypePrettyCodeOptions: z.custom<RehypePrettyCodeOptions>().default({})
})
.optional(),
.default({}),
whiteListTagsStyling: z.array(z.string()).optional(),
contentDirBasePath: z
.string()
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { FC, ReactElement, ReactNode } from 'react'
import type { z } from 'zod'
import type { mathJaxOptionsSchema, nextraConfigSchema } from './server/schemas'

export interface LoaderOptions extends NextraConfig {
export interface LoaderOptions extends z.infer<typeof nextraConfigSchema> {
isPageImport?: boolean
locales: string[]
}
Expand Down