code.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { codeToHtml } from "shiki";
  2. import { CopyButton } from "./copy-button";
  3. const vercelDarkTheme = {
  4. name: "vercel-dark",
  5. type: "dark" as const,
  6. colors: {
  7. "editor.background": "transparent",
  8. "editor.foreground": "#EDEDED",
  9. },
  10. settings: [
  11. {
  12. scope: ["comment", "punctuation.definition.comment"],
  13. settings: { foreground: "#666666" },
  14. },
  15. {
  16. scope: ["string", "string.quoted", "string.template"],
  17. settings: { foreground: "#50E3C2" },
  18. },
  19. {
  20. scope: ["constant.numeric", "constant.language.boolean", "constant.language.null"],
  21. settings: { foreground: "#50E3C2" },
  22. },
  23. {
  24. scope: ["keyword", "storage.type", "storage.modifier"],
  25. settings: { foreground: "#FF0080" },
  26. },
  27. {
  28. scope: ["keyword.operator", "keyword.control"],
  29. settings: { foreground: "#FF0080" },
  30. },
  31. {
  32. scope: ["entity.name.function", "support.function", "meta.function-call"],
  33. settings: { foreground: "#7928CA" },
  34. },
  35. {
  36. scope: ["variable", "variable.other", "variable.parameter"],
  37. settings: { foreground: "#EDEDED" },
  38. },
  39. {
  40. scope: ["entity.name.tag", "support.class.component", "entity.name.type"],
  41. settings: { foreground: "#FF0080" },
  42. },
  43. {
  44. scope: ["punctuation", "meta.brace", "meta.bracket"],
  45. settings: { foreground: "#888888" },
  46. },
  47. {
  48. scope: ["support.type.property-name", "entity.name.tag.json", "meta.object-literal.key"],
  49. settings: { foreground: "#EDEDED" },
  50. },
  51. {
  52. scope: ["entity.other.attribute-name"],
  53. settings: { foreground: "#50E3C2" },
  54. },
  55. {
  56. scope: ["support.type.primitive", "entity.name.type.primitive"],
  57. settings: { foreground: "#50E3C2" },
  58. },
  59. ],
  60. };
  61. const vercelLightTheme = {
  62. name: "vercel-light",
  63. type: "light" as const,
  64. colors: {
  65. "editor.background": "transparent",
  66. "editor.foreground": "#171717",
  67. },
  68. settings: [
  69. {
  70. scope: ["comment", "punctuation.definition.comment"],
  71. settings: { foreground: "#6b7280" },
  72. },
  73. {
  74. scope: ["string", "string.quoted", "string.template"],
  75. settings: { foreground: "#067a6e" },
  76. },
  77. {
  78. scope: ["constant.numeric", "constant.language.boolean", "constant.language.null"],
  79. settings: { foreground: "#067a6e" },
  80. },
  81. {
  82. scope: ["keyword", "storage.type", "storage.modifier"],
  83. settings: { foreground: "#d6409f" },
  84. },
  85. {
  86. scope: ["keyword.operator", "keyword.control"],
  87. settings: { foreground: "#d6409f" },
  88. },
  89. {
  90. scope: ["entity.name.function", "support.function", "meta.function-call"],
  91. settings: { foreground: "#6e56cf" },
  92. },
  93. {
  94. scope: ["variable", "variable.other", "variable.parameter"],
  95. settings: { foreground: "#171717" },
  96. },
  97. {
  98. scope: ["entity.name.tag", "support.class.component", "entity.name.type"],
  99. settings: { foreground: "#d6409f" },
  100. },
  101. {
  102. scope: ["punctuation", "meta.brace", "meta.bracket"],
  103. settings: { foreground: "#6b7280" },
  104. },
  105. {
  106. scope: ["support.type.property-name", "entity.name.tag.json", "meta.object-literal.key"],
  107. settings: { foreground: "#171717" },
  108. },
  109. {
  110. scope: ["entity.other.attribute-name"],
  111. settings: { foreground: "#067a6e" },
  112. },
  113. {
  114. scope: ["support.type.primitive", "entity.name.type.primitive"],
  115. settings: { foreground: "#067a6e" },
  116. },
  117. ],
  118. };
  119. interface CodeProps {
  120. children: string;
  121. lang?: "json" | "tsx" | "typescript" | "bash" | "javascript";
  122. }
  123. export async function Code({ children, lang = "typescript" }: CodeProps) {
  124. const html = await codeToHtml(children.trim(), {
  125. lang,
  126. themes: {
  127. light: vercelLightTheme,
  128. dark: vercelDarkTheme,
  129. },
  130. defaultColor: false,
  131. });
  132. return (
  133. <div className="group relative my-6 rounded-lg border border-border bg-neutral-100 dark:bg-[#0a0a0a] text-sm font-mono overflow-hidden">
  134. <div className="absolute top-3 right-3 z-10">
  135. <CopyButton
  136. text={children.trim()}
  137. className="opacity-0 group-hover:opacity-100 text-neutral-500 dark:text-neutral-400 bg-neutral-100 dark:bg-[#0a0a0a]"
  138. />
  139. </div>
  140. <div className="overflow-x-auto [&_pre]:bg-transparent! [&_pre]:m-0! [&_pre]:p-4! [&_code]:bg-transparent! [&_.shiki]:bg-transparent!" dangerouslySetInnerHTML={{ __html: html }} />
  141. </div>
  142. );
  143. }