在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):thephpleague/html-to-markdown开源软件地址(OpenSource Url):https://github.com/thephpleague/html-to-markdown开源编程语言(OpenSource Language):PHP 99.9%开源软件介绍(OpenSource Introduction):HTML To Markdown for PHPLibrary 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:
How to use itRequire the library by issuing this command: composer require league/html-to-markdown Add Next, create a new HtmlConverter instance, passing in your valid HTML code to its use League\HTMLToMarkdown\HtmlConverter;
$converter = new HtmlConverter();
$html = "<h3>Quick, to the Batpoles!</h3>";
$markdown = $converter->convert($html); The echo $markdown; // ==> ### Quick, to the Batpoles! The included Conversion optionsBy default, HTML To Markdown preserves HTML tags without Markdown equivalents, like To strip HTML tags that don't have a Markdown equivalent while preserving the content inside them, set $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 $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 $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 $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 $converter = new HtmlConverter(array('strip_placeholder_links' => true));
$html = '<a>Github</a>';
$markdown = $converter->convert($html); // $markdown now contains "Github" Style optionsBy default bold tags are converted using the asterisk syntax, and italic tags are converted using the underlined syntax. Change these by using the $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 optionsBy default, $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 optionsBy default, $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://google.com](https://google.com)" Passing custom Environment objectYou can pass current $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 supportSupport 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
Style notes
DependenciesHTML 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. ContributorsMany thanks to all contributors so far. Further improvements and feature suggestions are very welcome. How it worksHTML 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
Trying to convert Markdown to HTML?Use one of these great libraries:
No guarantees about the Elvish, though. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论