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