Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass inline code #233

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -172,6 +172,7 @@ export function rehypePrettyCode(
grid = true,
theme = 'github-dark-dimmed',
keepBackground = true,
bypassInlineCode = false,
defaultLang = '',
tokensMap = {},
filterMetaString = (v) => v,
Expand Down Expand Up @@ -230,7 +231,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 @@ -275,7 +276,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 @@ -22,6 +22,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.