code.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { codeToHtml } from "shiki";
  2. const vercelTheme = {
  3. name: "vercel",
  4. type: "dark" as const,
  5. colors: {
  6. "editor.background": "transparent",
  7. "editor.foreground": "#EDEDED",
  8. },
  9. settings: [
  10. {
  11. scope: ["comment", "punctuation.definition.comment"],
  12. settings: { foreground: "#666666" },
  13. },
  14. {
  15. scope: ["string", "string.quoted", "string.template"],
  16. settings: { foreground: "#50E3C2" },
  17. },
  18. {
  19. scope: ["constant.numeric", "constant.language.boolean", "constant.language.null"],
  20. settings: { foreground: "#50E3C2" },
  21. },
  22. {
  23. scope: ["keyword", "storage.type", "storage.modifier"],
  24. settings: { foreground: "#FF0080" },
  25. },
  26. {
  27. scope: ["keyword.operator", "keyword.control"],
  28. settings: { foreground: "#FF0080" },
  29. },
  30. {
  31. scope: ["entity.name.function", "support.function", "meta.function-call"],
  32. settings: { foreground: "#7928CA" },
  33. },
  34. {
  35. scope: ["variable", "variable.other", "variable.parameter"],
  36. settings: { foreground: "#EDEDED" },
  37. },
  38. {
  39. scope: ["entity.name.tag", "support.class.component", "entity.name.type"],
  40. settings: { foreground: "#FF0080" },
  41. },
  42. {
  43. scope: ["punctuation", "meta.brace", "meta.bracket"],
  44. settings: { foreground: "#888888" },
  45. },
  46. {
  47. scope: ["support.type.property-name", "entity.name.tag.json", "meta.object-literal.key"],
  48. settings: { foreground: "#EDEDED" },
  49. },
  50. {
  51. scope: ["entity.other.attribute-name"],
  52. settings: { foreground: "#50E3C2" },
  53. },
  54. {
  55. scope: ["support.type.primitive", "entity.name.type.primitive"],
  56. settings: { foreground: "#50E3C2" },
  57. },
  58. ],
  59. };
  60. interface CodeProps {
  61. children: string;
  62. lang?: "json" | "tsx" | "typescript" | "bash" | "javascript";
  63. }
  64. export async function Code({ children, lang = "typescript" }: CodeProps) {
  65. const html = await codeToHtml(children.trim(), {
  66. lang,
  67. theme: vercelTheme,
  68. });
  69. return (
  70. <div
  71. className="my-6 rounded-lg border border-border bg-card p-4 overflow-x-auto text-sm [&_pre]:!bg-transparent [&_pre]:!p-0 [&_pre]:!m-0 [&_code]:!bg-transparent font-mono"
  72. dangerouslySetInnerHTML={{ __html: html }}
  73. />
  74. );
  75. }