@@ 20-61 (lines=42) @@ | ||
17 | /** |
|
18 | * Class ContentObserver. |
|
19 | */ |
|
20 | class ContentObserver |
|
21 | { |
|
22 | /** |
|
23 | * @var ContentRenderInterface |
|
24 | */ |
|
25 | private $renderer; |
|
26 | ||
27 | /** |
|
28 | * ContentObserver constructor. |
|
29 | * |
|
30 | * @param ContentRenderInterface $renderer |
|
31 | */ |
|
32 | public function __construct(ContentRenderInterface $renderer) |
|
33 | { |
|
34 | $this->renderer = $renderer; |
|
35 | } |
|
36 | ||
37 | /** |
|
38 | * @param Article $article |
|
39 | */ |
|
40 | public function saving(Article $article): void |
|
41 | { |
|
42 | if ($article->content_rendered && ! $article->content_source) { |
|
43 | return; |
|
44 | } |
|
45 | ||
46 | $rendered = $this->renderer->renderBody((string) $article->content_source); |
|
47 | ||
48 | $rendered = $this->parseHeaders($rendered); |
|
49 | ||
50 | $article->content_rendered = (string) $rendered->getContent(); |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * @param string $content |
|
55 | * @return ContentHeadersRenderer |
|
56 | */ |
|
57 | private function parseHeaders(string $content): ContentHeadersRenderer |
|
58 | { |
|
59 | return (new ContentHeadersRenderer($content))->parse(); |
|
60 | } |
|
61 | } |
|
62 |
@@ 20-61 (lines=42) @@ | ||
17 | /** |
|
18 | * Class ContentObserver. |
|
19 | */ |
|
20 | class ContentObserver |
|
21 | { |
|
22 | /** |
|
23 | * @var ContentRenderInterface |
|
24 | */ |
|
25 | private $renderer; |
|
26 | ||
27 | /** |
|
28 | * ContentObserver constructor. |
|
29 | * |
|
30 | * @param ContentRenderInterface $renderer |
|
31 | */ |
|
32 | public function __construct(ContentRenderInterface $renderer) |
|
33 | { |
|
34 | $this->renderer = $renderer; |
|
35 | } |
|
36 | ||
37 | /** |
|
38 | * @param Docs $docs |
|
39 | */ |
|
40 | public function saving(Docs $docs): void |
|
41 | { |
|
42 | // Parse markdown |
|
43 | $rendered = $this->renderer->renderBody((string) $docs->content_source); |
|
44 | ||
45 | // Parse content headers |
|
46 | $parsed = $this->parseHeaders($rendered); |
|
47 | ||
48 | // Update data |
|
49 | $docs->nav = $parsed->getLinks(); |
|
50 | $docs->content_rendered = (string) $parsed->getContent(); |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * @param string $content |
|
55 | * @return ContentHeadersRenderer |
|
56 | */ |
|
57 | private function parseHeaders(string $content): ContentHeadersRenderer |
|
58 | { |
|
59 | return (new ContentHeadersRenderer($content))->parse(); |
|
60 | } |
|
61 | } |
|
62 |