def convert_text()

in migration/src/jira_util.py [0:0]


def convert_text(text: str, att_replace_map: dict[str, str] = {}, account_map: dict[str, str] = {}, jira_users: dict[str, str] = {}, att_dir: Optional[Path] = None) -> str:
    """Convert Jira markup to Markdown
    """
    def repl_att(m: re.Match):
        res = m.group(0)
        for src, repl in att_replace_map.items():
            if m.group(2) == src:
                res = f"[{m.group(1)}]({repl})"
        return res

    def escape_gh_issue_link(m: re.Match):
        # escape #NN by backticks to prevent creating an unintentional issue link
        res = f"{m.group(1)}`{m.group(2)}`{m.group(3)}"
        return res

    text = re.sub(REGEX_CRLF, "\n", text)  # jira2markup does not support carriage return (?)

    # convert Jira special emojis into corresponding or similar Unicode characters
    for emoji, unicode in JIRA_EMOJI_TO_UNICODE.items():
        text = re.sub(emoji, unicode, text)

    # convert @ mentions
    mentions = re.findall(REGEX_MENTION_ATMARK, text)
    if mentions:
        mentions = set(filter(lambda x: x != '', itertools.chain.from_iterable(mentions)))
        for m in mentions:
            jira_id = m[1:]
            disp_name = jira_users.get(jira_id)
            gh_m = account_map.get(jira_id)
            # replace Jira name with GitHub account or Jira display name if it is available, othewise show Jira name with `` to avoid unintentional mentions
            mention = lambda: f"@{gh_m}" if gh_m else disp_name if disp_name else f"`@{jira_id}`"
            text = text.replace(m, mention())
    
    # convert ~ mentions
    mentions = re.findall(REGEX_MENION_TILDE, text)
    if mentions:
        mentions = set(filter(lambda x: x != '', itertools.chain.from_iterable(mentions)))
        for m in mentions:
            jira_id = m[2:-1]
            disp_name = jira_users.get(jira_id)
            gh_m = account_map.get(jira_id)
            # replace Jira name with GitHub account or Jira display name if it is available, othewise show Jira name with ``
            mention = lambda: f"@{gh_m}" if gh_m else disp_name if disp_name else f"`@{jira_id}`"
            text = text.replace(m, mention())
    
    # escape tilde to avoid unintentional strike-throughs in Markdown
    text = text.replace("~", "\~")

    # convert Jira markup into Markdown with customization
    elements = MarkupElements()
    elements.replace(Code, TweakedCode)
    elements.replace(UnorderedList, UnorderedTweakedList)
    elements.replace(OrderedList, OrderedTweakedList)
    elements.replace(BlockQuote, TweakedBlockQuote)
    elements.replace(Quote, TweakedQuote)
    elements.replace(Monospaced, TweakedMonospaced)
    elements.insert_after(Ruler, LongRuler)
    elements.append(EscapeHtmlTag)
    elements.append(EscapeQuoteMD)
    text = jira2markdown.convert(text, elements=elements)

    # convert links to attachments
    text = re.sub(REGEX_LINK, repl_att, text)

    # escape github style cross-issue link (#NN)
    text = re.sub(REGEX_GITHUB_ISSUE_LINK, escape_gh_issue_link, text)

    # embed attachments (patches, etc.) if possible
    links = re.findall(REGEX_LINK, text)
    if links and att_dir:
        paths = []
        for link in links:
            try:
                path = att_dir.joinpath(link[0])
                if path.exists():
                    paths.append(path)
            except OSError:
                continue
        if paths:
            path = paths[0]
            # skip unknown file extensions; skip too large files.
            if path.suffix in FILE_EXT_TO_LANG and path.stat().st_size < 50000:
                text += __textdata_as_details(path, FILE_EXT_TO_LANG[path.suffix])

    return text