Search code examples
javascriptdocusaurus

how can I execute a helper method function and run it in Docusaurus's markdown


I have a file that contains Markdown:

# title example
example text that your mind has nothing to do with.

and I want to transform the text to uppercase.

is there any way to write a javascript function, then run it over the whole text of that markdown automatically?

the method as this case example is the toUpperCase().


Solution

  • I don't know if someone might need this, but here is some solutions I found out that can be applicable:

    1. If you have a few files or a single file as an example that you want to run a method within it, you can use the <script></script> inside the .mdx file, like this :

       <script>
         document.body.innerHTML = document.body.innerHTML.toUpperCase();
       </script>
      

    This will be executed when the documentation page loads.


    1. if you want to execute some code across all your documentation pages, you can make a serverMiddleware, inside the plugins folder under your project directory, make as an example a uppercase.js file, then set your code like this:

       module.exports = {
        serverMiddleware: (app) => {
          app.use((req, res, next) => {
            res.on('finish', () => {
              const body = res._getData().toString().toUpperCase();
              res.setHeader('Content-Length', Buffer.byteLength(body));
              res.end(body);
            });
            next();
          });
        },
      };
      

    then declare that plugin inside the docusaurus.config.js :

    module.exports = {
      // ...
      plugins: ['uppercase'],
      // ...
    }