openpanel/packages/cli/src/utils/swizzle/renderCodeMarkdown.ts
Stefan Pejcic 09f9f9502d packages
2024-11-07 19:03:37 +01:00

47 lines
1.0 KiB
TypeScript

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;
};