import { BlockAttributes } from "block-attributes" ; import { AttributeDerivatives , BlockOutputFormat , OutputFormat , } from "../types" ; let unusedAutogeneratedContextId = 0 ; const normalizeExpression = ( value ) => { return `${ value }` . trim ( ) ; } ; const addOutputExpressions = ( derivatives , type , value ) => { const expressionsToAdd = [ ] ; if ( value instanceof Array ) { value . forEach ( ( v ) => { expressionsToAdd . push ( normalizeExpression ( v ) ) ; } ) ; } else if ( value !== true ) { expressionsToAdd . push ( normalizeExpression ( value ) ) ; } if ( expressionsToAdd . length || value === true ) { derivatives . outputFormats . push ( type as any as BlockOutputFormat ) ; } if ( expressionsToAdd . length ) { derivatives . outputExpressionsByFormat [ type ] = expressionsToAdd ; } } ; export const extractAttributeDerivatives = ( attributes ) : AttributeDerivatives | null => { const attributesWithMixIns = { ... attributes } ; if ( attributesWithMixIns . s === true || attributesWithMixIns . siding === true ) { attributesWithMixIns . isolated = true ; attributesWithMixIns . follows = "default" ; } if ( attributesWithMixIns . isolated ) { attributesWithMixIns . context = `_autogenerated__${ unusedAutogeneratedContextId }` ; unusedAutogeneratedContextId += 1 ; } const result = { contextName : typeof attributesWithMixIns . context !== "undefined" ? normalizeExpression ( attributesWithMixIns . context ) : "default" , outputFormats : [ ] , outputExpressionsByFormat : { } , id : attributesWithMixIns . id , follows : attributesWithMixIns . follows , } ; let isLitVis = false ; for ( const key in attributesWithMixIns ) { if ( Object . prototype . hasOwnProperty . call ( attributesWithMixIns , key ) ) { const value = attributesWithMixIns [ key ] ; switch ( key ) { case "l" : case "literate" : if ( value === false ) { return null ; } isLitVis = true ; if ( value !== "hidden" && attributesWithMixIns [ "hide" ] !== true ) { result . outputFormats . push ( "l" ) ; } break ; case "v" : case "visualize" : isLitVis = true ; addOutputExpressions ( result , "v" , value ) ; break ; case "r" : case "raw" : isLitVis = true ; addOutputExpressions ( result , "r" , value ) ; break ; case "j" : case "json" : isLitVis = true ; addOutputExpressions ( result , "j" , value ) ; break ; case "m" : case "markdown" : isLitVis = true ; addOutputExpressions ( result , "m" , value ) ; break ; case "interactive" : result . interactive = ! ! value ; break ; } } } if ( isLitVis ) { return result ; } return null ; } ;