All contributions

thephpleague/html-to-markdown

HTML To Markdown

A PHP library from The PHP League that converts HTML to Markdown, turning lists, links, code blocks and other elements into their Markdown equivalents.

Language
PHP
GitHub stars
1,908
Merged changes
3

What we changed

Ordered lists with start="0" begin at zero, links with an & in the address become autolinks, and a <pre> block no longer gets an empty first line in the fenced code block.

What this work shows

  • The HTML spec and the libxml parser
  • Markdown syntax: autolinks and code fences
  • HTML entities and URL encoding
  • PHPUnit tests
What was broken

A list starting at zero began at one

<ol start="0"> is valid HTML and CommonMark supports lists starting at zero, but the converter numbered such a list from 1. The check built on intval() treated 0 the same as a missing attribute, because in PHP both are falsy.

How we fixed it

is_numeric instead of a truthiness check

ListItemConverter now checks the attribute with is_numeric(), which is false for an empty string and true for "0". A list from zero gives 0. and 1., and other values such as start="120" work as before.

#277

What was broken

A link with & did not become an autolink

With use_autolinks, the converter writes a link as <address> when its text matches the href. The text goes through htmlspecialchars, though, so & became &amp;, the comparison failed and the output was a plain link with &amp; in its text.

How we fixed it

Comparing after undoing &amp;

Before comparing, we undo only &amp; back to &, so https://example.com/?a=1&b=2 gives an autolink. Other encoding differences, such as %2F against /, still give a plain link, and a test also covers an address with &lt; and &gt; entities.

#281

What was broken

An empty line at the top of a code block

The HTML spec says a newline right after the <pre> start tag is dropped, but the libxml parser keeps it. The fenced code block in Markdown got an extra empty first line as a result.

How we fixed it

One leading newline dropped

PreformattedConverter strips one leading newline, CRLF included, before building the code block. It does so only there and not in getContent(), because a <pre> kept as raw HTML loses that newline when rendered. We also updated old testPreformat expectations that had pinned the bug.

#278

Merged changes 3

  1. #281 fix: use autolinks for URLs that contain an ampersand Merged 18 October 2026
  2. #278 fix: strip the newline right after the <pre> start tag Merged 18 October 2026
  3. #277 fix: honor start="0" on ordered lists Merged 7 September 2026

What it taught us

Conversion between formats breaks at the edges: zero treated as a missing value, the &amp; entity compared with a raw &, a newline the HTML spec says to drop. We check each such case against the format's rule and close it with a test.

More about this technology: PHP

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.