import re


def remove_old_yaml(file: str) -> bool:
    """检测文件是否有yaml header (用 --- 分隔), 如果存在的话就删除;
    return: 是否曾经有旧yaml
    """
    has_yaml = False
    f = open(file, 'r+', encoding="utf-8")
    lines = f.readlines()
    f.seek(0)

    yaml_pattern = '---\s*$'

    if re.match(yaml_pattern, lines[0]):  # has old yaml, remove it;
        print("removing old yaml header...")
        has_yaml = True
        yaml_ends = False
        for line in lines[1:]:  # delete lines between first two "---" marks
            if (yaml_ends):
                f.write(line)
            elif re.match(yaml_pattern, line):
                yaml_ends = True
        f.truncate()

    f.close()
    return has_yaml