Skip to content

How to do inlined code highlighting #2376

Answered by novusnota
novusnota asked this question in Q&A
Discussion options

You must be logged in to vote

For the time being, I came up with an adhoc single-file rehype plugin that uses Shiki (like Expressive Code does) and highlights all inline code which uses following syntax: some code{:lang-name}. The {:lang-name} (for example, console.log(){:js}) is also seen in Nextra docs and Rehype Pretty Code (didn't take it because it would conflict with Expressive Code).

Snippet: inline-code-highlighting.js

Here's the snippet:

/**
 * @import {Root} from 'hast'
 */

import { visit } from 'unist-util-visit';
import { toString } from 'hast-util-to-string';
import {
  bundledLanguages,
  createHighlighter,
  createWasmOnigEngine,
} from 'shiki';
import fs from 'node:fs';

// Import custom grammars
const 

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@novusnota
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by novusnota
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants