@@ -51,8 +51,55 @@ for (let i = 2; i < args.length; i++) {
5151 }
5252}
5353
54- // Read input cast file.
55- const input = fs . readFileSync ( inputFile , 'utf8' ) ;
54+ // Read input cast file and convert v3 to v2 if needed.
55+ // svg-term only supports asciicast v1 and v2 formats, but asciinema 3.x
56+ // produces v3 format with two breaking differences:
57+ // 1. Header uses {term: {cols, rows, type}} instead of {width, height}
58+ // 2. Timestamps are relative (delta from previous event) not absolute
59+ // Additionally, v3 introduces event type "x" (exit) which v2 doesn't have.
60+ let input = fs . readFileSync ( inputFile , 'utf8' ) ;
61+ const lines = input . split ( '\n' ) ;
62+ if ( lines . length > 0 ) {
63+ try {
64+ const header = JSON . parse ( lines [ 0 ] ) ;
65+ if ( header . version === 3 ) {
66+ // Convert header.
67+ header . version = 2 ;
68+ if ( header . term ) {
69+ header . width = header . term . cols ;
70+ header . height = header . term . rows ;
71+ if ( ! header . env ) {
72+ header . env = { } ;
73+ }
74+ if ( header . term . type ) {
75+ header . env . TERM = header . term . type ;
76+ }
77+ delete header . term ;
78+ }
79+ // Convert event lines: relative timestamps to absolute, drop non-"o" events.
80+ const convertedLines = [ JSON . stringify ( header ) ] ;
81+ let absoluteTime = 0 ;
82+ for ( let i = 1 ; i < lines . length ; i ++ ) {
83+ const line = lines [ i ] . trim ( ) ;
84+ if ( ! line ) {
85+ continue ;
86+ }
87+ try {
88+ const event = JSON . parse ( line ) ;
89+ absoluteTime += event [ 0 ] ;
90+ if ( event [ 1 ] === 'o' ) {
91+ convertedLines . push ( JSON . stringify ( [ parseFloat ( absoluteTime . toFixed ( 6 ) ) , 'o' , event [ 2 ] ] ) ) ;
92+ }
93+ } catch ( _ ) {
94+ // Skip malformed lines.
95+ }
96+ }
97+ input = convertedLines . join ( '\n' ) + '\n' ;
98+ }
99+ } catch ( _ ) {
100+ // Not valid JSON header - let svg-term handle the error.
101+ }
102+ }
56103
57104// Define custom theme with lineHeight set to 1.0.
58105// Based on Atom One Dark theme colors.
0 commit comments