renderer.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package markdown
  2. import (
  3. "strconv"
  4. "strings"
  5. "github.com/alecthomas/chroma/v2"
  6. "github.com/alecthomas/chroma/v2/lexers"
  7. "github.com/alecthomas/chroma/v2/styles"
  8. "github.com/ayn2op/discordo/internal/config"
  9. "github.com/ayn2op/discordo/internal/ui"
  10. "github.com/ayn2op/tview"
  11. "github.com/diamondburned/ningen/v3/discordmd"
  12. "github.com/gdamore/tcell/v3"
  13. "github.com/yuin/goldmark/ast"
  14. )
  15. type Renderer struct {
  16. cfg *config.Config
  17. listIx *int
  18. listNested int
  19. }
  20. const codeBlockIndent = " "
  21. func NewRenderer(cfg *config.Config) *Renderer {
  22. return &Renderer{cfg: cfg}
  23. }
  24. func (r *Renderer) RenderLines(source []byte, node ast.Node, base tcell.Style) []tview.Line {
  25. r.listIx = nil
  26. r.listNested = 0
  27. builder := tview.NewLineBuilder()
  28. styleStack := []tcell.Style{base}
  29. currentStyle := func() tcell.Style {
  30. return styleStack[len(styleStack)-1]
  31. }
  32. pushStyle := func(style tcell.Style) {
  33. styleStack = append(styleStack, style)
  34. }
  35. popStyle := func() {
  36. if len(styleStack) > 1 {
  37. styleStack = styleStack[:len(styleStack)-1]
  38. }
  39. }
  40. theme := r.cfg.Theme.MessagesList
  41. _ = ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
  42. switch node := node.(type) {
  43. case *ast.Document:
  44. // noop
  45. case *ast.Heading:
  46. if entering {
  47. builder.Write(strings.Repeat("#", node.Level)+" ", currentStyle())
  48. } else {
  49. builder.NewLine()
  50. }
  51. case *ast.Text:
  52. if entering {
  53. builder.Write(string(node.Segment.Value(source)), currentStyle())
  54. switch {
  55. case node.HardLineBreak():
  56. builder.NewLine()
  57. builder.NewLine()
  58. case node.SoftLineBreak():
  59. builder.NewLine()
  60. }
  61. }
  62. case *ast.FencedCodeBlock:
  63. if entering {
  64. builder.NewLine()
  65. r.renderFencedCodeBlock(builder, source, node, currentStyle())
  66. }
  67. case *ast.AutoLink:
  68. if entering {
  69. builder.Write(string(node.URL(source)), ui.MergeStyle(currentStyle(), theme.URLStyle.Style))
  70. }
  71. case *ast.Link:
  72. if entering {
  73. pushStyle(ui.MergeStyle(currentStyle(), theme.URLStyle.Style))
  74. } else {
  75. popStyle()
  76. }
  77. case *ast.List:
  78. if node.IsOrdered() {
  79. start := node.Start
  80. r.listIx = &start
  81. } else {
  82. r.listIx = nil
  83. }
  84. if entering {
  85. builder.NewLine()
  86. r.listNested++
  87. } else {
  88. r.listNested--
  89. }
  90. case *ast.ListItem:
  91. if entering {
  92. builder.Write(strings.Repeat(" ", r.listNested-1), currentStyle())
  93. if r.listIx != nil {
  94. builder.Write(strconv.Itoa(*r.listIx)+". ", currentStyle())
  95. *r.listIx++
  96. } else {
  97. builder.Write("- ", currentStyle())
  98. }
  99. } else {
  100. builder.NewLine()
  101. }
  102. case *discordmd.Inline:
  103. if entering {
  104. pushStyle(applyInlineAttr(currentStyle(), node.Attr))
  105. } else {
  106. popStyle()
  107. }
  108. case *discordmd.Mention:
  109. if entering {
  110. builder.Write(mentionText(node), ui.MergeStyle(currentStyle(), theme.MentionStyle.Style))
  111. }
  112. case *discordmd.Emoji:
  113. if entering {
  114. builder.Write(":"+node.Name+":", ui.MergeStyle(currentStyle(), theme.EmojiStyle.Style))
  115. }
  116. }
  117. return ast.WalkContinue, nil
  118. })
  119. return builder.Finish()
  120. }
  121. func (r *Renderer) renderFencedCodeBlock(builder *tview.LineBuilder, source []byte, node *ast.FencedCodeBlock, base tcell.Style) {
  122. var code strings.Builder
  123. lines := node.Lines()
  124. for i := range lines.Len() {
  125. line := lines.At(i)
  126. code.Write(line.Value(source))
  127. }
  128. language := strings.TrimSpace(string(node.Language(source)))
  129. lexer := lexers.Get(language)
  130. declaredLanguageSupported := lexer != nil
  131. // Detect the language from its content.
  132. var analyzed bool
  133. if lexer == nil {
  134. lexer = lexers.Analyse(code.String())
  135. analyzed = lexer != nil
  136. }
  137. if lexer == nil {
  138. lexer = lexers.Fallback
  139. }
  140. // At this point, it should be noted that some lexers can be extremely chatty.
  141. // To mitigate this, use the coalescing lexer to coalesce runs of identical token types into a single token.
  142. lexer = chroma.Coalesce(lexer)
  143. // Show a fallback header when the language is omitted or unknown.
  144. headerStyle := base.Dim(true)
  145. if analyzed {
  146. builder.Write(codeBlockIndent+"code: analyzed", headerStyle)
  147. builder.NewLine()
  148. } else if language == "" {
  149. builder.Write(codeBlockIndent+"code", headerStyle)
  150. builder.NewLine()
  151. } else if !declaredLanguageSupported {
  152. builder.Write(codeBlockIndent+"code: "+language, headerStyle)
  153. builder.NewLine()
  154. }
  155. iterator, err := lexer.Tokenise(nil, code.String())
  156. if err != nil {
  157. for i := range lines.Len() {
  158. line := lines.At(i)
  159. builder.Write(codeBlockIndent+string(line.Value(source)), base)
  160. }
  161. return
  162. }
  163. theme := styles.Get(r.cfg.Markdown.Theme)
  164. if theme == nil {
  165. theme = styles.Fallback
  166. }
  167. builder.Write(codeBlockIndent, base)
  168. for token := iterator(); token != chroma.EOF; token = iterator() {
  169. style := applyChromaStyle(base, theme.Get(token.Type))
  170. // Chroma tokens may include embedded newlines, so split and re-emit with indentation on each visual line.
  171. parts := strings.Split(token.Value, "\n")
  172. for i, part := range parts {
  173. if i > 0 {
  174. builder.NewLine()
  175. builder.Write(codeBlockIndent, base)
  176. }
  177. if part != "" {
  178. builder.Write(part, style)
  179. }
  180. }
  181. }
  182. }
  183. func applyChromaStyle(base tcell.Style, entry chroma.StyleEntry) tcell.Style {
  184. style := base
  185. if entry.Colour.IsSet() {
  186. style = style.Foreground(tcell.NewRGBColor(
  187. int32(entry.Colour.Red()),
  188. int32(entry.Colour.Green()),
  189. int32(entry.Colour.Blue()),
  190. ))
  191. }
  192. // Intentionally do not apply token background colors so code blocks keep the user's terminal/chat background.
  193. // if entry.Background.IsSet() {
  194. // style = style.Background(tcell.NewRGBColor(
  195. // int32(entry.Background.Red()),
  196. // int32(entry.Background.Green()),
  197. // int32(entry.Background.Blue()),
  198. // ))
  199. // }
  200. switch entry.Bold {
  201. case chroma.Yes:
  202. style = style.Bold(true)
  203. case chroma.No:
  204. style = style.Bold(false)
  205. }
  206. switch entry.Italic {
  207. case chroma.Yes:
  208. style = style.Italic(true)
  209. case chroma.No:
  210. style = style.Italic(false)
  211. }
  212. switch entry.Underline {
  213. case chroma.Yes:
  214. style = style.Underline(true)
  215. case chroma.No:
  216. style = style.Underline(false)
  217. }
  218. return style
  219. }
  220. func mentionText(node *discordmd.Mention) string {
  221. switch {
  222. case node.Channel != nil:
  223. return "#" + node.Channel.Name
  224. case node.GuildUser != nil:
  225. name := node.GuildUser.DisplayOrUsername()
  226. if member := node.GuildUser.Member; member != nil && member.Nick != "" {
  227. name = member.Nick
  228. }
  229. return "@" + name
  230. case node.GuildRole != nil:
  231. return "@" + node.GuildRole.Name
  232. default:
  233. return ""
  234. }
  235. }
  236. func applyInlineAttr(style tcell.Style, attr discordmd.Attribute) tcell.Style {
  237. switch attr {
  238. case discordmd.AttrBold:
  239. return style.Bold(true)
  240. case discordmd.AttrItalics:
  241. return style.Italic(true)
  242. case discordmd.AttrUnderline:
  243. return style.Underline(true)
  244. case discordmd.AttrStrikethrough:
  245. return style.StrikeThrough(true)
  246. case discordmd.AttrMonospace:
  247. return style.Reverse(true)
  248. }
  249. return style
  250. }