Skip to content

Commit 511ec27

Browse files
superhighfiveso-az
authored andcommitted
Bypass inline code (#233)
1 parent f8fc980 commit 511ec27

File tree

8 files changed

+95
-2
lines changed

8 files changed

+95
-2
lines changed

.changeset/perfect-onions-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"rehype-pretty-code": minor
3+
---
4+
5+
Adds an option to bypass inline code blocks

docs/src/content/docs/index.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ interface Options {
220220
grid?: boolean;
221221
theme?: Theme | Record<string, Theme>;
222222
keepBackground?: boolean;
223+
bypassInlineCode?: boolean;
223224
defaultLang?: string | { block?: string; inline?: string };
224225
tokensMap?: Record<string, string>;
225226
transformers?: ShikiTransformer[];
@@ -277,6 +278,16 @@ const options = {
277278
};
278279
```
279280

281+
### `bypassInlineCode`
282+
283+
Skip inline code highlighting:
284+
285+
```js
286+
const options = {
287+
bypassInlineCode: true,
288+
};
289+
```
290+
280291
### `defaultLang`
281292

282293
When no code language is specified, inline code or code blocks will not be

examples/next/src/app/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Inline ANSI: `> Local: http://localhost:3000/{:ansi}`
9494
- [grid](#grid)
9595
- [theme](#theme)
9696
- [keepBackground](#keepbackground)
97+
- [bypassInlineCode](#bypassInlineCode)
9798
- [defaultLang](#defaultlang)
9899
- [transformers](#transformers)
99100
- [Meta Strings](#meta-strings)

packages/core/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export function rehypePrettyCode(
177177
grid = true,
178178
theme = 'github-dark-dimmed',
179179
keepBackground = true,
180+
bypassInlineCode = false,
180181
defaultLang = '',
181182
tokensMap = {},
182183
filterMetaString = (v) => v,
@@ -235,7 +236,7 @@ export function rehypePrettyCode(
235236

236237
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
237238
visit(tree, 'element', (element, _, parent) => {
238-
if (isInlineCode(element, parent)) {
239+
if (isInlineCode(element, parent, bypassInlineCode)) {
239240
const textElement = element.children[0];
240241
if (!isText(textElement)) return;
241242
const value = textElement.value;
@@ -280,7 +281,7 @@ export function rehypePrettyCode(
280281

281282
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
282283
visit(tree, 'element', (element, _, parent) => {
283-
if (isInlineCode(element, parent)) {
284+
if (isInlineCode(element, parent, bypassInlineCode)) {
284285
const textElement = element.children[0];
285286
if (!isText(textElement)) return;
286287
const value = textElement.value;

packages/core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface Options {
2424
grid?: boolean;
2525
theme?: Theme | Record<string, Theme>;
2626
keepBackground?: boolean;
27+
bypassInlineCode?: boolean;
2728
defaultLang?: string | { block?: string; inline?: string };
2829
tokensMap?: Record<string, string>;
2930
transformers?: Array<ShikiTransformer>;

packages/core/src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export function isText(value: ElementContent | null): value is Text {
2020
export function isInlineCode(
2121
element: Element,
2222
parent: Element | Root | undefined,
23+
bypass = false,
2324
): element is Element {
25+
if (bypass) {
26+
return false;
27+
}
2428
return (
2529
(element.tagName === 'code' &&
2630
isElement(parent) &&

packages/core/test/fixtures/bypassInlineCode.md

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/test/results/bypassInlineCode.html

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)