def cu_fields_dict_to_markdown()

in function_app/src/helpers/content_understanding.py [0:0]


def cu_fields_dict_to_markdown(fields_dict: dict) -> str:
    """
    Converts a dictionary of Content Understanding fields to a
    markdown-formatted string.

    :param cu_fields_dict: Dictionary of Content Understanding fields.
    :type cu_fields_dict: dict
    :return: Markdown-formatted string.
    :rtype: str
    """
    markdown_lines = []
    for field_name, field_info in fields_dict.items():
        if field_info.get("type") == "object":
            markdown_lines.append(
                f"{apply_md_text_formatting(field_name, True)}: {json.dumps(simplify_cu_field_dict(field_info), indent=4)}"
            )
        elif field_info.get("type") == "array":
            if field_info.get("valueArray", []) and field_info["valueArray"][0][
                "type"
            ] not in [
                "object",
                "array",
            ]:
                value_str = [
                    format_simple_cu_field_output(item, True)
                    for item in field_info.get("valueArray", [])
                ]
                markdown_lines.append(
                    f"{apply_md_text_formatting(field_name, True)}: {value_str}"
                )
            else:
                markdown_lines.append(
                    f"{apply_md_text_formatting(field_name, True)}: {json.dumps(simplify_cu_field_dict(field_info), indent=4)}"
                )
        else:
            formatted_output = format_simple_cu_field_output(field_info, True)
            if formatted_output.startswith("'|") and formatted_output.endswith("|'"):
                # Output is a table - add a newline before and after
                formatted_output = f"\n{formatted_output[1:-1]}\n"
            markdown_lines.append(
                f"{apply_md_text_formatting(field_name, True)}: {formatted_output}"
            )
    return "\n".join(markdown_lines)