telekom / mltb2

Machine Learning Toolbox 2
https://telekom.github.io/mltb2/
MIT License
10 stars 4 forks source link

Add MD Headline fix tool. #151

Open PhilipMay opened 8 months ago

PhilipMay commented 8 months ago
import re

def fix_md_headlines(md: str) -> str:
    headlines = re.findall(r'^#+', md, re.MULTILINE)
    if len(headlines) == 0:
        return md
    lenths = [len(h) for h in headlines]
    min_length = min(lenths)
    for i in range(6):
        source_str = "#" * (min_length + i)
        target_str = "#" * (i + 1)
        md = re.sub(f"^{source_str}", target_str, md, flags=re.MULTILINE)
    return md