PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.4
Elementor Website Builder – more than just a page builder v4.2.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / markdown-render / html-to-markdown.php

html-to-markdown.php in Elementor Website Builder – more than just a page builder 4.2.4, at modules/markdown-render/html-to-markdown.php

654 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\MarkdownRender;
3
4 use WP_HTML_Processor;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 class Html_To_Markdown {
11
12 const SAFE_URL_SCHEMES = [ 'http', 'https', 'mailto', 'tel', 'ftp', 'ftps' ];
13
14 const SAFE_DATA_PREFIXES = [ 'data:image/png', 'data:image/jpeg', 'data:image/jpg', 'data:image/gif', 'data:image/webp', 'data:image/svg+xml' ];
15
16 const ESCAPABLE_MARKDOWN_CHARS = [ '\\', '`', '*', '_', '[', ']' ];
17
18 const VOID_TAGS = [ 'br', 'hr', 'img', 'input', 'meta', 'link', 'area', 'base', 'col', 'embed', 'param', 'source', 'track', 'wbr' ];
19
20 private $output = '';
21
22 private $inline_buffer = '';
23
24 private $list_stack = [];
25
26 private $blockquote_depth = 0;
27
28 private $in_pre = false;
29
30 private $pre_buffer = '';
31
32 private $pre_language = '';
33
34 private $in_code = false;
35
36 private $in_link = false;
37
38 private $link_href = '';
39
40 private $link_buffer = '';
41
42 private $in_table = false;
43
44 private $table_headers = [];
45
46 private $table_rows = [];
47
48 private $current_row = [];
49
50 private $current_cell = '';
51
52 private $in_table_head = false;
53
54 private $row_is_header = false;
55
56 private $heading_level = 0;
57
58 public static function convert( string $html ): string {
59 if ( '' === $html ) {
60 return '';
61 }
62
63 $instance = new self();
64
65 return $instance->run( $html );
66 }
67
68 private function run( string $html ): string {
69 $html = $this->normalize_html( $html );
70
71 $processor = WP_HTML_Processor::create_full_parser( $html );
72
73 if ( null === $processor ) {
74 return $this->fallback( $html );
75 }
76
77 try {
78 $this->walk( $processor );
79 } catch ( \Exception $e ) {
80 return $this->fallback( $html );
81 }
82
83 $this->flush_inline();
84
85 $output = $this->output;
86 $output = preg_replace( "/[ \t]+\n/", "\n", $output );
87 $output = preg_replace( "/\n{3,}/", "\n\n", $output );
88
89 return trim( $output );
90 }
91
92 private function fallback( string $html ): string {
93 $text = wp_strip_all_tags( $html );
94 $text = html_entity_decode( $text, ENT_QUOTES, 'UTF-8' );
95
96 return trim( preg_replace( "/\n{3,}/", "\n\n", $text ) );
97 }
98
99 private function normalize_html( string $html ): string {
100 $html = preg_replace( '/\r\n?/', "\n", $html );
101 $html = preg_replace( '#<script\b[^>]*>.*?</script>#is', '', $html );
102 $html = preg_replace( '#<style\b[^>]*>.*?</style>#is', '', $html );
103 $html = str_replace( "\xE2\x80\x8B", '', $html );
104
105 return $html;
106 }
107
108 private function walk( WP_HTML_Processor $p ): void {
109 while ( $p->next_token() ) {
110 $type = $p->get_token_type();
111
112 if ( '#text' === $type ) {
113 $this->handle_text( $p->get_modifiable_text() );
114 continue;
115 }
116
117 if ( '#tag' !== $type ) {
118 continue;
119 }
120
121 $tag = strtolower( (string) $p->get_tag() );
122 $is_closer = $p->is_tag_closer();
123 $is_void = in_array( $tag, self::VOID_TAGS, true );
124
125 if ( $is_closer ) {
126 $this->handle_close( $tag );
127 continue;
128 }
129
130 $this->handle_open( $tag, $p );
131
132 if ( $is_void ) {
133 $this->handle_close( $tag );
134 }
135 }
136 }
137
138 private function handle_text( string $text ): void {
139 if ( '' === $text ) {
140 return;
141 }
142
143 if ( $this->in_pre ) {
144 $this->pre_buffer .= $text;
145 return;
146 }
147
148 if ( $this->in_table ) {
149 $this->current_cell .= $this->escape_text( $text, false );
150 return;
151 }
152
153 $collapsed = preg_replace( '/[ \t\n]+/', ' ', $text );
154 $escaped = $this->escape_text( $collapsed, $this->in_code );
155
156 if ( $this->in_link ) {
157 $this->link_buffer .= $escaped;
158 return;
159 }
160
161 $this->inline_buffer .= $escaped;
162 }
163
164 private function handle_open( string $tag, WP_HTML_Processor $p ): void {
165 switch ( $tag ) {
166 case 'p':
167 case 'div':
168 case 'section':
169 case 'article':
170 case 'header':
171 case 'footer':
172 case 'main':
173 case 'aside':
174 case 'figure':
175 $this->flush_inline();
176 return;
177
178 case 'h1':
179 case 'h2':
180 case 'h3':
181 case 'h4':
182 case 'h5':
183 case 'h6':
184 $this->flush_inline();
185 $this->heading_level = (int) $tag[1];
186 $this->inline_buffer = str_repeat( '#', $this->heading_level ) . ' ';
187 return;
188
189 case 'br':
190 if ( $this->in_table ) {
191 $this->current_cell .= ' ';
192 return;
193 }
194 if ( $this->heading_level > 0 ) {
195 $this->inline_buffer .= ' ';
196 return;
197 }
198 $this->inline_buffer .= "\n";
199 return;
200
201 case 'hr':
202 $this->flush_inline();
203 $this->output .= "\n\n---\n\n";
204 return;
205
206 case 'strong':
207 case 'b':
208 $this->append_inline( '**' );
209 return;
210
211 case 'em':
212 case 'i':
213 $this->append_inline( '*' );
214 return;
215
216 case 'del':
217 case 's':
218 case 'strike':
219 $this->append_inline( '~~' );
220 return;
221
222 case 'code':
223 if ( $this->in_pre ) {
224 $lang = (string) $p->get_attribute( 'class' );
225 if ( preg_match( '/language-([A-Za-z0-9_+\-]+)/', $lang, $m ) ) {
226 $this->pre_language = $m[1];
227 }
228 return;
229 }
230 $this->in_code = true;
231 $this->append_inline( '`CODE_OPEN`' );
232 return;
233
234 case 'pre':
235 $this->flush_inline();
236 $this->in_pre = true;
237 $this->pre_buffer = '';
238 $this->pre_language = '';
239 return;
240
241 case 'a':
242 $href = (string) $p->get_attribute( 'href' );
243 $this->in_link = true;
244 $this->link_href = $href;
245 $this->link_buffer = '';
246 return;
247
248 case 'img':
249 $this->emit_image( $p );
250 return;
251
252 case 'ul':
253 case 'ol':
254 if ( ! empty( $this->list_stack ) && '' !== trim( $this->inline_buffer ) ) {
255 $this->emit_list_item();
256 } else {
257 $this->flush_inline();
258 }
259 $this->list_stack[] = [
260 'type' => $tag,
261 'index' => 1,
262 ];
263 return;
264
265 case 'li':
266 $this->flush_inline();
267 return;
268
269 case 'blockquote':
270 $this->flush_inline();
271 $this->blockquote_depth++;
272 return;
273
274 case 'table':
275 $this->flush_inline();
276 $this->in_table = true;
277 $this->table_headers = [];
278 $this->table_rows = [];
279 return;
280
281 case 'thead':
282 $this->in_table_head = true;
283 return;
284
285 case 'tr':
286 if ( ! $this->in_table ) {
287 return;
288 }
289 $this->current_row = [];
290 $this->row_is_header = $this->in_table_head;
291 return;
292
293 case 'th':
294 $this->row_is_header = true;
295 $this->current_cell = '';
296 return;
297
298 case 'td':
299 $this->current_cell = '';
300 return;
301 }
302 }
303
304 private function handle_close( string $tag ): void {
305 switch ( $tag ) {
306 case 'p':
307 case 'div':
308 case 'section':
309 case 'article':
310 case 'header':
311 case 'footer':
312 case 'main':
313 case 'aside':
314 case 'figure':
315 $this->flush_inline();
316 return;
317
318 case 'h1':
319 case 'h2':
320 case 'h3':
321 case 'h4':
322 case 'h5':
323 case 'h6':
324 $this->inline_buffer = preg_replace( '/[ \t]+/', ' ', $this->inline_buffer );
325 $this->flush_inline();
326 $this->heading_level = 0;
327 return;
328
329 case 'strong':
330 case 'b':
331 $this->append_inline( '**' );
332 return;
333
334 case 'em':
335 case 'i':
336 $this->append_inline( '*' );
337 return;
338
339 case 'del':
340 case 's':
341 case 'strike':
342 $this->append_inline( '~~' );
343 return;
344
345 case 'code':
346 if ( $this->in_pre ) {
347 return;
348 }
349 $this->append_inline( '`CODE_CLOSE`' );
350 $this->in_code = false;
351 return;
352
353 case 'pre':
354 $this->emit_pre();
355 $this->in_pre = false;
356 $this->pre_buffer = '';
357 $this->pre_language = '';
358 return;
359
360 case 'a':
361 $this->emit_link();
362 $this->in_link = false;
363 $this->link_href = '';
364 $this->link_buffer = '';
365 return;
366
367 case 'ul':
368 case 'ol':
369 $this->flush_inline();
370 array_pop( $this->list_stack );
371 if ( empty( $this->list_stack ) ) {
372 $this->output .= "\n\n";
373 }
374 return;
375
376 case 'li':
377 $this->emit_list_item();
378 return;
379
380 case 'blockquote':
381 $this->flush_inline();
382 $this->blockquote_depth = max( 0, $this->blockquote_depth - 1 );
383 if ( 0 === $this->blockquote_depth ) {
384 $this->output .= "\n";
385 }
386 return;
387
388 case 'table':
389 $this->emit_table();
390 $this->in_table = false;
391 $this->table_headers = [];
392 $this->table_rows = [];
393 return;
394
395 case 'thead':
396 $this->in_table_head = false;
397 return;
398
399 case 'tr':
400 if ( ! $this->in_table ) {
401 return;
402 }
403 if ( $this->row_is_header && empty( $this->table_headers ) ) {
404 $this->table_headers = $this->current_row;
405 } else {
406 $this->table_rows[] = $this->current_row;
407 }
408 $this->current_row = [];
409 $this->row_is_header = false;
410 return;
411
412 case 'th':
413 case 'td':
414 $this->current_row[] = trim( preg_replace( '/\s+/', ' ', $this->current_cell ) );
415 $this->current_cell = '';
416 return;
417 }
418 }
419
420 private function append_inline( string $marker ): void {
421 if ( $this->in_link ) {
422 $this->link_buffer .= $marker;
423 return;
424 }
425
426 $this->inline_buffer .= $marker;
427 }
428
429 private function emit_link(): void {
430 $text = $this->link_buffer;
431 $href = $this->link_href;
432
433 if ( '' === $href || '#' === $href ) {
434 $this->inline_buffer .= $text;
435 return;
436 }
437
438 if ( ! $this->is_safe_url( $href ) ) {
439 $this->inline_buffer .= $text;
440 return;
441 }
442
443 $href = $this->escape_url_for_markdown( $href );
444
445 $this->inline_buffer .= '[' . $text . '](' . $href . ')';
446 }
447
448 private function emit_image( WP_HTML_Processor $p ): void {
449 $src = (string) $p->get_attribute( 'src' );
450 $alt = (string) $p->get_attribute( 'alt' );
451
452 if ( '' === $src ) {
453 return;
454 }
455
456 if ( ! $this->is_safe_url( $src ) ) {
457 return;
458 }
459
460 $alt = $this->escape_text( $alt, false );
461 $src = $this->escape_url_for_markdown( $src );
462
463 $this->inline_buffer .= '![' . $alt . '](' . $src . ')';
464 }
465
466 private function emit_pre(): void {
467 $content = html_entity_decode( $this->pre_buffer, ENT_QUOTES, 'UTF-8' );
468 $content = rtrim( $content, "\n" );
469
470 $this->output .= "\n\n```" . $this->pre_language . "\n" . $content . "\n```\n\n";
471 }
472
473 private function emit_list_item(): void {
474 $content = trim( $this->inline_buffer );
475 $this->inline_buffer = '';
476
477 if ( '' === $content ) {
478 return;
479 }
480
481 $top = end( $this->list_stack );
482
483 if ( false === $top ) {
484 $this->output .= $content . "\n";
485 return;
486 }
487
488 $depth = count( $this->list_stack ) - 1;
489 $indent = str_repeat( ' ', $depth );
490
491 if ( 'ol' === $top['type'] ) {
492 $marker = $top['index'] . '. ';
493 $this->list_stack[ array_key_last( $this->list_stack ) ]['index']++;
494 } else {
495 $marker = '- ';
496 }
497
498 $lines = explode( "\n", $content );
499 $first = array_shift( $lines );
500 $rendered = $indent . $marker . $first;
501
502 foreach ( $lines as $line ) {
503 $rendered .= "\n" . $indent . ' ' . $line;
504 }
505
506 $this->output .= $this->prefix_blockquote( $rendered ) . "\n";
507 }
508
509 private function emit_table(): void {
510 if ( empty( $this->table_headers ) && empty( $this->table_rows ) ) {
511 return;
512 }
513
514 if ( empty( $this->table_headers ) && ! empty( $this->table_rows ) ) {
515 $this->table_headers = array_fill( 0, count( $this->table_rows[0] ), '' );
516 }
517
518 $column_count = count( $this->table_headers );
519 $lines = [];
520 $lines[] = '| ' . implode( ' | ', array_map( [ $this, 'escape_table_cell' ], $this->table_headers ) ) . ' |';
521 $lines[] = '|' . str_repeat( ' --- |', $column_count );
522
523 foreach ( $this->table_rows as $row ) {
524 $row = array_pad( $row, $column_count, '' );
525 $row = array_slice( $row, 0, $column_count );
526 $lines[] = '| ' . implode( ' | ', array_map( [ $this, 'escape_table_cell' ], $row ) ) . ' |';
527 }
528
529 $this->output .= "\n\n" . implode( "\n", $lines ) . "\n\n";
530 }
531
532 private function escape_table_cell( string $value ): string {
533 return str_replace( [ '|', "\n" ], [ '\\|', ' ' ], $value );
534 }
535
536 private function flush_inline(): void {
537 if ( '' === $this->inline_buffer ) {
538 return;
539 }
540
541 $content = $this->finalize_inline( $this->inline_buffer );
542 $this->inline_buffer = '';
543
544 $content = trim( $content );
545
546 if ( '' === $content ) {
547 return;
548 }
549
550 if ( ! empty( $this->list_stack ) ) {
551 $this->inline_buffer = $content;
552 return;
553 }
554
555 $content = $this->prefix_blockquote( $content );
556
557 $this->output .= "\n\n" . $content . "\n\n";
558 }
559
560 private function finalize_inline( string $buffer ): string {
561 return preg_replace_callback(
562 '/`CODE_OPEN`(.*?)`CODE_CLOSE`/s',
563 function ( $matches ) {
564 return $this->render_inline_code( $matches[1] );
565 },
566 $buffer
567 );
568 }
569
570 private function render_inline_code( string $content ): string {
571 $content = preg_replace( '/\s+/', ' ', $content );
572
573 preg_match_all( '/`+/', $content, $runs );
574 $max = 0;
575
576 foreach ( $runs[0] as $run ) {
577 $max = max( $max, strlen( $run ) );
578 }
579
580 $fence = str_repeat( '`', $max + 1 );
581 $pad = ( '' !== $content && ( '`' === $content[0] || '`' === substr( $content, -1 ) ) ) ? ' ' : '';
582
583 return $fence . $pad . $content . $pad . $fence;
584 }
585
586 private function prefix_blockquote( string $content ): string {
587 if ( 0 === $this->blockquote_depth ) {
588 return $content;
589 }
590
591 $prefix = str_repeat( '> ', $this->blockquote_depth );
592 $lines = explode( "\n", $content );
593
594 return implode( "\n", array_map(
595 function ( $line ) use ( $prefix ) {
596 return $prefix . $line;
597 },
598 $lines
599 ) );
600 }
601
602 private function escape_text( string $text, bool $in_code ): string {
603 $text = html_entity_decode( $text, ENT_QUOTES, 'UTF-8' );
604
605 if ( $in_code ) {
606 return $text;
607 }
608
609 $replacements = [];
610
611 foreach ( self::ESCAPABLE_MARKDOWN_CHARS as $char ) {
612 $replacements[ $char ] = '\\' . $char;
613 }
614
615 return strtr( $text, $replacements );
616 }
617
618 private function escape_url_for_markdown( string $url ): string {
619 return strtr( $url, [
620 ' ' => '%20',
621 '(' => '%28',
622 ')' => '%29',
623 '<' => '%3C',
624 '>' => '%3E',
625 ] );
626 }
627
628 private function is_safe_url( string $url ): bool {
629 $url = trim( $url );
630
631 if ( '' === $url ) {
632 return false;
633 }
634
635 if ( '#' === $url[0] || '/' === $url[0] || '?' === $url[0] ) {
636 return true;
637 }
638
639 $lower = strtolower( $url );
640
641 foreach ( self::SAFE_DATA_PREFIXES as $prefix ) {
642 if ( 0 === strpos( $lower, $prefix ) ) {
643 return true;
644 }
645 }
646
647 if ( ! preg_match( '#^([a-z][a-z0-9+.\-]*):#i', $url, $m ) ) {
648 return true;
649 }
650
651 return in_array( strtolower( $m[1] ), self::SAFE_URL_SCHEMES, true );
652 }
653 }
654