-
Notifications
You must be signed in to change notification settings - Fork 8
fix(code-block): normalize indentation of template literal snippets #217
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
base: main
Are you sure you want to change the base?
Changes from all commits
62692d6
73fa7a8
e76c01b
be10fa3
d04aeae
3e2deb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import type { ReactNode, RefObject } from "react"; | |
|
|
||
| import { Classes } from "@/constants/selectors"; | ||
| import { cn } from "@/utils/cn"; | ||
| import { dedent } from "@/utils/dedent"; | ||
| import { getNodeText } from "@/utils/get-node-text"; | ||
| import type { CodeBlockTheme, CodeStyling } from "@/utils/shiki/code-styling"; | ||
|
|
||
|
|
@@ -98,7 +99,12 @@ const CodeBlock = function CodeBlock(params: CodeBlockProps) { | |
| copyButtonProps, | ||
| } = params; | ||
|
|
||
| const codeString = getNodeText(children); | ||
| let codeString = getNodeText(children); | ||
|
|
||
| if (typeof codeString === "string") { | ||
| codeString = dedent(codeString); | ||
| } | ||
|
|
||
| const hasGrayBackgroundContainer = !!filename || !!icon; | ||
|
|
||
| return ( | ||
|
|
@@ -152,7 +158,9 @@ const CodeBlock = function CodeBlock(params: CodeBlockProps) { | |
| hideAskAiButton={hideAskAiButton} | ||
| isSmallText={isSmallText} | ||
| {...params} | ||
| /> | ||
| > | ||
| {codeString} | ||
| </BaseCodeBlock> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overriding BaseCodeBlock children breaks pre-highlighted Shiki renderingMedium Severity Passing Additional Locations (1) |
||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| const INDENT_REGEX = /^[ ]*/; | ||
|
|
||
| export function dedent(code: string): string { | ||
| const lines = code.split("\n"); | ||
|
|
||
| while (lines.length && lines[0].trim() === "") { | ||
| lines.shift(); | ||
| } | ||
|
|
||
| while (lines.length && lines.at(-1)?.trim() === "") { | ||
| lines.pop(); | ||
| } | ||
|
|
||
| if (lines.length === 0) { | ||
| return ""; | ||
| } | ||
|
|
||
| const indents = lines | ||
| .filter((line) => line.trim().length > 0) | ||
| .map((line) => line.match(INDENT_REGEX)?.[0].length ?? 0); | ||
|
|
||
| const minIndent = Math.min(...indents); | ||
|
|
||
| return lines.map((line) => line.slice(minIndent)).join("\n"); | ||
| } |


Uh oh!
There was an error while loading. Please reload this page.