Skip to content

Conversation

dav-is
Copy link
Member

@dav-is dav-is commented Jun 30, 2025

Adds an isomorphic (server and client loading/parsing) and build-optimizable (via a loader) code highlighting component.

Notable Features

  • allows deferring highlighting to various points: init (no defer), stream, hydration, and idle
  • Supports variants and transforms. Variants are manual, transforms happen at build time. Supports TS to JS transforms automatically without the need for a .js file in the repo.
  • Supports MDX or JSX (TSX) usage
  • Supports webpack and turbopack.
  • Supports both the app and pages router in Next.js (no hard dependency on server components like other code highlighters)

Adds 947 unit tests, covering 70% of statements, 85% of branches, runs in 3 seconds on my machine

Note

The docs are added in a separate PR: #431

Install instructions with variants (remembers preference in local storage)

image

A demo of a codeblock

Screenshot From 2025-07-21 18-10-10

A demo within a demo

Screenshot From 2025-07-21 18-10-29

A demo of a demo with multiple variants

image

Linking to Demo Title Consistent with File Linking

Note

See the address bar

Screenshot From 2025-07-29 00-39-09 Screenshot From 2025-07-29 00-39-20

Read the docs for this component

Precompute Loader Docs

Read the rest of the added docs

Notes

We use ESM only packages for some things (https://www.npmjs.com/package/unified, https://www.npmjs.com/package/jsondiffpatch, etc) so CJS publishing is disabled (at least for now)

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jul 1, 2025
@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged. and removed PR: out-of-date The pull request has merge conflicts and can't be merged. labels Jul 2, 2025
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jul 11, 2025
@dav-is
Copy link
Member Author

dav-is commented Sep 4, 2025

So let's add import 'server-only' to those?

I added import 'server-only' to the docs: f444119 (#431)

When I added server-only to files in this PR, they failed to work within webpack loaders even though the webpack loader isn't bundled into the client. So I had to revert that change.

@dav-is dav-is requested a review from Janpot September 5, 2025 03:14
…dd support for sourceParser and sourceTransformers in loader checks
@oliviertassinari oliviertassinari temporarily deployed to davis/add-code-highlighter-loader - mui-tools-public PR #379 September 5, 2025 03:23 — with Render Destroyed
@Janpot
Copy link
Member

Janpot commented Sep 5, 2025

When I added server-only to files in this PR, they failed to work within webpack loaders even though the webpack loader isn't bundled into the client. So I had to revert that change.

Is that a bug in Next.js? If so, can you open a ticket with them?

// Set hash in URL and state
const setHash = React.useCallback(
(value: string | null, replace: boolean = true) => {
if (typeof window === 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

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

I don't believe this will ever be called in a server context

Suggested change
if (typeof window === 'undefined') {

/**
* Hook for managing URL hash state with SSR support
*/
export function useUrlHashState(options: UseUrlHashStateOptions = {}): UseUrlHashStateResult {
Copy link
Member

Choose a reason for hiding this comment

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

Let's keep these publicly exported hooks simple and to the point. The options are unnecessary, and same for anything else than hash and setHash. What's the point of hasUserInteraction? It's doing nothing beyond wrapping React.useState

filename = 'file.ts',
prettierConfig: PrettierOptions | boolean = true,
): Promise<string> {
// Babel collapses newlines all over the place, which messes with the formatting of almost any
Copy link
Member

Choose a reason for hiding this comment

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

Why care about preserving formatting at all cost if we're going to run prettier over it anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is largely the code from the original https://github.com/ember-cli/babel-remove-types/blob/main/src/index.ts

I think the idea was that it would remove extra newlines?

Copy link
Member

Choose a reason for hiding this comment

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

Ok, advocate of the devil here, why are we forking this code then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mainly JSX support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation. scope: docs-infra Changes related to the docs-infra product.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[docs-infra] Unified demo package
7 participants