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

Fix #399: MarkdownFileParser not using the Hyde path #419

Merged
merged 5 commits into from
Aug 12, 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
15 changes: 14 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This release continues refactoring the internal codebase. As part of this, a lar
- Blog posts now have the same open graph title format as other pages
- Merged deprecated method `getRoutesForModel` into `getRoutes` in `RouteCollection`
- Cleans up and refactors `GeneratesDocumentationSearchIndexFile`, and marks it as internal
- Changed MarkdownFileParser to expect that the supplied filepath is relative to the root of the project (this may break method calls where an absolute path is supplied, see upgrade guide)
- internal: Inline deprecated internal method usage `getOutputPath` replacing it `Hyde::pages()` helper with in `HydeRebuildStaticSiteCommand`

### Deprecated
Expand All @@ -32,7 +33,19 @@ This release continues refactoring the internal codebase. As part of this, a lar
- internal: Remove deprecated testing helper functions `backup` and `restore`

### Fixed
- for any bug fixes.
- MarkdownFileParser not using the Hyde path [#399](https://github.com/hydephp/develop/issues/399)

### Security
- in case of vulnerabilities.

### Upgrade Guide

#### MarkdownFileParser path change
This class now expects the supplied filepath to be relative to the root of the project. This will only affect you if you have written any custom code that uses this class. All internal Hyde code is already updated to use the new path format.

To upgrade, change any calls you may have like follows:

```diff
-return (new MarkdownFileParser(Hyde::path('_posts/foo.md')))->get();
+return (new MarkdownFileParser('_posts/foo.md'))->get();
```
3 changes: 1 addition & 2 deletions packages/framework/src/Models/MarkdownDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Hyde\Framework\Models;

use Hyde\Framework\Contracts\MarkdownDocumentContract;
use Hyde\Framework\Hyde;
use Hyde\Framework\Modules\Markdown\MarkdownFileParser;

/**
Expand Down Expand Up @@ -41,6 +40,6 @@ public function markdown(): Markdown

public static function parse(string $localFilepath): static
{
return (new MarkdownFileParser(Hyde::path($localFilepath)))->get();
return (new MarkdownFileParser($localFilepath))->get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function markdown(string $key): static
$collection = new DataCollection($key);
foreach ($collection->getMarkdownFiles() as $file) {
$collection->push(
(new MarkdownFileParser($file))->get()
(new MarkdownFileParser(Hyde::pathToRelative($file)))->get()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework\Modules\Markdown;

use Hyde\Framework\Hyde;
use Hyde\Framework\Models\MarkdownDocument;
use Spatie\YamlFrontMatter\YamlFrontMatter;

Expand All @@ -26,9 +27,9 @@ class MarkdownFileParser
*/
public string $markdown = '';

public function __construct(string $filepath)
public function __construct(string $localFilepath)
{
$stream = file_get_contents($filepath);
$stream = file_get_contents(Hyde::path($localFilepath));

// Check if the file has Front Matter.
if (str_starts_with($stream, '---')) {
Expand Down
10 changes: 5 additions & 5 deletions packages/framework/tests/Feature/MarkdownFileParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function test_can_parse_markdown_file()
{
file_put_contents(Hyde::path('_posts/test-post.md'), 'Foo bar');

$document = (new MarkdownFileParser(Hyde::path('_posts/test-post.md')))->get();
$document = (new MarkdownFileParser(('_posts/test-post.md')))->get();
$this->assertInstanceOf(MarkdownDocument::class, $document);

$this->assertEquals('Foo bar', $document->markdown);
Expand All @@ -45,7 +45,7 @@ public function test_can_parse_markdown_file_with_front_matter()
{
$this->makeTestPost();

$document = (new MarkdownFileParser(Hyde::path('_posts/test-post.md')))->get();
$document = (new MarkdownFileParser(('_posts/test-post.md')))->get();
$this->assertInstanceOf(MarkdownDocument::class, $document);

$this->assertEquals(FrontMatter::fromArray([
Expand All @@ -64,7 +64,7 @@ public function test_parsed_markdown_post_contains_valid_front_matter()
{
$this->makeTestPost();

$post = (new MarkdownFileParser(Hyde::path('_posts/test-post.md')))->get();
$post = (new MarkdownFileParser(('_posts/test-post.md')))->get();
$this->assertEquals('My New Post', $post->matter('title'));
$this->assertEquals('Mr. Hyde', $post->matter('author'));
$this->assertEquals('blog', $post->matter('category'));
Expand All @@ -74,7 +74,7 @@ public function test_parsed_front_matter_does_not_contain_slug_key()
{
file_put_contents(Hyde::path('_posts/test-post.md'), "---\nslug: foo\n---\n");

$post = (new MarkdownFileParser(Hyde::path('_posts/test-post.md')))->get();
$post = (new MarkdownFileParser(('_posts/test-post.md')))->get();
$this->assertNull($post->matter('slug'));
$this->assertEquals(FrontMatter::fromArray([]), $post->matter);
}
Expand All @@ -83,7 +83,7 @@ public function test_static_parse_shorthand()
{
$this->makeTestPost();

$post = MarkdownFileParser::parse(Hyde::path('_posts/test-post.md'));
$post = MarkdownFileParser::parse(('_posts/test-post.md'));
$this->assertEquals('My New Post', $post->matter('title'));
$this->assertEquals('Mr. Hyde', $post->matter('author'));
$this->assertEquals('blog', $post->matter('category'));
Expand Down