Refactor my C++ custom wxWidgets code into separate methods and make it more clearer and improve it:

void wxMarkdownTextCtrl::WriteText(const wxString& text)
{
    wxString formattedText = text;

    formattedText.Replace("<br>", "\n");
    formattedText.Replace("<strong>", "**");
    formattedText.Replace("</strong>", "**");
    formattedText.Replace("<em>", "*");
    formattedText.Replace("</em>", "*");
    formattedText.Replace("\n- ", wxString::FromUTF8("\n● "));
    formattedText.Replace("\n* ", wxString::FromUTF8("\n● "));

    wxString::size_type pos = 0;
    while (pos < formattedText.Length() && pos != wxString::npos)
    {
        wxString::size_type startBoldItalic = formattedText.find("***", pos);
        wxString::size_type startBold = formattedText.find("**", pos);
        wxString::size_type startItalic = formattedText.find("*", pos);
        wxString::size_type startUnderline = formattedText.find("__", pos);
        wxString::size_type startLinkText = formattedText.find("[", pos);
        wxString::size_type startCode = formattedText.find("`", pos);

        wxString::size_type startHeading1 = formattedText.find("\n# ", pos);
        wxString::size_type startHeading2 = formattedText.find("\n## ", pos);
        wxString::size_type startHeading3 = formattedText.find("\n### ", pos);
        wxString::size_type startHeading4 = formattedText.find("\n#### ", pos);
        wxString::size_type startHeading5 = formattedText.find("\n##### ", pos);
        wxString::size_type startHeading6 = formattedText.find("\n###### ", pos);
        wxString::size_type startBlockquote = formattedText.find("\n> ", pos);

        if (pos == 0)
        {
            startHeading1 = formattedText.find("# ", pos);
            startHeading2 = formattedText.find("## ", pos);
            startHeading3 = formattedText.find("### ", pos);
            startHeading4 = formattedText.find("#### ", pos);
            startHeading5 = formattedText.find("##### ", pos);
            startHeading6 = formattedText.find("###### ", pos);
            startBlockquote = formattedText.find("> ", pos);
        }

        std::vector<std::pair<wxString::size_type, int>> markers = {
            { startBoldItalic, 3 },
            { startBold, 2 },
            { startItalic, 1 },
            { startUnderline, 2 },
            { startCode, 1 },
            { startHeading6, 7 },
            { startHeading5, 6 },
            { startHeading4, 5 },
            { startHeading3, 4 },
            { startHeading2, 3 },
            { startHeading1, 2 },
            { startBlockquote, 2 },
            { startLinkText, -1 }
        };

        // Find the earliest marker
        auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
            return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
        });

        if (earliestIt->first != wxString::npos)
        {
            wxString::size_type markerPos = earliestIt->first;
            int markerLength = earliestIt->second;

            wxString::size_type endMarker;

            if (startBoldItalic == markerPos)
            {
                endMarker = formattedText.find("***", markerPos + markerLength);
            }
            else if (startBold == markerPos)
            {
                endMarker = formattedText.find("**", markerPos + markerLength);
            }
            else if (startUnderline == markerPos)
            {
                endMarker = formattedText.find("__", markerPos + markerLength);
            }
            else if (startItalic == markerPos)
            {
                endMarker = formattedText.find("*", markerPos + markerLength);
            }
            else if (startCode == markerPos)
            {
                endMarker = formattedText.find("`", markerPos + markerLength);
            }
            else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos || startBlockquote == markerPos)
            {
                endMarker = formattedText.find("\n", markerPos + markerLength);
            }
            else if (startLinkText == markerPos)
            {
                wxString::size_type endLinkText = formattedText.find("]", markerPos);
                wxString::size_type startLinkUrl = formattedText.find("(", endLinkText);
                wxString::size_type endLinkUrl = formattedText.find(")", startLinkUrl);

                if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
                {
                    wxString beforeMarker = formattedText.substr(pos, markerPos - pos);
                    wxString linkText = formattedText.substr(markerPos + 1, endLinkText - markerPos - 1);
                    wxString linkUrl = formattedText.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);

                    if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
                    {
                        wxRichTextCtrl::WriteText(beforeMarker);

                        wxRichTextAttr urlStyle;
                        urlStyle.SetTextColour(*wxBLUE);
                        urlStyle.SetFontUnderlined(true);

                        wxRichTextCtrl::BeginStyle(urlStyle);
                        wxRichTextCtrl::BeginURL(linkUrl);
                        wxRichTextCtrl::WriteText(linkText);
                        wxRichTextCtrl::EndURL();
                        wxRichTextCtrl::EndStyle();

                        pos = endLinkUrl + 1;
                        continue;
                    }
                    else
                    {
                        wxString invalidLinkText = formattedText.substr(markerPos, endLinkUrl - markerPos + 1);
                        wxRichTextCtrl::WriteText(beforeMarker + invalidLinkText);
                        pos = endLinkUrl + 1;
                        continue;
                    }
                }
            }

            if (endMarker != wxString::npos)
            {
                wxString beforeMarker = formattedText.substr(pos, markerPos - pos);
                wxRichTextCtrl::WriteText(beforeMarker);
                wxString styledText = formattedText.substr(markerPos + markerLength, endMarker - markerPos - markerLength);

                if (startBoldItalic == markerPos || startBold == markerPos || startUnderline == markerPos) // Bold and/or Underline
                {
                    if (startBoldItalic == markerPos)
                    {
                        wxRichTextCtrl::BeginBold();
                        wxRichTextCtrl::BeginUnderline();
                    }
                    else if (startBold == markerPos)
                    {
                        wxRichTextCtrl::BeginBold();
                    }
                    else if (startUnderline == markerPos)
                    {
                        wxRichTextCtrl::BeginUnderline();
                    }

                    wxRichTextCtrl::WriteText(styledText);

                    if (startBoldItalic == markerPos)
                    {
                        wxRichTextCtrl::EndBold();
                        wxRichTextCtrl::EndUnderline();
                    }
                    else if (startBold == markerPos)
                    {
                        wxRichTextCtrl::EndBold();
                    }
                    else if (startUnderline == markerPos)
                    {
                        wxRichTextCtrl::EndUnderline();
                    }
                }
                else if (markerLength == 1 && startItalic == markerPos) // Italic
                {
                    wxRichTextCtrl::BeginItalic();
                    wxRichTextCtrl::WriteText(styledText);
                    wxRichTextCtrl::EndItalic();
                }
                else if (markerLength == 1 && startCode == markerPos) // Inline code
                {
                    wxTextAttr style;
                    wxRichTextCtrl::GetStyle(pos, style);

                    wxTextAttr codeAttr;
                    codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
                    codeAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
                    codeAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));

                    wxRichTextCtrl::BeginStyle(codeAttr);
                    wxRichTextCtrl::WriteText(styledText);
                    wxRichTextCtrl::EndStyle();
                }
                else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
                {
                    wxTextAttr style;
                    wxRichTextCtrl::GetStyle(pos, style);

                    wxTextAttr blockquoteAttr;
                    blockquoteAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
                    blockquoteAttr.SetTextColour(wxColour(220, 220, 220));

                    wxRichTextCtrl::BeginStyle(blockquoteAttr);
                    wxRichTextCtrl::WriteText(styledText);
                    wxRichTextCtrl::EndStyle();

                    if (endMarker < formattedText.Length() && formattedText[endMarker] == '\n')
                    {
                        wxRichTextCtrl::WriteText("\n");
                    }
                }
                else if (markerLength >= 2 && markerLength <= 7 && (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
                {
                    wxFont font = wxRichTextCtrl::GetFont();
                    int pointSize = font.GetPointSize();

                    if (startHeading1 == markerPos)
                    {
                        pointSize *= 2;
                    }
                    else if (startHeading2 == markerPos)
                    {
                        pointSize *= 1.7;
                    }
                    else if (startHeading3 == markerPos)
                    {
                        pointSize *= 1.5;
                    }
                    else if (startHeading4 == markerPos)
                    {
                        pointSize *= 1.3;
                    }
                    else if (startHeading5 == markerPos)
                    {
                        pointSize *= 1.2;
                    }
                    else if (startHeading6 == markerPos)
                    {
                        pointSize *= 1.1;
                    }

                    wxRichTextCtrl::WriteText("\n");
                    wxRichTextCtrl::BeginFontSize(pointSize);
                    wxRichTextCtrl::WriteText(styledText);
                    wxRichTextCtrl::EndFontSize();
                    wxRichTextCtrl::WriteText("\n");
                }

                if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)
                {
                    pos = endMarker + 1;
                }
                else
                {
                    pos = endMarker + markerLength;
                }
            }
            else
            {
                wxRichTextCtrl::WriteText(formattedText.substr(pos));
                pos = wxString::npos;
            }
        }
        else
        {
            wxRichTextCtrl::WriteText(formattedText.substr(pos));
            pos = wxString::npos;
        }
    }
}