Skip to content

Commit 9b11ee8

Browse files
authored
Merge pull request #127 from hydephp/refine-markdown-document-classes
Refine markdown document classes
2 parents 1c3ea80 + f1df273 commit 9b11ee8

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This serves two purposes:
2323

2424
### Added
2525
- Added Hyde::makeTitle() helper, an improved version of Hyde::titleFromSlug()
26+
- Added new helper method render() to MarkdownDocuments to compile the Markdown to HTML, fixes https://github.com/hydephp/develop/issues/109
2627

2728
### Changed
2829
- Update default HydeFront version to v1.12.x

packages/framework/src/Contracts/AbstractMarkdownPage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @test \Hyde\Framework\Testing\Feature\AbstractPageTest
2121
*/
22-
abstract class AbstractMarkdownPage extends AbstractPage
22+
abstract class AbstractMarkdownPage extends AbstractPage implements MarkdownPageContract
2323
{
2424
use HasDynamicTitle;
2525

packages/framework/src/Contracts/MarkdownDocumentContract.php

+45-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,51 @@ interface MarkdownDocumentContract
88
* Construct the class.
99
*
1010
* @param array $matter The parsed front matter.
11-
* @param string $body The parsed markdown body.
11+
* @param string $body The parsed Markdown body.
1212
*/
1313
public function __construct(array $matter = [], string $body = '');
14+
15+
/**
16+
* Get the front matter property for the specified key, or null if it does not exist.
17+
*
18+
* @param string $key
19+
*/
20+
public function __get(string $key);
21+
22+
/**
23+
* Get the Markdown body.
24+
*
25+
* @return string
26+
*/
27+
public function __toString(): string;
28+
29+
/**
30+
* Get all the front matter as an array, or the property if a key is specified,
31+
* falling back to the supplied default return value if the key is not found.
32+
*
33+
* @param string|null $key
34+
* @param mixed|null $default
35+
* @return mixed|null
36+
*/
37+
public function matter(string $key = null, mixed $default = null): mixed;
38+
39+
/**
40+
* Get the Markdown body.
41+
*
42+
* @return string
43+
*/
44+
public function body(): string;
45+
46+
/**
47+
* Render the Markdown body into HTML.
48+
*/
49+
public function render(): string;
50+
51+
/**
52+
* Parse a Markdown file and return a new MarkdownDocument instance.
53+
*
54+
* @param string $localFilepath
55+
* @return static<MarkdownDocumentContract>
56+
*/
57+
public static function parseFile(string $localFilepath): static;
1458
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Contracts;
4+
5+
use Hyde\Framework\Models\MarkdownDocument;
6+
7+
interface MarkdownPageContract
8+
{
9+
/**
10+
* Get the page's Markdown Document object.
11+
*
12+
* @return \Hyde\Framework\Models\MarkdownDocument
13+
*/
14+
public function markdown(): MarkdownDocument;
15+
}

packages/framework/src/Models/MarkdownDocument.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hyde\Framework\Models;
44

5+
use Hyde\Framework\Actions\MarkdownConverter;
56
use Hyde\Framework\Contracts\MarkdownDocumentContract;
67
use Hyde\Framework\Hyde;
78
use Hyde\Framework\Services\MarkdownFileService;
@@ -21,12 +22,17 @@ public function __construct(array $matter = [], string $body = '')
2122
$this->body = $body;
2223
}
2324

24-
public function __get(string $key)
25+
public function __toString(): string
26+
{
27+
return $this->body;
28+
}
29+
30+
public function __get(string $key): mixed
2531
{
2632
return $this->matter($key);
2733
}
2834

29-
public function matter(string $key = null, $default = null)
35+
public function matter(string $key = null, mixed $default = null): mixed
3036
{
3137
if ($key) {
3238
return Arr::get($this->matter, $key, $default);
@@ -40,6 +46,11 @@ public function body(): string
4046
return $this->body;
4147
}
4248

49+
public function render(): string
50+
{
51+
return MarkdownConverter::parse($this->body);
52+
}
53+
4354
public static function parseFile(string $localFilepath): static
4455
{
4556
return (new MarkdownFileService(Hyde::path($localFilepath)))->get();

packages/framework/tests/Unit/MarkdownDocumentTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public function test_magic_get_method_returns_null_if_property_does_not_exist()
4242
$this->assertNull($document->bar);
4343
}
4444

45+
public function test_magic_to_string_method_returns_body()
46+
{
47+
$document = new MarkdownDocument(['foo' => 'bar'], 'Hello, world!');
48+
$this->assertEquals('Hello, world!', (string) $document);
49+
}
50+
4551
public function test_matter_method_returns_empty_array_if_document_has_no_matter()
4652
{
4753
$document = new MarkdownDocument();
@@ -84,6 +90,12 @@ public function test_body_method_returns_document_body()
8490
$this->assertEquals('Hello, world!', $document->body());
8591
}
8692

93+
public function test_render_method_returns_rendered_html()
94+
{
95+
$document = new MarkdownDocument([], 'Hello, world!');
96+
$this->assertEquals("<p>Hello, world!</p>\n", $document->render());
97+
}
98+
8799
public function test_parse_file_method_parses_a_file_using_the_markdown_file_service()
88100
{
89101
file_put_contents('_pages/foo.md', 'Hello, world!');

0 commit comments

Comments
 (0)