league/html-to-markdown
Composer 安装命令:
composer require league/html-to-markdown
包简介
An HTML-to-markdown conversion helper for PHP
README 文档
README
Library which converts HTML to Markdown for your sanity and convenience.
Requires: PHP 7.2+
Lead Developer: @colinodell
Original Author: @nickcernis
Why convert HTML to Markdown?
"What alchemy is this?" you mutter. "I can see why you'd convert Markdown to HTML," you continue, already labouring the question somewhat, "but why go the other way?"
Typically you would convert HTML to Markdown if:
- You have an existing HTML document that needs to be edited by people with good taste.
- You want to store new content in HTML format but edit it as Markdown.
- You want to convert HTML email to plain text email.
- You know a guy who's been converting HTML to Markdown for years, and now he can speak Elvish. You'd quite like to be able to speak Elvish.
- You just really like Markdown.
How to use it
Require the library by issuing this command:
composer require league/html-to-markdown
Add require 'vendor/autoload.php'; to the top of your script.
Next, create a new HtmlConverter instance, passing in your valid HTML code to its convert() function:
use League\HTMLToMarkdown\HtmlConverter; $converter = new HtmlConverter(); $html = "<h3>Quick, to the Batpoles!</h3>"; $markdown = $converter->convert($html);
The $markdown variable now contains the Markdown version of your HTML as a string:
echo $markdown; // ==> ### Quick, to the Batpoles!
The included demo directory contains an HTML->Markdown conversion form to try out.
Conversion options
Caution
By default, this library preserves HTML tags without Markdown equivalents, like <span>, <div>, <iframe>, <script>, etc. If you will be parsing untrusted input from users, please consider setting the strip_tags and/or remove_nodes options documented below, and also using a library (like HTML Purifier) to provide additional HTML filtering.
To strip HTML tags that don't have a Markdown equivalent while preserving the content inside them, set strip_tags to true, like this:
$converter = new HtmlConverter(array('strip_tags' => true)); $html = '<span>Turnips!</span>'; $markdown = $converter->convert($html); // $markdown now contains "Turnips!"
Or more explicitly, like this:
$converter = new HtmlConverter(); $converter->getConfig()->setOption('strip_tags', true); $html = '<span>Turnips!</span>'; $markdown = $converter->convert($html); // $markdown now contains "Turnips!"
Note that only the tags themselves are stripped, not the content they hold.
To strip tags and their content, pass a space-separated list of tags in remove_nodes, like this:
$converter = new HtmlConverter(array('remove_nodes' => 'span div')); $html = '<span>Turnips!</span><div>Monkeys!</div>'; $markdown = $converter->convert($html); // $markdown now contains ""
By default, all comments are stripped from the content. To preserve them, use the preserve_comments option, like this:
$converter = new HtmlConverter(array('preserve_comments' => true)); $html = '<span>Turnips!</span><!-- Monkeys! -->'; $markdown = $converter->convert($html); // $markdown now contains "Turnips!<!-- Monkeys! -->"
To preserve only specific comments, set preserve_comments with an array of strings, like this:
$converter = new HtmlConverter(array('preserve_comments' => array('Eggs!'))); $html = '<span>Turnips!</span><!-- Monkeys! --><!-- Eggs! -->'; $markdown = $converter->convert($html); // $markdown now contains "Turnips!<!-- Eggs! -->"
By default, placeholder links are preserved. To strip the placeholder links, use the strip_placeholder_links option, like this:
$converter = new HtmlConverter(array('strip_placeholder_links' => true)); $html = '<a>Github</a>'; $markdown = $converter->convert($html); // $markdown now contains "Github"
Style options
By default bold tags are converted using the asterisk syntax, and italic tags are converted using the underlined syntax. Change these by using the bold_style and italic_style options.
$converter = new HtmlConverter(); $converter->getConfig()->setOption('italic_style', '*'); $converter->getConfig()->setOption('bold_style', '__'); $html = '<em>Italic</em> and a <strong>bold</strong>'; $markdown = $converter->convert($html); // $markdown now contains "*Italic* and a __bold__"
Line break options
By default, br tags are converted to two spaces followed by a newline character as per traditional Markdown. Set hard_break to true to omit the two spaces, as per GitHub Flavored Markdown (GFM).
$converter = new HtmlConverter(); $html = '<p>test<br>line break</p>'; $converter->getConfig()->setOption('hard_break', true); $markdown = $converter->convert($html); // $markdown now contains "test\nline break" $converter->getConfig()->setOption('hard_break', false); // default $markdown = $converter->convert($html); // $markdown now contains "test \nline break"
Autolinking options
By default, a tags are converted to the easiest possible link syntax, i.e. if no text or title is available, then the <url> syntax will be used rather than the full [url](url) syntax. Set use_autolinks to false to change this behavior to always use the full link syntax.
$converter = new HtmlConverter(); $html = '<p><a href="https://thephpleague.com">https://thephpleague.com</a></p>'; $converter->getConfig()->setOption('use_autolinks', true); $markdown = $converter->convert($html); // $markdown now contains "<https://thephpleague.com>" $converter->getConfig()->setOption('use_autolinks', false); // default $markdown = $converter->convert($html); // $markdown now contains "[https://thephpleague.com](https://thephpleague.com)"
Passing custom Environment object
You can pass current Environment object to customize i.e. which converters should be used.
$environment = new Environment(array( // your configuration here )); $environment->addConverter(new HeaderConverter()); // optionally - add converter manually $converter = new HtmlConverter($environment); $html = '<h3>Header</h3> <img src="" /> '; $markdown = $converter->convert($html); // $markdown now contains "### Header" and "<img src="" />"
Table support
Support for Markdown tables is not enabled by default because it is not part of the original Markdown syntax. To use tables add the converter explicitly:
use League\HTMLToMarkdown\HtmlConverter; use League\HTMLToMarkdown\Converter\TableConverter; $converter = new HtmlConverter(); $converter->getEnvironment()->addConverter(new TableConverter()); $html = "<table><tr><th>A</th></tr><tr><td>a</td></tr></table>"; $markdown = $converter->convert($html);
Limitations
- Markdown Extra, MultiMarkdown and other variants aren't supported – just Markdown.
Style notes
-
Setext (underlined) headers are the default for H1 and H2. If you prefer the ATX style for H1 and H2 (# Header 1 and ## Header 2), set
header_styleto 'atx' in the options array when you instantiate the object:$converter = new HtmlConverter(array('header_style'=>'atx'));Headers of H3 priority and lower always use atx style.
-
Links and images are referenced inline. Footnote references (where image src and anchor href attributes are listed in the footnotes) are not used.
-
Blockquotes aren't line wrapped – it makes the converted Markdown easier to edit.
Dependencies
HTML To Markdown requires PHP's xml, lib-xml, and dom extensions, all of which are enabled by default on most distributions.
Errors such as "Fatal error: Class 'DOMDocument' not found" on distributions such as CentOS that disable PHP's xml extension can be resolved by installing php-xml.
Contributors
Many thanks to all contributors so far. Further improvements and feature suggestions are very welcome.
How it works
HTML To Markdown creates a DOMDocument from the supplied HTML, walks through the tree, and converts each node to a text node containing the equivalent markdown, starting from the most deeply nested node and working inwards towards the root node.
To-do
- Support for nested lists and lists inside blockquotes.
- Offer an option to preserve tags as HTML if they contain attributes that can't be represented with Markdown (e.g.
style).
Trying to convert Markdown to HTML?
Use one of these great libraries:
- league/commonmark (recommended)
- cebe/markdown
- PHP Markdown
- Parsedown
No guarantees about the Elvish, though.
league/html-to-markdown 适用场景与选型建议
league/html-to-markdown 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.76M 次下载、GitHub Stars 达 1.89k, 最近一次更新时间为 2015 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「markdown」 「html」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 league/html-to-markdown 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 league/html-to-markdown 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 league/html-to-markdown 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Adds more BBCode
HTML and form generation
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
Reusable utilities library for Lacus Solutions' packages (type description, HTML escaping, random sequences)
Creates a markdown changelog for your GitHub repository.
统计信息
- 总下载量: 31.76M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1902
- 点击次数: 15
- 依赖项目数: 205
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-14