@@ -3,6 +3,15 @@ def format(color: 'white')
33 require 'tempfile'
44 require 'base64'
55
6+ # Terminal detection
7+ def kitty?
8+ ENV [ 'TERM' ] == 'xterm-kitty' || ENV [ 'TERM_PROGRAM' ] == 'Kitty'
9+ end
10+
11+ def iterm2?
12+ ENV [ 'TERM_PROGRAM' ] == 'iTerm.app'
13+ end
14+
615 # Get terminal columns, similar to tput cols
716 cols = `tput cols` . to_i
817 em = ( cols * 0.60 ) . to_f
@@ -38,15 +47,42 @@ def format(color: 'white')
3847 system ( "pandoc --pdf-engine=xelatex -V mainfont=\" IBM Plex Serif Light\" -f commonmark_x #{ tmp_file . path } -o #{ pdf_file } >/dev/null 2>&1" )
3948
4049 if File . exist? ( pdf_file )
41- pdf_data = File . read ( pdf_file )
42- base64_data = Base64 . strict_encode64 ( pdf_data )
43- # Clean up
44- tmp_file . unlink
45- File . unlink ( pdf_file )
46- # Return iTerm2 inline image sequence
47- "\e ]1337;File=inline=1;preserveAspectRatio=1:#{ base64_data } \a "
50+ if kitty?
51+ # Convert PDF to PNG for Kitty
52+ png_file = tmp_file . path . sub ( '.md' , '.png' )
53+ system ( "convert #{ pdf_file } #{ png_file } >/dev/null 2>&1" )
54+ if File . exist? ( png_file )
55+ png_data = File . read ( png_file )
56+ base64_data = Base64 . strict_encode64 ( png_data )
57+ # Clean up
58+ tmp_file . unlink
59+ File . unlink ( pdf_file )
60+ File . unlink ( png_file )
61+ # Return Kitty inline image sequence
62+ "\e _Gf=100,a=T,t=d;#{ base64_data } \e \\ "
63+ else
64+ # Fallback if PNG conversion fails
65+ tmp_file . unlink
66+ File . unlink ( pdf_file )
67+ self
68+ end
69+ elsif iterm2?
70+ pdf_data = File . read ( pdf_file )
71+ base64_data = Base64 . strict_encode64 ( pdf_data )
72+ # Clean up
73+ tmp_file . unlink
74+ File . unlink ( pdf_file )
75+ # Return iTerm2 inline image sequence
76+ "\e ]1337;File=inline=1;preserveAspectRatio=1:#{ base64_data } \a "
77+ else
78+ # Fallback for other terminals
79+ tmp_file . unlink
80+ File . unlink ( pdf_file )
81+ self
82+ end
4883 else
4984 # Fallback if PDF generation fails
85+ tmp_file . unlink
5086 self
5187 end
5288 end
0 commit comments