Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yearofthewhopper/867bd8f01c273843a65836b9d32d55a1 to your computer and use it in GitHub Desktop.
Save yearofthewhopper/867bd8f01c273843a65836b9d32d55a1 to your computer and use it in GitHub Desktop.
function convertMarkdownToPlainText() {
// Get the active document and body
var body = DocumentApp.getActiveDocument().getBody();
// Retrieve the text from the document
var text = body.getText();
// Replace common Markdown patterns with plain text equivalents
text = text.replace(/__([^_]+)__/g, '$1'); // bold
text = text.replace(/\*\*([^\*]+)\*\*/g, '$1'); // bold
text = text.replace(/_([^_]+)_/g, '$1'); // italic
text = text.replace(/\*([^\*]+)\*/g, '$1'); // italic
text = text.replace(/\#\#([^\#]+)\#\#/g, '$1'); // headers
text = text.replace(/\#\#\#([^\#]+)\#\#\#/g, '$1'); // smaller headers
// Set the new text
body.setText(text);
}
function onOpen() {
var ui = DocumentApp.getUi();
// Create a new menu in Google Docs with a single menu item
ui.createMenu('Markdown Converter')
.addItem('Convert to Plain Text', 'convertMarkdownToPlainText')
.addToUi();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment