import re


def clean_newline(line):
    """Cleans string so formatting does not cross lines when joined with \\n.

    Just looks for unpaired '`' characters, other formatting characters do not
    seem to be joined across newlines.

    For reference, discord uses:
    https://github.com/Khan/simple-markdown/blob/master/simple-markdown.js
    """
    match = None
    for match1 in re.finditer(r'(`+)\s*([\s\S]*?[^`])\s*\1(?!`)', line):
        match = match1

    idx = match.end() if match else 0

    line = line[:idx] + line[idx:].replace('`', '\`')

    return line