You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
where someFile.md.js contains the snippet compiled to Javascript via ScalaJS.
If I run docs/mdoc --watch the web UI allows to open the new file and the JS is executed.
The problem
However when Docusaurus v2 (now default) is used with sbt-mdoc, these scripts are not executed.
This is connected to Docusaurus being a React-based SPA.
It is a pity not to use this very nice feature of mdoc!
Some ideas to solve the problem
Docusaurus accepts MDXv1 files as we can read here. This means two things:
We can include JSX code into the markdown files that mdoc generates
We can include JSX code that invokes the Javascript file produced by ScalaJS
In particular we can can modify this section to add JSX code.
My first idea was to inject something like this at the end of the generated markdown:
where BrowserOnly is needed to delay execution to when document will be available.
We then to make sure that someFIle.md.js can be accessed statically (I had to copy to another folder to be able to download it).
But this is not working yet maybe because of webpack not including it, but I am not sure. I see that:
No code is executed (console.logs inside the useEffect hook are never executed)
The script is not added
You can experiment modifying the generated markdown that is watched by Docusaurus without modifying the source of mdoc
The text was updated successfully, but these errors were encountered:
I experimented a bit with alternative site generators and I found that using the new scaladoc with mdoc provides a very lightweight strategy to fix this.
ScalaJS snippets
Markdown files containing snippets with the modifier
scala mdoc:js
are transformed into markdown files which end with two script tagswhere
someFile.md.js
contains the snippet compiled to Javascript via ScalaJS.If I run
docs/mdoc --watch
the web UI allows to open the new file and the JS is executed.The problem
However when Docusaurus v2 (now default) is used with
sbt-mdoc
, these scripts are not executed.This is connected to Docusaurus being a React-based SPA.
It is a pity not to use this very nice feature of mdoc!
Some ideas to solve the problem
Docusaurus accepts MDXv1 files as we can read here. This means two things:
In particular we can can modify this section to add JSX code.
My first idea was to inject something like this at the end of the generated markdown:
were
fun
is the function exported in the Javascript file created by ScalaJS.This is the tricky part, where I am stuck, on how to make this function available here:
First approach: proper imports
Change ScalaJS settings to compile an ESModule and import the function here using
raw-loader
. However this does not seem to be supportedSecond approach: dirty dom manipulation
This approach follows the current implementation (it mimics
mdoc.js
):node
div is readyBecause Docusaurus uses react this is particularly tricky. It could like this:
where
BrowserOnly
is needed to delay execution to whendocument
will be available.We then to make sure that
someFIle.md.js
can be accessed statically (I had to copy to another folder to be able to download it).But this is not working yet maybe because of webpack not including it, but I am not sure. I see that:
console.log
s inside theuseEffect
hook are never executed)You can experiment modifying the generated markdown that is watched by Docusaurus without modifying the source of mdoc
The text was updated successfully, but these errors were encountered: