mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
packages
This commit is contained in:
46
packages/cli/src/utils/swizzle/renderCodeMarkdown.ts
Normal file
46
packages/cli/src/utils/swizzle/renderCodeMarkdown.ts
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user