ineersa/html2markdown 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ineersa/html2markdown

Composer 安装命令:

composer require ineersa/html2markdown

包简介

`html2markdown` converts a page of HTML into clean, easy-to-read plain ASCII text. Better yet, that ASCII also happens to be valid Markdown (a text-to-HTML format).

README 文档

README

CI codecov

html2text converts a page of HTML into clean, easy-to-read plain ASCII text. Better yet, that ASCII also happens to be valid Markdown (a text-to-HTML format).

It is a PHP port of Alir3z4/html2text with few fixes and updates.

Functionality parity is checked via the test suite, which contains all the test cases from the original and more. Most of the code was translated with AI with a lot of refactoring and fixes.

How to install/requirements

Project is using new DOM extension for better HTML parser and requires ext-libxml. PHP version required - 8.4+

To install run composer command:

composer require ineersa/html2text

Usage

Basic usage:

$html = (string) file_get_contents($source);

$config = new Ineersa\Html2text\Config();
$html2Markdown = new Ineersa\Html2text\HTML2Markdown($config);
$markdown = $html2Markdown($html);

Config options are compatible with Python library:

final readonly class Config
{
    public function __construct(
        /** Use Unicode characters instead of ASCII fallbacks. */
        public bool $unicodeSnob = false,
        /** Escape all special characters even if output is less readable. */
        public bool $escapeSnob = false,
        /** Append footnote links immediately after each paragraph. */
        public bool $linksEachParagraph = false,
        /** Wrap long lines at the configured column (0 disables wrapping). */
        public int $bodyWidth = 78,
        /** Skip internal anchors like href="#local-anchor". */
        public bool $skipInternalLinks = true,
        /** Render links using inline Markdown syntax. */
        public bool $inlineLinks = true,
        /** Surround links with angle brackets to prevent wraps. */
        public bool $protectLinks = false,
        /** Allow links to wrap across lines. */
        public bool $wrapLinks = true,
        /** Wrap list items at the configured body width. */
        public bool $wrapListItems = false,
        /** Wrap table output text. */
        public bool $wrapTables = false,
        /** Is Google Doc */
        public bool $googleDoc = false,
        /** Callback to apply at tag processing, $callback($this, $tag, $attrs, $start), should return true to break processing, false otherwise */
        public ?\Closure $tagCallback = null,
        /** Pixels Google uses to indent nested lists. */
        public int $googleListIndent = 36,
        /**
         * Values that indicate bold text in inline styles.
         *
         * @var string[]
         */
        public array $boldTextStyleValues = ['bold', '700', '800', '900'],
        /** Ignore anchor tags entirely. */
        public bool $ignoreAnchors = false,
        /** Ignore mailto links during conversion. */
        public bool $ignoreMailtoLinks = false,
        /** Drop all image tags from the output. */
        public bool $ignoreImages = false,
        /** Keep image tags rendered as raw HTML. */
        public bool $imagesAsHtml = false,
        /** Replace images with their alt text. */
        public bool $imagesToAlt = false,
        /** Include width/height attributes when preserving images. */
        public bool $imagesWithSize = false,
        /** Ignore text emphasis such as italics and bold. */
        public bool $ignoreEmphasis = false,
        /** Wrap inline code with custom markers. */
        public bool $markCode = false,
        /** Use backquotes instead of indentation for code blocks. */
        public bool $backquoteCodeStyle = false,
        /** Fallback alt text when an image omits it. */
        public string $defaultImageAlt = '',
        /** Pad tables to align cell widths. */
        public bool $padTables = false,
        /** Convert absolute links with identical href/text to <href> style. */
        public bool $useAutomaticLinks = true,
        /** Render tables as HTML instead of Markdown. */
        public bool $bypassTables = false,
        /** Ignore table tags but retain row content. */
        public bool $ignoreTables = false,
        /** Emit a single line break after block elements (requires width 0). */
        public bool $singleLineBreak = false,
        /** Use as the opening quotation mark for <q> tags. */
        public string $openQuote = '"',
        /** Use as the closing quotation mark for <q> tags. */
        public string $closeQuote = '"',
        /** Include <sup> and <sub> tags in the output. */
        public bool $includeSupSub = false,
        /** baseUrl to join with URLs if needed */
        public string $baseUrl = '',
        /** Number of list nesting levels skipped when applying visual indentation. */
        public int $listIndentBaseLevel = 0,
        /** Add indentation before definition list descriptions (<dd>). */
        public bool $indentDefinitionDescriptions = true,
        /** Add a blank line after closing a definition description (<dd>). */
        public bool $blankLineAfterDefinitionDescription = false,
        /** Append an extra newline after closing a top-level list. */
        public bool $appendFinalListNewline = true,
        /** Append one extra raw newline after a top-level list closes. */
        public bool $appendRawNewlineAfterTopLevelList = false,
        /** Emphasis marks */
        public string $ulItemMark = '*',
        public string $emphasisMark = '_',
        public string $strongMark = '**',
        /** hide strikethrough emphasis */
        public bool $hideStrikethrough = false,
    ) {
    }
}

New parity-oriented options added for finer output control:

  • listIndentBaseLevel: removes indentation from the first N list levels (useful when aligning with other converters that keep top-level lists flush-left).
  • indentDefinitionDescriptions: toggles the prefix for <dd> entries.
  • blankLineAfterDefinitionDescription: inserts a paragraph break after each </dd>.
  • appendFinalListNewline: appends a newline when closing the outermost list.
  • appendRawNewlineAfterTopLevelList: appends one more raw newline after a top-level list close.

Rust parity

This project can be validated against the Rust implementation using imported HTML->Markdown fixture pairs.

  • PHP Fixtures Suite checks the PHP port's original fixture set.
  • Rust Fixtures Suite checks parity fixtures imported from the Rust repository.
  • You can run only parity tests with: vendor/bin/phpunit --testsuite "Rust Fixtures Suite".
Rust parity configuration example
<?php

use Ineersa\Html2text\Config;
use Ineersa\Html2text\HTML2Markdown;

$config = new Config(
    baseUrl: '',
    bodyWidth: 0,
    skipInternalLinks: false,
    ulItemMark: '-',
    emphasisMark: '*',
    listIndentBaseLevel: 1,
    indentDefinitionDescriptions: false,
    blankLineAfterDefinitionDescription: true,
    appendFinalListNewline: false,
    appendRawNewlineAfterTopLevelList: true,
);

$converter = new HTML2Markdown($config);
Suggested LLM-oriented configuration

For LLM ingestion, a practical default is to minimize reflow noise, keep inline context, and preserve structural cues:

<?php

use Ineersa\Html2text\Config;
use Ineersa\Html2text\HTML2Markdown;

$config = new Config(
    bodyWidth: 0,
    unicodeSnob: true,
    inlineLinks: true,
    skipInternalLinks: true,
    wrapLinks: false,
    wrapListItems: false,
    wrapTables: false,
    padTables: false,
    useAutomaticLinks: true,
    backquoteCodeStyle: true,
    imagesToAlt: true,
    ulItemMark: '-',
    emphasisMark: '*',
);

$converter = new HTML2Markdown($config);

Why this profile works well for LLM workflows:

  • bodyWidth: 0 avoids hard-wrapped lines that can split sentence context.
  • inlineLinks: true keeps reference targets close to anchor text.
  • skipInternalLinks: true reduces table-of-contents and in-page anchor noise.
  • backquoteCodeStyle: true keeps code blocks explicit and model-friendly.
  • imagesToAlt: true preserves useful image semantics without raw HTML clutter.

Development

You can find information about the repository in AGENTS.md

Composer has scripts section with commands to run all required tools:

"scripts": {
    "cs-fix": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php",
    "phpstan": "vendor/bin/phpstan analyse -c phpstan.dist.neon",
    "tests": "vendor/bin/phpunit --colors=always --testdox",
    "coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always --testdox --coverage-text --coverage-html coverage/ --coverage-clover coverage/clover.xml",
    "tests-xdebug": "php -d xdebug.mode=debug -d xdebug.client_host=127.0.0.1 -d xdebug.client_port=9003 -d xdebug.start_with_request=yes vendor/bin/phpunit --colors=always --testdox"
  }

License

This project is licensed under the GNU General Public License v3.0 or later.

It is a PHP port of Alir3z4/html2text,
which is licensed under the GPL as well.
All credit goes to the original authors for their work.

ineersa/html2markdown 适用场景与选型建议

ineersa/html2markdown 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ineersa/html2markdown 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ineersa/html2markdown 我们能提供哪些服务?
定制开发 / 二次开发

基于 ineersa/html2markdown 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 39
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2026-03-12