Skip to content

Commit 85fa331

Browse files
committed
Make CodeBlock lines apart
1 parent 1e29f03 commit 85fa331

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Diff for: src/components/content/CodeBlock/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface CodeBlockProps {
1515
/**
1616
* The code
1717
*/
18-
children: string;
18+
children: string | string[];
1919

2020
/**
2121
* To style the `pre` tag
@@ -67,11 +67,11 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ lang, children, className, marked
6767
${!isNextMarked && isMarked ? "round-bot" : ""}
6868
`.trim();
6969

70-
70+
7171
_renderedLines[i] = (
7272
<tr key={i} suppressHydrationWarning className={`line ${markingStyle}`}>
7373
{showLines && <td suppressHydrationWarning className="nr"><span suppressHydrationWarning>{lineNumber}</span></td>}
74-
<td suppressHydrationWarning className="cd" dangerouslySetInnerHTML={{ __html: line.html }} />
74+
<td suppressHydrationWarning className="cd whitespace-break-spaces" dangerouslySetInnerHTML={{ __html: line.html }} />
7575
</tr>
7676
);
7777

@@ -91,7 +91,7 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ lang, children, className, marked
9191
<>
9292
<link key="codeblock:stylesheed" rel="stylesheet" href="/styles/codeblock.min.css" />
9393
<pre className={computedClassName} data-ssr-rendered={allowSSR} data-is-codeblock="true">
94-
<code className="language-bash">
94+
<code className="">
9595
<table className="w-full">
9696
<tbody>{renderedLines}</tbody>
9797
</table>

Diff for: src/hooks/useCodeHighlight.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type SupportedLanguages = "bash" | "js" | "html" | "css";
66

77
interface Options {
88
lang: SupportedLanguages;
9-
code: string;
9+
code: string | string[];
1010
markedLines?: (number | [number, number])[];
1111
allowSSR?: boolean;
1212
}
@@ -53,10 +53,11 @@ const useCodeHightlight = ({ lang, code, markedLines, allowSSR }: Options) => {
5353
}, [lang]);
5454

5555
const codeLines = useMemo<CodeLine[]>(() => {
56-
let codeToRender = code;
56+
let codeToRender = Array.isArray(code) ? code.join("\n") : code;
5757

5858
// Normalize whitespace
5959
const startingWhitespace = codeToRender.match(/^\s+/)?.[0]?.replace("\n", "");
60+
6061
if (startingWhitespace) {
6162
const normalizeWhitespaceRegex = new RegExp(`(?<=\n)[^\S\n]{${startingWhitespace.length}}(?!\n)`, "g");
6263
codeToRender = codeToRender.replace(normalizeWhitespaceRegex, "");

0 commit comments

Comments
 (0)