@@ -27,41 +27,50 @@ export default class ObsidianFunctionPlot extends Plugin {
2727 }
2828
2929 async functionPlotHandler ( source : string , el : HTMLElement , _ctx : MarkdownPostProcessorContext ) : Promise < void > {
30- // styles
31- el . classList . add ( 'functionplot' )
32- // parse yaml for bounds and functions to plot
33- const header = ( source . match ( / - { 3 , } ( [ ^ ] + ) - { 3 , } / ) || [ null , null ] ) [ 1 ]
34- console . log ( header )
35- const functions = ( header ? source . substring ( header . length ) : source )
36- . split ( '\n' )
37- . map ( line => line . trim ( ) )
38- . filter ( line => line . length > 0 )
39-
40- const config : HeaderOptions = Object . assign (
41- { } ,
42- DEFAULT_HEADER_OPTIONS ,
43- parseYaml ( header || '' )
44- )
30+ try {
31+ // styles
32+ el . classList . add ( 'functionplot' )
33+ // parse yaml for bounds and functions to plot
34+
35+ const header = ( source . match ( / - { 3 } [ ^ ] + - { 3 } / ) || [ null ] ) [ 0 ]
36+ const functions = ( header ? source . substring ( header . length ) : source )
37+ . split ( '\n' )
38+ . map ( line => line . trim ( ) )
39+ . filter ( line => line . length > 0 )
40+
41+ const config : HeaderOptions = Object . assign (
42+ { } ,
43+ DEFAULT_HEADER_OPTIONS ,
44+ parseYaml ( header . match ( / - { 3 , } ( [ ^ ] + ?) - { 3 , } / ) [ 1 ] || '' )
45+ )
4546
46- // prepare options for call to FunctionPlotya
47- const fPlotOptions : FunctionPlotOptions = {
48- target : el as unknown as string , // weird workaround
49- title : config . title ,
50- grid : config . grid ,
51- disableZoom : config . disableZoom ,
52- xAxis : {
53- domain : [ config . bounds [ 0 ] , config . bounds [ 1 ] ] ,
54- label : config . xLabel
55- } ,
56- yAxis : {
57- domain : [ config . bounds [ 2 ] , config . bounds [ 3 ] ] ,
58- label : config . yLabel
59- } ,
60- data : functions . map ( line => { return { "fn" : line . split ( '=' ) [ 1 ] . trim ( ) } } )
47+ // prepare options for call to FunctionPlotya
48+ const fPlotOptions : FunctionPlotOptions = {
49+ target : el as unknown as string , // weird workaround
50+ title : config . title ,
51+ grid : config . grid ,
52+ disableZoom : config . disableZoom ,
53+ xAxis : {
54+ domain : [ config . bounds [ 0 ] , config . bounds [ 1 ] ] ,
55+ label : config . xLabel
56+ } ,
57+ yAxis : {
58+ domain : [ config . bounds [ 2 ] , config . bounds [ 3 ] ] ,
59+ label : config . yLabel
60+ } ,
61+ data : functions . map ( line => { return { "fn" : line . split ( '=' ) [ 1 ] . trim ( ) } } )
62+ }
63+ // render
64+ functionPlot ( fPlotOptions )
65+ // make text listen to stylesheet
66+ el . querySelectorAll ( 'text' ) . forEach ( el => el . setAttribute ( 'fill' , 'currentColor' ) )
67+ } catch ( e ) {
68+ el . innerHTML = `
69+ <div style="border-radius:1em;background:black;opacity:0.5">
70+ <p style="opacity:1;font-size:0.7em;padding:1em">
71+ ${ e } \n\n${ source }
72+ </p>
73+ </div>`
6174 }
62- // render
63- functionPlot ( fPlotOptions )
64- // make text listen to stylesheet
65- el . querySelectorAll ( 'text' ) . forEach ( el => el . setAttribute ( 'fill' , 'currentColor' ) )
6675 }
6776}
0 commit comments