Open vincenzobaz opened 2 years ago
Thank you for the detailed report! One workaround for now is to use Docusaurus v1.
I'm happy to merge any PR that adds a custom code path to generate Docusaurus v2 compatible JS. The option can be read around here
We can also automatically pass the option from the sbt DocusaurusPlugin here https://github.com/scalameta/mdoc/blob/6a5be8cbde534662b27f1b68d2e12384a31216c9/mdoc-sbt/src/main/scala/mdoc/DocusaurusPlugin.scala#L98
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