SaswatPadhi / pseudocode.js

Beautiful pseudocode for the Web
https://saswatpadhi.github.io/pseudocode.js
MIT License
221 stars 33 forks source link

JSON parsing error in `renderElement` #42

Closed femtomc closed 1 year ago

femtomc commented 1 year ago

I'm trying to run the README example -- but I'm running into a JSON.parse error in renderElement:

image

I'm building a static site through quarto and hugo, and I've confirmed that my HTML output is what I expect -- specifically:

<pre id="hello-world-code" class="pseudocode">
  \begin{algorithmic}
  \PRINT \texttt{'hello world'}
  \end{algorithmic}
</pre>
<script>
    pseudocode.renderElement(document.getElementById("hello-world-code"));
</script>

gets inserted into my generated HTML (as I would expect). I also fetch pseudocode.js from CDN in header.

Any mistake I've made standing out?

femtomc commented 1 year ago

If I literally just paste:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pseudocode@latest/build/pseudocode.min.css">
<script src="https://cdn.jsdelivr.net/npm/pseudocode@latest/build/pseudocode.min.js"></script>

<pre id="hello-world-code">
    \begin{algorithmic}
    \PRINT \texttt{'hello world'}
    \end{algorithmic}
</pre>

<script>
    pseudocode.renderElement(document.getElementById("hello-world-code"));
</script>

into an HTML file, I get the same error, unfortunately.

SaswatPadhi commented 1 year ago

Hi @femtomc,

Thanks for reporting this. Seems like one of my recent changes is incompatible with an undefined options parameter. Setting it to empty {} works as expected. Please see https://jsfiddle.net/hy1sa537/

I am working on a fix right now.

SaswatPadhi commented 1 year ago

Found the issue. It's this line:

https://github.com/SaswatPadhi/pseudocode.js/blob/f898035581e5f57c7b125493e9fab68701aeecba/pseudocode.js#L50

introduced in 30f2576509167727ea3ff356ad2635e9df9505ce to allow per-element overriding of options.

JSON.parse doesn't like empty strings :man_facepalming: