really fix html2markdown() - when using environment, we must set the defaults

This commit is contained in:
Mario Vavti 2018-10-30 22:16:37 +01:00
parent 0b371c8103
commit c622ba84b9

View File

@ -288,10 +288,20 @@ function bb_to_markdown($Text, $options = []) {
function html2markdown($html,$options = []) { function html2markdown($html,$options = []) {
$markdown = ''; $markdown = '';
$internal_errors = libxml_use_internal_errors(true); if(! $options) {
$options = [
'header_style' => 'setext', // Set to 'atx' to output H1 and H2 headers as # Header1 and ## Header2
'suppress_errors' => true, // Set to false to show warnings when loading malformed HTML
'strip_tags' => false, // Set to true to strip tags that don't have markdown equivalents. N.B. Strips tags, not their content. Useful to clean MS Word HTML output.
'bold_style' => '**', // DEPRECATED: Set to '__' if you prefer the underlined style
'italic_style' => '*', // DEPRECATED: Set to '_' if you prefer the underlined style
'remove_nodes' => '', // space-separated list of dom nodes that should be removed. example: 'meta style script'
'hard_break' => false, // Set to true to turn <br> into `\n` instead of ` \n`
'list_item_style' => '-', // Set the default character for each <li> in a <ul>. Can be '-', '*', or '+'
];
}
$environment = new Environment($options); $environment = Environment::createDefaultEnvironment($options);
$environment->createDefaultEnvironment();
$environment->addConverter(new TableConverter()); $environment->addConverter(new TableConverter());
$converter = new HtmlConverter($environment); $converter = new HtmlConverter($environment);
@ -301,8 +311,6 @@ function html2markdown($html,$options = []) {
logger("Invalid HTML. HTMLToMarkdown library threw an exception."); logger("Invalid HTML. HTMLToMarkdown library threw an exception.");
} }
libxml_use_internal_errors($internal_errors);
return $markdown; return $markdown;
} }