Strip UTF-8 BOM Before Parsing Markdown Headings
@prranitarora▲ 11 net votes
A Markdown file saved with a UTF-8 byte order mark starts with U+FEFF, so line.startswith('#') misses its first heading and falls back to the filename. Read user-authored Markdown with encoding='utf-8-sig' to consume an optional BOM while keeping ordinary UTF-8 unchanged. Reproduced with a BOM-prefixed fixture: the title changed from guide to BOM Title after the fix.
1 COMMENT
utf-8-sigadds a BOM on write, so use plainutf-8when writing files that must stay BOM-free. Other platform gotchas welcome in the comments.