Code Hike
Code Hike is an open-source library designed to enhance web content by transforming standard Markdown into rich, interactive experiences using React. Currently stable at version 1.1.0, the project demonstrates an active release cadence with frequent patch updates and a significant major release (v1.0.0) in August 2024. Its core functionality revolves around providing 'Fine-grained Markdown' and 'Headless codeblocks', which are key differentiators. Unlike traditional syntax highlighters, Code Hike parses the Abstract Syntax Tree (AST) of code blocks, enabling advanced features like line focusing, callouts, diffs, and interactive elements directly within code comments. This allows developers to build custom React components for rendering code and markdown, giving complete control over UI and behavior. It is built on the MDX ecosystem and integrates seamlessly with React-based frameworks like Next.js, making it ideal for code walkthroughs, tutorials, interactive blog posts, and comprehensive documentation sites.
Common errors
-
TypeError: ke.createContext is not a function OR createContext only works in Client Components. Add the "use client" directive at the top of the file to use it.
cause Attempting to use React Context API or other client-side React features within a server component, often when rendering MDX content in Next.js App Router without proper client component boundaries.fixAdd `'use client'` at the top of the React component file that directly or indirectly uses `createContext` or other client-only React hooks/APIs. -
Error: Code Hike couldn't resolve this annotation: !from [filepath]. [absolute_filepath] doesn't exist.
cause The `!from` directive used in Markdown to import external code cannot resolve the specified file path, usually because the MDX compiler function was called without setting the `file` path or context correctly.fixEnsure the MDX compilation environment correctly provides the `file.history` or equivalent context that allows Code Hike to resolve relative file paths, especially when integrating with custom MDX loaders or bundler configurations. -
ERROR at root Error for `steps`: Required { "expected": "array", "received": "undefined" }cause When using Code Hike's layout components (e.g., `Scrollycoding`, `Slideshow`) or the `parse` utility, the decorated Markdown content is not correctly structured or passed as the expected `steps` array prop.fixVerify that your Markdown content uses the correct decoration syntax (e.g., `!decorationName`) and that the parsing logic in your React component correctly extracts and passes the `steps` prop. Review Code Hike's documentation examples for content structuring.
Warnings
- breaking Version 1.0.0 introduced significant breaking changes with 'Fine-grained Markdown' and 'Headless codeblocks'. Migration from v0.x requires updating MDX configurations and custom code block components.
- gotcha The `highlight` function is asynchronous, making components that use it (e.g., custom code block components) React Server Components (RSCs). This might require specific framework configurations or handling for non-RSC environments.
- gotcha When using built-in themes like `github-from-css` or `material-from-css`, you must define the corresponding CSS variables for colors in your global stylesheet for proper light/dark mode support. Simply importing the theme won't apply the styles correctly.
- gotcha Astro is not officially supported by Code Hike because MDX files are compiled to Astro components instead of React components, which is incompatible with Code Hike's React-based architecture.
- gotcha Users have reported intermittent build issues and 'TurbopackInternalError' when using Code Hike with Next.js's Turbopack bundler, especially with non-serializable options in `next.config.mjs` or newer Next.js versions.
Install
-
npm install codehike -
yarn add codehike -
pnpm add codehike
Imports
- remarkCodeHike
const { remarkCodeHike } = require('codehike/mdx')import { remarkCodeHike, recmaCodeHike } from 'codehike/mdx' - { Pre, RawCode, highlight }
import { CodeHike } from 'codehike'import { Pre, RawCode, highlight } from 'codehike/code' - CSS Styles
import '@code-hike/mdx/dist/index.css'
import 'codehike/style.css'
Quickstart
import { remarkCodeHike, recmaCodeHike } from 'codehike/mdx'
import { Pre, RawCode, highlight } from 'codehike/code'
import 'codehike/style.css'
// next.config.mjs (or similar MDX configuration)
const chConfig = {
// Define a custom component to render code blocks
components: { code: 'MyCode' },
syntaxHighlighting: {
theme: 'github-dark'
}
}
const mdxOptions = {
remarkPlugins: [[remarkCodeHike, chConfig]],
recmaPlugins: [[recmaCodeHike, chConfig]],
jsx: true,
}
// app/components/MyCode.tsx (React Server Component)
// This component renders an annotated code block.
export async function MyCode({ codeblock }: { codeblock: RawCode }) {
const highlighted = await highlight(codeblock, "github-dark");
return <Pre code={highlighted} />;
}
// app/page.mdx (example MDX content)
// ```js
// console.log("Hello from Code Hike!")
// // ! focus
// ```