This commit is contained in:
Stefan Pejcic
2024-11-07 19:03:37 +01:00
parent c6df945ed5
commit 09f9f9502d
2472 changed files with 620417 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
import chalk from "chalk";
import cardinal from "cardinal";
import boxen from "boxen";
const getCodeData = (content: string): { title?: string; code: string } => {
const titleRegexp = /^(?:\/\/\s?title:\s?)(.*?)\n/g;
const [commentLine, titleMatch] = titleRegexp.exec(content) ?? [];
if (titleMatch) {
const title = titleMatch.trim();
const code = content.replace(commentLine || "", "");
return { title, code };
}
return { code: content };
};
export const renderCodeMarkdown = (content: string) => {
const { title, code: rawCode } = getCodeData(content);
let highlighted = "";
// run cardinal on codeContent
try {
const code = cardinal.highlight(rawCode, {
jsx: true,
});
highlighted = code;
} catch (err) {
highlighted = rawCode;
}
// wrap to boxen
const boxed = boxen(highlighted, {
padding: 1,
margin: 0,
borderStyle: "round",
borderColor: "gray",
titleAlignment: "left",
title: title ? chalk.bold(title) : undefined,
});
return boxed;
};