app-markdown-theme.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { SyntaxStyle, type ThemeTokenStyle } from "@mbears/opentui-core";
  2. import type { TuiThemeTokens } from "./theme/index.js";
  3. export function markdownSyntaxRules(theme: TuiThemeTokens): ThemeTokenStyle[] {
  4. return [
  5. token(["default"], { foreground: theme.markdownText }),
  6. token(["markup.heading", "markup.heading.1", "markup.heading.2", "markup.heading.3"], {
  7. bold: true,
  8. foreground: theme.markdownHeading,
  9. }),
  10. token(["markup.heading.4", "markup.heading.5", "markup.heading.6"], {
  11. bold: true,
  12. foreground: theme.markdownHeading,
  13. }),
  14. token(["markup.bold", "markup.strong"], {
  15. bold: true,
  16. foreground: theme.markdownStrong,
  17. }),
  18. token(["markup.italic"], { foreground: theme.markdownEmph, italic: true }),
  19. token(["markup.list"], { foreground: theme.markdownListItem }),
  20. token(["markup.quote"], { foreground: theme.markdownBlockQuote, italic: true }),
  21. token(["markup.raw", "markup.raw.block"], { foreground: theme.markdownCodeBlock }),
  22. token(["markup.raw.inline"], {
  23. background: theme.backgroundPanel,
  24. foreground: theme.markdownCode,
  25. }),
  26. token(["markup.link", "markup.link.url"], {
  27. foreground: theme.markdownLink,
  28. underline: true,
  29. }),
  30. token(["markup.link.label", "label"], {
  31. foreground: theme.markdownLinkText,
  32. underline: true,
  33. }),
  34. token(["conceal"], { foreground: theme.textMuted }),
  35. token(["comment"], { foreground: theme.syntaxComment, italic: true }),
  36. token(["string", "symbol"], { foreground: theme.syntaxString }),
  37. token(["number", "boolean"], { foreground: theme.syntaxNumber }),
  38. token(["keyword"], { foreground: theme.syntaxKeyword }),
  39. token(["function", "function.call", "function.method.call"], {
  40. foreground: theme.syntaxFunction,
  41. }),
  42. token(["type", "type.builtin"], { foreground: theme.syntaxType }),
  43. token(["operator"], { foreground: theme.syntaxOperator }),
  44. token(["punctuation", "punctuation.bracket"], { foreground: theme.syntaxPunctuation }),
  45. token(["diff.plus"], { foreground: theme.diffAdded }),
  46. token(["diff.minus"], { foreground: theme.diffRemoved }),
  47. token(["diff.delta"], { foreground: theme.diffHunkHeader }),
  48. token(["error"], { foreground: theme.error }),
  49. token(["warning"], { foreground: theme.warning }),
  50. token(["info"], { foreground: theme.info }),
  51. ];
  52. }
  53. export function createMarkdownSyntaxStyle(theme: TuiThemeTokens): SyntaxStyle | undefined {
  54. try {
  55. return SyntaxStyle.fromTheme(markdownSyntaxRules(theme));
  56. } catch {
  57. return undefined;
  58. }
  59. }
  60. function token(
  61. scope: string[],
  62. style: ThemeTokenStyle["style"],
  63. ): ThemeTokenStyle {
  64. return { scope, style };
  65. }