How to convert Google Docs to Markdown format
Posted by Jim DeLaHunt on 30 Apr 2019 at 11:03 pm | Tagged as: robobait, software engineering
Recently I needed to convert a Google Docs wordprocessing document to Markdown format (Github’s dialect). A simple web search turned up several hits, most of them unhelpful. I finally found a Google Apps script to do the conversion, which was almost, but not quite, suitable. But with a simple modification, it did the trick. I am sharing it here, in the hope that it will be helpful to someone else searching for “convert Google Docs to Markdown”.
Google Docs is a “What You See is What You Get” visual word processor. Its own document format is opaque. But it lets you download your documents in a variety of formats: OpenDocument (.ODF), HTML, PDF, plain text. Sadly, Markdown is not on that list. Markdown is a simple markup language. Several web apps and services use Markdown for applying simple formatting and hyperlinks to text documents. In particular, GitHub uses Markdown for documentation and issues pages.
My need was to break a Google Docs document into pieces, with formatting and hyperlinks intact, and upload the pieces to GitHub. GitHub accepts Markdown formatting. So, if I could just convert the Google Docs document to Markdown, I could (fairly easily) break it into pieces and upload it to GitHub. Today’s tale is how to convert a Google Docs document to Markdown. What I learned about breaking it into pieces and uploading to GitHub is a story for another day.
I found a project, gdocs2md by Renato Mangini, on GitHub at https://github.com/mangini/gdocs2md/ . This is a script which you can install into a Google Docs document. It is written in Google’s Apps Script, a JavaScript-like language with interfaces for manipulating Google Apps documents. One installs scripts into a Google Apps document, similar to how one can install Visual Basic scripts into a Microsoft Word or Excel document.
Mangini’s gdocs2md ReadMe file gives good instructions for how to install the script into a Google Apps document. I won’t repeat those here. They should be clear to a software developer familiar with scripting, and I won’t try here to teach scripting to someone who doesn’t yet know how to script. One invokes the script by calling a “ConvertToMarkdown” function from Google Docs.
Unfortunately, the gdocs2md script can only deliver the converted Markdown form of the document by emailing it. I didn’t want to do that. The document contents were somewhat confidential. I was happier keeping it off the insecure public email system and away from possible eavesdroppers. I preferred to have the script pop up a dialogue box with the converted Markdown text, allowing me to copy the text and paste it into a text editor for further manipulation.
It turns out that I could make a simple modification to gdocs2md, so that it pops up a dialogue box instead of emailing. It took me a bit of searching to find out what code would pop up the dialogue box. Let me save you that work. In file “converttomarkdown.gapps”, lines 71-77, which is the end of function ConvertToMarkdown(), insert ‘//’ in front of each of the lines below, to comment them out:
 attachments.push({"fileName":DocumentApp.getActiveDocument().getName()+".md", "mimeType": "text/plain", "content": text});  MailApp.sendEmail(Session.getActiveUser().getEmail(),                    "[MARKDOWN_MAKER] "+DocumentApp.getActiveDocument().getName(),                    "Your converted markdown document is attached (converted from "+DocumentApp.getActiveDocument().getUrl()+")"+                    "\n\nDon't know how to use the format options? See http://github.com/mangini/gdocs2md\n",                    { "attachments": attachments });
After those lines, insert the following lines:
 var ui = HtmlService.createHtmlOutput("<pre>"+text+"</pre>")  DocumentApp.getUi().showModelessDialog(ui,"Markdown");
Use the File… Save menu command (or click the button with the quaint diskette icon) to save the script file into the document.
Now, when you follow the gdocs2md instructions, calling the “ConvertToMarkdown” function, it will pop up a dialogue box titled “Markdown” instead of sending email. The converted Markdown document text is in the dialogue box. Click on the text, select it all, copy it, and paste it where you need it.
I hope this makes it easier for others to convert Google Docs documents to Markdown format in the future.
P.S. I have offered this code to gdocs2md as Issue #65, “Put converted text in a dialogue box instead of emailing (enhancement)”.
Hi Jim,
I used Renato’s script a while ago and contributed some enhancements. When Docs add-ons became a thing, I created Docs to Markdown to do conversion. It is not based on Renato’s code, but certainly inspired by it!
See https://github.com/evbacher/gd2md-html/wiki for details and links to install the add-on.
Best,
– Ed
Thank you for the link, Ed! It’s great to have another alternative. If I had found your script first, I probably would have used it instead.