]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/Markdown/HtmlToMarkdown.php
Merge branch 'create-content-meta-tags' of https://github.com/james-geiger/BookStack...
[bookstack] / app / Entities / Tools / Markdown / HtmlToMarkdown.php
1 <?php namespace BookStack\Entities\Tools\Markdown;
2
3 use League\HTMLToMarkdown\Converter\BlockquoteConverter;
4 use League\HTMLToMarkdown\Converter\CodeConverter;
5 use League\HTMLToMarkdown\Converter\CommentConverter;
6 use League\HTMLToMarkdown\Converter\DivConverter;
7 use League\HTMLToMarkdown\Converter\EmphasisConverter;
8 use League\HTMLToMarkdown\Converter\HardBreakConverter;
9 use League\HTMLToMarkdown\Converter\HeaderConverter;
10 use League\HTMLToMarkdown\Converter\HorizontalRuleConverter;
11 use League\HTMLToMarkdown\Converter\ImageConverter;
12 use League\HTMLToMarkdown\Converter\LinkConverter;
13 use League\HTMLToMarkdown\Converter\ListBlockConverter;
14 use League\HTMLToMarkdown\Converter\ListItemConverter;
15 use League\HTMLToMarkdown\Converter\PreformattedConverter;
16 use League\HTMLToMarkdown\Converter\TextConverter;
17 use League\HTMLToMarkdown\Environment;
18 use League\HTMLToMarkdown\HtmlConverter;
19
20 class HtmlToMarkdown
21 {
22     protected $html;
23
24     public function __construct(string $html)
25     {
26         $this->html = $html;
27     }
28
29     /**
30      * Run the conversion
31      */
32     public function convert(): string
33     {
34         $converter = new HtmlConverter($this->getConverterEnvironment());
35         $html = $this->prepareHtml($this->html);
36         return $converter->convert($html);
37     }
38
39     /**
40      * Run any pre-processing to the HTML to clean it up manually before conversion.
41      */
42     protected function prepareHtml(string $html): string
43     {
44         // Carriage returns can cause whitespace issues in output
45         $html = str_replace("\r\n", "\n", $html);
46         // Attributes on the pre tag can cause issues with conversion
47         return preg_replace('/<pre .*?>/', '<pre>', $html);
48     }
49
50     /**
51      * Get the HTML to Markdown customized environment.
52      * Extends the default provided environment with some BookStack specific tweaks.
53      */
54     protected function getConverterEnvironment(): Environment
55     {
56         $environment = new Environment([
57             'header_style' => 'atx', // Set to 'atx' to output H1 and H2 headers as # Header1 and ## Header2
58             'suppress_errors' => true, // Set to false to show warnings when loading malformed HTML
59             '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.
60             'strip_placeholder_links' => false, // Set to true to remove <a> that doesn't have href.
61             'bold_style' => '**', // DEPRECATED: Set to '__' if you prefer the underlined style
62             'italic_style' => '*', // DEPRECATED: Set to '_' if you prefer the underlined style
63             'remove_nodes' => '', // space-separated list of dom nodes that should be removed. example: 'meta style script'
64             'hard_break' => false, // Set to true to turn <br> into `\n` instead of `  \n`
65             'list_item_style' => '-', // Set the default character for each <li> in a <ul>. Can be '-', '*', or '+'
66             'preserve_comments' => false, // Set to true to preserve comments, or set to an array of strings to preserve specific comments
67             'use_autolinks' => false, // Set to true to use simple link syntax if possible. Will always use []() if set to false
68             'table_pipe_escape' => '\|', // Replacement string for pipe characters inside markdown table cells
69             'table_caption_side' => 'top', // Set to 'top' or 'bottom' to show <caption> content before or after table, null to suppress
70         ]);
71
72         $environment->addConverter(new BlockquoteConverter());
73         $environment->addConverter(new CodeConverter());
74         $environment->addConverter(new CommentConverter());
75         $environment->addConverter(new DivConverter());
76         $environment->addConverter(new EmphasisConverter());
77         $environment->addConverter(new HardBreakConverter());
78         $environment->addConverter(new HeaderConverter());
79         $environment->addConverter(new HorizontalRuleConverter());
80         $environment->addConverter(new ImageConverter());
81         $environment->addConverter(new LinkConverter());
82         $environment->addConverter(new ListBlockConverter());
83         $environment->addConverter(new ListItemConverter());
84         $environment->addConverter(new CustomParagraphConverter());
85         $environment->addConverter(new PreformattedConverter());
86         $environment->addConverter(new TextConverter());
87
88         return $environment;
89     }
90 }