HACKER Q&A
📣 defaultcompany

Tool to convert JavaScript code comments into Markdown?


I would like a tool which takes code in a javascript project and generates markdown where the comments are taken as direct markdown and the code is wrapped in triple backticks. This would allow me to make a blog post from a javascript project without maintaining two separate files and updating the code in both.

For example if I have this in a javascript file:

  /*
  # Tutorial Title

  Welcome to the codebase tutorial.

  ## Add Function

  This function is used to add numbers. `1+2=3` is an example of some backquoted text.
  */
  function add(a,b) {
    return a + b;
  }

  /*
  ## Subtract Function

  This function is used to subtract! ***Use at your own risk***
  */
  function subtract(a,b) {
    return a - b;
  }
I would like the resulting markdown to be:

  # Tutorial Title

  Welcome to the codebase tutorial.

  ## Add Function

  This function is used to add numbers. `1+2=3` is an example of some backquoted text.
  ```
  function add(a,b) {
    return a + b;
  }
  ```

  ## Subtract Function

  This function is used to subtract! ***Use at your own risk***
  ```
  function subtract(a,b) {
    return a - b;
  }
  ```
Does anyone know of a tool which will do this?


  👤 austincheney Accepted Answer ✓
I don’t know the library responsible but VS Code does this automatically for TypeScript code. Simply apply a block comment immediately prior to a type definition or the application of a type definition and that comment becomes rendered markdown in the tooltip elsewhere the code is called.

👤 qq4
Why not delete the first line and replace the rest of the comment symbols with triple back ticks? Seems good enough for jazz to me.

  sed -e 's/\/\*/```/g' -e 's/\*\//```/g' -e '1d'