Issue with Formatting Markdown Data in create document from a template Google Docs Module

Hi everyone,

I’m facing an issue when trying to create a document from a template using the Google Docs module in Make.com. Here’s the problem:

I want to generate a document from a template, but the data I need to insert is in Markdown format.
• When I pass the Markdown data directly to the template, it appears in the document as raw Markdown code instead of being formatted.
• I also tried converting the Markdown to HTML before passing it to the template, but the result still shows the raw HTML code in the document, not the formatted content.

Has anyone faced a similar issue or knows how to properly handle Markdown/HTML data in this scenario? Any help or tips would be greatly appreciated!

Thanks in advance!

2 Likes

Hello @Yonatan_Tesfaye,
Please check full discussion on this topic which is related to this topic.

:+1:

1 Like

Hi there @younatan_tesfaye

Did you find a solution to this? I trying following the instructions in what is suggested on the reply to this post but I couldn’t get it to work? It may be because I am using a template and have mapped fields?

Any advice would be very much appreciated :pray:t2:

2 Likes

Hey @Tom_Street
I didn’t get any solutions yet for the template one but it will work on the create document module (blank doc) just change the markdown to HTML and then it will pars the HTML.

3 Likes

Thanks @Yonatan_Tesfaye. I will update if I find any solution.

1 Like

@Yonatan_Tesfaye I resolved this issue by creating an apps script in the template. This is what I used:

function processNewDocs() {
  var folder = DriveApp.getFolderById("YOUR_FOLDER_ID_HERE"); // Replace with your actual folder ID
  var files = folder.getFiles();
  
  while (files.hasNext()) {
    var file = files.next();
    if (file.getMimeType() === MimeType.GOOGLE_DOCS) {
      var doc = DocumentApp.openById(file.getId());
      processMarkdownInDoc(doc);
    }
  }
}

function processMarkdownInDoc(doc) {
  var body = doc.getBody();
  var textElement = body.editAsText();

  // Process Headers (using regex that allows an optional space and enforces the correct number of '#' characters)
  // H1: Exactly one '#' (not followed by another '#')
  applyHeaderFormatting(textElement, /^#(?!#)\s?(.*)$/gm, 24, true);
  // H2: Exactly two '#' (not followed by a third)
  applyHeaderFormatting(textElement, /^##(?!#)\s?(.*)$/gm, 20, true);
  // H3: Exactly three '#' (not followed by a fourth)
  applyHeaderFormatting(textElement, /^###(?!#)\s?(.*)$/gm, 18, true);
  // H4: Exactly four '#' (not followed by a fifth)
  applyHeaderFormatting(textElement, /^####(?!#)\s?(.*)$/gm, 16, true);
  // H5: Exactly five '#' (not followed by a sixth)
  applyHeaderFormatting(textElement, /^#####(?!#)\s?(.*)$/gm, 14, true);

  // Process bold (**text**)
  applyTextFormatting(textElement, /\*\*(.*?)\*\*/g, "bold");

  // Process italics (*text* and _text_)
  applyTextFormatting(textElement, /(?:\*|_)(.*?)(?:\*|_)/g, "italic");

  // Save the document
  doc.saveAndClose();
}

function applyHeaderFormatting(textElement, regex, fontSize, bold) {
  var text = textElement.getText();
  // Get all matches using the provided regex
  var matches = [...text.matchAll(regex)];

  // Process matches in reverse order to avoid index shifting issues after modifying the text
  for (let i = matches.length - 1; i >= 0; i--) {
    let match = matches[i];
    let start = match.index;
    let headerContent = match[1];
    let contentLength = headerContent.length;

    // Delete the entire matched header (including the '#' symbols and any extra whitespace)
    textElement.deleteText(start, start + match[0].length - 1);
    // Insert the header text without markdown symbols
    textElement.insertText(start, headerContent);
    // Apply the header formatting (font size and bold if requested)
    textElement.setFontSize(start, start + contentLength - 1, fontSize);
    if (bold) {
      textElement.setBold(start, start + contentLength - 1, true);
    }
  }
}

function applyTextFormatting(textElement, regex, formatType) {
  var text = textElement.getText();
  // Get all matches for the markdown (e.g. **text** or *text*/*_text_)
  var matches = [...text.matchAll(regex)];

  // Process matches in reverse order so that modifications don’t affect the positions of earlier matches
  for (let i = matches.length - 1; i >= 0; i--) {
    let match = matches[i];
    let start = match.index;
    let content = match[1];
    let contentLength = content.length;

    // Remove the markdown symbols by deleting the whole matched string
    textElement.deleteText(start, start + match[0].length - 1);
    // Reinsert just the content (without markdown symbols)
    textElement.insertText(start, content);

    // Apply the desired formatting (bold or italic)
    if (formatType === "bold") {
      textElement.setBold(start, start + contentLength - 1, true);
    } else if (formatType === "italic") {
      textElement.setItalic(start, start + contentLength - 1, true);
    }
  }
}
4 Likes

:rocket: Yaa that works :brain: Thank you

2 Likes

Awesome, thank you! It works but only if I run it in each document. When a new document is created from my template it doesn’t automatically convert the Markdown-script to proper formatting. In appscript for the new document from template it always asks for permissions each time and then I need to run it again to convert. How did you solve this? Thank you in advance!

2 Likes

Hey there @Nextfav :wave:

Welcome to the community, we are excited you decided to join us! :ferry:

Could you please open a new conversation with your question? This way you are more likely to receive assistance from our community heroes!
You can always add a link to this post as a reference for others.

Thank you very much and have a wonderful day! :sunny:

2 Likes