How can I make a specific word bold within a paragraph using Google Apps Script in Google Docs?

I am working on a Google Apps Script that processes text in a Google Document and detects markdown formatting to style it properly, removing the markdown symbols after applying the styles. I’ve successfully transformed # example headline into a heading level one and ## example subheading into heading level two. However, I am facing challenges when trying to identify and format bold text, especially when it appears mid-sentence. It seems that Google Docs might split such sentences into multiple elements in the DOM. Here’s how my document appears:

# Main Title

## Subsection Title

Some **bolded** words and some *italicized* text.
Additional text follows on a fresh line.

Below is my existing function:

function formatDocumentFromMarkdown() {
  var document = DocumentApp.getActiveDocument();
  var documentBody = document.getBody();

  var paragraphsArray = documentBody.getParagraphs();

  var mainTitlePattern = /^# (.*)$/;
  var subsectionPattern = /^## (.*)$/;
  var boldTextPattern = /\*\*(.*?)\*\*/g;

  for (var i = 0; i < paragraphsArray.length; i++) {
    var currentParagraph = paragraphsArray[i];
    var paragraphContent = currentParagraph.getText();

    if (mainTitlePattern.test(paragraphContent)) {
      var titleText = paragraphContent.replace(mainTitlePattern, "$1");
      currentParagraph.setHeading(DocumentApp.ParagraphHeading.HEADING1);
      currentParagraph.setText(titleText);
    } else if (subsectionPattern.test(paragraphContent)) {
      var subsectionText = paragraphContent.replace(subsectionPattern, "$1");
      currentParagraph.setHeading(DocumentApp.ParagraphHeading.HEADING2);
      currentParagraph.setText(subsectionText);
    }

    var boldMatchesArray = paragraphContent.matchAll(boldTextPattern);
    for (var boldMatch of boldMatchesArray) {
      var fullMatch = boldMatch[0];
      var boldContent = boldMatch[1];
      var updatedText = paragraphContent.replace(fullMatch, boldContent);
      currentParagraph.setText(updatedText);
      currentParagraph.setBold(true);
    }
  }
}

In this code, I need to modify the way I update the paragraph text so that I can selectively set specific words to bold while leaving the rest unaffected, considering there may be multiple bold terms in a single sentence. Can anyone provide insights on how to achieve this?

Hey there, you might want to try iterating over the paragraph’s elements instead of setting text directly on the paragraph. Check the elements within and apply bold styling specifically to the TextRange of matched bold text. This way other parts of the sentence remain untouched.