import re


def strip_text(text: str) -> str:
    """
    Filters text between delimiters from markdown file

    Parameters
    ----------
    text : str
        Text of markdown file to filter

    Returns
    -------
    str
        Filtered markdown text
    """
    # First remove text from Python cells (with `# <COGINST>` delimiters)
    # and replace with a STUDENT CODE HERE comment
    stu_notebook = re.sub(
        r"# <COGINST>(.*?)</COGINST>", "# STUDENT CODE HERE", text, flags=re.S
    )

    # Remove text from markdown cells (with `<COGINST>` delimiters)
    # and replace with italicized `SOLUTION HERE`
    stu_notebook = re.sub(
        r"<COGINST>(.*?)</COGINST>", "*SOLUTION HERE*", stu_notebook, flags=re.S
    )

    # Remove instructor-only notes from markdown cells (with `<COGNOTE>` delimiters)
    stu_notebook = re.sub(r"<COGNOTE>(.*?)</COGNOTE>", "", stu_notebook, flags=re.S)

    # Remove single lines from code (with `<COGLINE>` addendum), preserving whitespace
    # and replace with a STUDENT CODE HERE comment
    stu_notebook = re.sub(r"\S(?<!\s)(.*?)<COGLINE>", "# STUDENT CODE HERE", stu_notebook)

    # Replace expression to the right of a `=` and left of `<COGSTUB>` with a comment.
    # z = 1 # <COGSTUB> compute `z`
    # z = # compute `z`
    stu_notebook = re.sub(r"=.\S(?<!\s)(.*?)<COGSTUB>", "= #", stu_notebook)

    return stu_notebook