def to_md(content): is_inside_code_block = False output_spans = [] for i in range(len(content)): if content[i]=="\n" and not is_inside_code_block: if len(output_spans)>0 and output_spans[-1].endswith("```"): output_spans.append("\n") else: output_spans.append("
") elif content[i]=="`": output_spans.append(content[i]) if len(output_spans)>=3 and all([output_spans[j]=="`" for j in [-3,-2,-1]]): is_inside_code_block = not is_inside_code_block output_spans = output_spans[:-3] if is_inside_code_block: if len(output_spans)==0: output_spans.append("```") elif output_spans[-1]=="
": output_spans[-1] = "\n" output_spans.append("```") elif output_spans[-1].endswith("\n"): output_spans.append("```") else: output_spans.append("\n```") if i+1