Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove body property from Markdown #338

Merged
merged 6 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This update contains **breaking changes** to the internal API regarding page mod
### Removed
- Removed `Facades\Markdown.php`, merged into `Models\Markdown.php`
- Removed `body()` method from `MarkdownDocumentContract` interface and all its implementations. Use `markdown()->body()` (or cast to string) instead
- Removed `body` property from Markdown pages. Use `markdown()->body()` (or cast to string) instead

### Fixed
- for any bug fixes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ public function save(): static
* Returning $document->body as is: 500ms
* Returning $document->body as Str::markdown(): 920ms + 10ms for regex
*/
public function getSearchContentForDocument(DocumentationPage $document): string
public function getSearchContentForDocument(DocumentationPage $page): string
{
// This is compiles the Markdown body into HTML, and then strips out all
// HTML tags to get a plain text version of the body. This takes a long
// site, but is the simplest implementation I've found so far.
return preg_replace('/<(.|\n)*?>/', ' ', Str::markdown($document->body));
return preg_replace('/<(.|\n)*?>/', ' ', Str::markdown($page->markdown));
}

public function getDestinationForSlug(string $slug): string
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Concerns/HasTableOfContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ trait HasTableOfContents
{
public function getTableOfContents(): string
{
return (new GeneratesSidebarTableOfContents($this->body))->execute();
return (new GeneratesSidebarTableOfContents($this->markdown))->execute();
}
}
5 changes: 0 additions & 5 deletions packages/framework/src/Contracts/AbstractMarkdownPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ abstract class AbstractMarkdownPage extends AbstractPage implements MarkdownDocu
public FrontMatter $matter;
public Markdown $markdown;

/** @deprecated */
public string $body;

/** @deprecated */
public string $title;

Expand All @@ -48,8 +45,6 @@ public function __construct(string $identifier = '', ?FrontMatter $matter = null
$this->identifier = $identifier;
$this->matter = $matter ?? new FrontMatter();
$this->markdown = $markdown ?? new Markdown();

$this->body = $this->markdown->body;
}

/** @inheritDoc */
Expand Down
3 changes: 2 additions & 1 deletion packages/framework/src/Contracts/MarkdownPageContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public static function make(string $identifier = '', array $matter = [], string
/**
* Construct a new MarkdownPage object from constructed data types.
* Normally, this is done by the SourceFileParser.
*
* @see \Hyde\Framework\Actions\SourceFileParser
*
*
* The types are strictly enforced to ensure a predictable behavior and constant access interface.
*
* @param string $identifier
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Models/Pages/MarkdownPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getCanonicalLink(): string

public function getPostDescription(): string
{
return $this->matter('description') ?? substr($this->body, 0, 125).'...';
return $this->matter('description') ?? substr($this->markdown, 0, 125).'...';
}

public static function getLatestPosts(): Collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MarkdownFileParser
*
* @var string
*/
public string $body = '';
public string $markdown = '';

public function __construct(string $filepath)
{
Expand All @@ -42,10 +42,10 @@ public function __construct(string $filepath)
}

if ($object->body()) {
$this->body = $object->body();
$this->markdown = $object->body();
}
} else {
$this->body = $stream;
$this->markdown = $stream;
}
}

Expand All @@ -54,7 +54,7 @@ public function __construct(string $filepath)
*/
public function get(): MarkdownDocument
{
return new MarkdownDocument($this->matter, $this->body);
return new MarkdownDocument($this->matter, $this->markdown);
}

public static function parse(string $filepath): MarkdownDocument
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/tests/Feature/MarkdownPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function test_created_model_contains_expected_data()
$page = MarkdownPage::parse('test-post');

$this->assertEquals('PHPUnit Test File', $page->title);
$this->assertEquals("# PHPUnit Test File \n Hello World!", $page->body);
$this->assertEquals("# PHPUnit Test File \n Hello World!", $page->markdown);
$this->assertEquals('test-post', $page->identifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setUp(): void

file_put_contents(Hyde::path('_docs/foo.md'), "# Foo\n\nHello world.");
$this->mock = DocumentationPage::parse('foo');
$this->html = Markdown::render($this->mock->body);
$this->html = Markdown::render($this->mock->markdown->body);
}

protected function tearDown(): void
Expand Down
6 changes: 3 additions & 3 deletions packages/framework/tests/Feature/SourceFileParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_markdown_page_parser()
$page = $parser->get();
$this->assertInstanceOf(MarkdownPage::class, $page);
$this->assertEquals('foo', $page->identifier);
$this->assertEquals('# Foo Bar', $page->body);
$this->assertEquals('# Foo Bar', $page->markdown);
$this->assertEquals('Foo Bar Baz', $page->title);
}

Expand All @@ -44,7 +44,7 @@ public function test_markdown_post_parser()
$page = $parser->get();
$this->assertInstanceOf(MarkdownPost::class, $page);
$this->assertEquals('foo', $page->identifier);
$this->assertEquals('# Foo Bar', $page->body);
$this->assertEquals('# Foo Bar', $page->markdown);
$this->assertEquals('Foo Bar Baz', $page->title);
}

Expand All @@ -56,7 +56,7 @@ public function test_documentation_page_parser()
$page = $parser->get();
$this->assertInstanceOf(DocumentationPage::class, $page);
$this->assertEquals('foo', $page->identifier);
$this->assertEquals('# Foo Bar', $page->body);
$this->assertEquals('# Foo Bar', $page->markdown);
$this->assertEquals('Foo Bar Baz', $page->title);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/framework/tests/Unit/HasTableOfContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HasTableOfContentsTest extends TestCase

public function testConstructorCreatesTableOfContentsString()
{
$this->body = '## Title';
$this->markdown = '## Title';
$this->assertEquals('<ul class="table-of-contents"><li><a href="#title">Title</a></li></ul>', str_replace("\n", '', $this->getTableOfContents()));
}
}
6 changes: 4 additions & 2 deletions packages/framework/tests/Unit/MarkdownPostParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Hyde\Framework\Hyde;
use Hyde\Framework\Models\FrontMatter;
use Hyde\Framework\Models\Markdown;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Testing\TestCase;

Expand Down Expand Up @@ -32,9 +33,10 @@ public function test_can_parse_markdown_file()
$this->assertInstanceOf(MarkdownPost::class, $post);
$this->assertCount(3, ($post->matter->toArray()));
$this->assertInstanceOf(FrontMatter::class, $post->matter);
$this->assertIsString($post->body);
$this->assertInstanceOf(Markdown::class, $post->markdown);
$this->assertIsString($post->markdown->body);
$this->assertIsString($post->identifier);
$this->assertTrue(strlen($post->body) > 32);
$this->assertTrue(strlen($post->markdown) > 32);
$this->assertTrue(strlen($post->identifier) > 8);
}

Expand Down