Skip to content

Commit a54a897

Browse files
fix(details): trim newlines at the beginning and end of code during rendering
1 parent 40a9c43 commit a54a897

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/routes/[pid=pid]/[org]/[repo]/[id=number]/PageRenderer.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@
5959
import Step from "$lib/components/Step.svelte";
6060
import Steps from "$lib/components/Steps.svelte";
6161
import BottomCollapsible from "./BottomCollapsible.svelte";
62-
import { transformerDiffMarking, transformerLanguageDetection } from "./syntax-highlighting";
62+
import {
63+
transformerDiffMarking,
64+
transformerLanguageDetection,
65+
transformerTrimCode
66+
} from "./syntax-highlighting";
6367
6468
const shikiPlugin: Plugin = {
6569
rehypePlugin: [
6670
rehypeShikiFromHighlighter,
6771
highlighter,
6872
{
6973
themes: { light: "github-light-default", dark: "github-dark-default" },
70-
transformers: [transformerLanguageDetection, transformerDiffMarking]
74+
transformers: [transformerTrimCode, transformerLanguageDetection, transformerDiffMarking]
7175
} satisfies Parameters<typeof rehypeShikiFromHighlighter>[1]
7276
]
7377
};

src/routes/[pid=pid]/[org]/[repo]/[id=number]/syntax-highlighting.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ export function detectLanguage(code: string): (SpecialLanguage | (string & {}))
2525
if (/[a-z-]+: \S+/.test(code)) return "css";
2626
}
2727

28+
/**
29+
* A transformer that trims unnecessary whitespace at the beginning and end of the string.
30+
*/
31+
export const transformerTrimCode: ShikiTransformer = {
32+
preprocess(code) {
33+
return code.replace(/(^\r?\n|\r?\n$)/g, "");
34+
}
35+
};
36+
2837
/**
2938
* A Shiki transformer used for language detection and setting the appropriate language metadata
3039
* in code blocks. Useful for handling code snippets with "diff" language and converting them

0 commit comments

Comments
 (0)