import { BlockInfo } from "block-info" ; import { extractAttributeDerivatives , LitvisNarrative } from "litvis" ; import YAML from "yamljs" ; import { LitvisEnhancerCache } from "../types" ; param $ param processedNarrative param cache export const enhanceWithLitvisVegaBlockKeywords = async ( $ , processedNarrative , cache ) : Promise < void > => { $ ( '[data-role="codeBlock"]' ) . each ( ( i , container ) => { const $container = $ ( container ) ; if ( $container . data ( "executor" ) ) { return ; } const info = $container . data ( "parsedInfo" ) ; if ( ! info ) { return ; } const normalizedInfo = $container . data ( "normalizedInfo" ) ; if ( normalizedInfo . language !== "vega" && normalizedInfo . language !== "vega-lite" ) { return ; } const derivatives = extractAttributeDerivatives ( info . attributes ) ; if ( ! derivatives ) { return ; } const text = $container . text ( ) ; const spec = text . trim ( ) ; let data ; let dataParseError ; try { if ( spec [ 0 ] !== "{" ) { data = YAML . parse ( spec ) ; } else { data = JSON . parse ( spec ) ; } } catch ( e ) { dataParseError = e ; } derivatives . outputFormats . forEach ( ( outputFormat ) => { if ( derivatives . outputExpressionsByFormat [ outputFormat ] ) { return ; } let $result ; let resultNormalizedInfo ; let resultText ; switch ( outputFormat ) { case "r" : $result = $ ( `<pre data-role="codeBlock" />` ) ; resultNormalizedInfo = { language : "" , attributes : { } , style : "display: inline-block" , } ; resultText = text ; break ; case "j" : $result = $ ( `<pre data-role="codeBlock" />` ) ; resultNormalizedInfo = { language : "json" , attributes : { } , style : "display: inline-block" , } ; if ( dataParseError ) { resultText = dataParseError . message ; } else { resultText = JSON . stringify ( data , null , 2 ) ; } break ; case "v" : { $result = $ ( `<pre data-role="codeBlock" />` ) ; resultNormalizedInfo = { language : normalizedInfo . language , attributes : { interactive : normalizedInfo . attributes . interactive , style : "display: inline-block" , } , } ; resultText = text ; break ; } } if ( ! $result ) { return ; } $result . text ( resultText ) ; $result . data ( "normalizedInfo" , resultNormalizedInfo ) ; $container . before ( $result ) ; } ) ; $container . remove ( ) ; } ) ; } ;