Skip to content

Commit

Permalink
Bypass inline code (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
superhighfives authored and o-az committed Aug 28, 2024
1 parent f8fc980 commit 511ec27
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-onions-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rehype-pretty-code": minor
---

Adds an option to bypass inline code blocks
11 changes: 11 additions & 0 deletions docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ interface Options {
grid?: boolean;
theme?: Theme | Record<string, Theme>;
keepBackground?: boolean;
bypassInlineCode?: boolean;
defaultLang?: string | { block?: string; inline?: string };
tokensMap?: Record<string, string>;
transformers?: ShikiTransformer[];
Expand Down Expand Up @@ -277,6 +278,16 @@ const options = {
};
```

### `bypassInlineCode`

Skip inline code highlighting:

```js
const options = {
bypassInlineCode: true,
};
```

### `defaultLang`

When no code language is specified, inline code or code blocks will not be
Expand Down
1 change: 1 addition & 0 deletions examples/next/src/app/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Inline ANSI: `> Local: http://localhost:3000/{:ansi}`
- [grid](#grid)
- [theme](#theme)
- [keepBackground](#keepbackground)
- [bypassInlineCode](#bypassInlineCode)
- [defaultLang](#defaultlang)
- [transformers](#transformers)
- [Meta Strings](#meta-strings)
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export function rehypePrettyCode(
grid = true,
theme = 'github-dark-dimmed',
keepBackground = true,
bypassInlineCode = false,
defaultLang = '',
tokensMap = {},
filterMetaString = (v) => v,
Expand Down Expand Up @@ -235,7 +236,7 @@ export function rehypePrettyCode(

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

// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
visit(tree, 'element', (element, _, parent) => {
if (isInlineCode(element, parent)) {
if (isInlineCode(element, parent, bypassInlineCode)) {
const textElement = element.children[0];
if (!isText(textElement)) return;
const value = textElement.value;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Options {
grid?: boolean;
theme?: Theme | Record<string, Theme>;
keepBackground?: boolean;
bypassInlineCode?: boolean;
defaultLang?: string | { block?: string; inline?: string };
tokensMap?: Record<string, string>;
transformers?: Array<ShikiTransformer>;
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export function isText(value: ElementContent | null): value is Text {
export function isInlineCode(
element: Element,
parent: Element | Root | undefined,
bypass = false,
): element is Element {
if (bypass) {
return false;
}
return (
(element.tagName === 'code' &&
isElement(parent) &&
Expand Down
7 changes: 7 additions & 0 deletions packages/core/test/fixtures/bypassInlineCode.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions packages/core/test/results/bypassInlineCode.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 511ec27

Please sign in to comment.