Created
June 24, 2025 04:13
-
-
Save elliotboney/a452107c1d35bafe5dc506d55e68ecea to your computer and use it in GitHub Desktop.
Google apps script to turn a google doc from markdown code to formatted google doc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convertFromMarkdown(file_idz) { | |
// Ensure the Google Drive Advanced Service is enabled (Steps below) | |
// Get the ID of the Google Doc to be updated | |
var file_id = file_idz || "put_a_default_id_here_if_you_want"; | |
// Open the document to read its content | |
const docFile = DocumentApp.openById(file_id); | |
const markdownContent = docFile.getBody().getText(); | |
// Create a blob with the markdown content. | |
// The MimeType 'text/markdown' is crucial for Drive to recognize it as markdown. | |
const blob = Utilities.newBlob(markdownContent, 'text/markdown'); | |
// Update the existing Google Doc with the new content. | |
// The 'convert: true' option tells Google Drive to convert the incoming blob | |
// (which is markdown) into a Google Docs format. | |
const updatedFile = Drive.Files.update( | |
{}, // Resource body: We're not changing file metadata (like name or parent), just content. | |
file_id, // The ID of the file to update | |
blob, // The new content (markdown blob) | |
{ | |
convert: true, // This is key: tells Drive to convert the markdown blob into Google Doc format | |
supportsAllDrives: true // Required if your file might be on a Shared Drive | |
} | |
); | |
// Return the ID of the updated file (which is the same as the original file_id) | |
return updatedFile.id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment