Markdown::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Rarst\Hugo\wprss2hugo\Serializer;
5
6
use League\HTMLToMarkdown\HtmlConverter;
7
8
/**
9
 * Markdown serializer.
10
 */
11
class Markdown implements Content
12
{
13
    /**
14
     * Convert HTML string to Markdown.
15
     */
16
    public function serialize(string $input): string
17
    {
18
        $converter = new HtmlConverter(['header_style' => 'atx']);
19
20
        // Override HTML comments handling to preserve 'more' tags.
21
        $environment = $converter->getEnvironment();
22
        $environment->addConverter(new CommentConverter());
23
24
        $markdown = $converter->convert($input);
25
        // Ends up with prepended slash for whatever reason...
26
        $markdown = str_replace('\<!--more-->', '<!--more-->', $markdown);
27
28
        return $markdown;
29
    }
30
31
    /**
32
     * Markdown file type.
33
     */
34
    public function type(): string
35
    {
36
        return 'md';
37
    }
38
}
39