ATX and Setext heading syntax is standardised independently from a host's generated URL fragments.
Code fences must be recognised before heading-looking example lines are scanned.
Explicit ids remove slug guesswork but still depend on a renderer that does not sanitise authored anchors away.
The full explanation
A Markdown heading is portable structure; its automatic URL fragment is not. Hosts make different choices about punctuation, Unicode, duplicate suffixes and raw HTML. A table of contents becomes more predictable when its target ids are explicit and its assumptions are visible in the document. Read the complete guide for the reasoning, examples and definitions behind this answer.