Skip to content

Commit

Permalink
Merge pull request #330 from hydephp/make-file-parser-helpers-public
Browse files Browse the repository at this point in the history
Make file parser helpers public
  • Loading branch information
caendesilva authored Aug 4, 2022
2 parents 4553440 + 338445c commit 9fe9ea1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
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 @@ The identifier property is closely related to the page model's route key propert
- Breaking: Rename AbstractPage property `slug` to `identifier`
- Breaking: Change `AbstractMarkdownPage` constructor argument positions, putting `identifier` first
- Begin changing references to slugs to identifiers, see motivation above
- Makes some helpers in SourceFileParser public static allowing them to be used outside the class

### Deprecated
- for soon-to-be removed features.
Expand Down
28 changes: 14 additions & 14 deletions packages/framework/src/Actions/SourceFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,29 @@ protected function parseMarkdownPage(string $pageClass): AbstractMarkdownPage

protected function constructDynamicData(): void
{
$this->page->title = $this->findTitleForPage();
$this->page->title = static::findTitleForPage($this->page, $this->slug);

if ($this->page instanceof DocumentationPage) {
$this->page->category = $this->getDocumentationPageCategory();
$this->page->category = static::getDocumentationPageCategory($this->slug, $this->page);
}
}

protected function findTitleForPage(): string
public static function findTitleForPage($page, $slug): string
{
if ($this->page instanceof BladePage) {
return Hyde::makeTitle($this->slug);
if ($page instanceof BladePage) {
return Hyde::makeTitle($slug);
}

if ($this->page->matter('title')) {
return $this->page->matter('title');
if ($page->matter('title')) {
return $page->matter('title');
}

return $this->findTitleFromMarkdownHeadings() ?? Hyde::makeTitle($this->slug);
return static::findTitleFromMarkdownHeadings($page) ?? Hyde::makeTitle($slug);
}

protected function findTitleFromMarkdownHeadings(): ?string
public static function findTitleFromMarkdownHeadings($page): ?string
{
foreach ($this->page->markdown()->toArray() as $line) {
foreach ($page->markdown()->toArray() as $line) {
if (str_starts_with($line, '# ')) {
return trim(substr($line, 2), ' ');
}
Expand All @@ -99,15 +99,15 @@ protected function findTitleFromMarkdownHeadings(): ?string
return null;
}

protected function getDocumentationPageCategory(): ?string
public static function getDocumentationPageCategory($slug, $page): ?string
{
// If the documentation page is in a subdirectory,
// then we can use that as the category name.
// Otherwise, we look in the front matter.

return str_contains($this->slug, '/')
? Str::before($this->slug, '/')
: $this->page->matter('category');
return str_contains($slug, '/')
? Str::before($slug, '/')
: $page->matter('category');
}

public function get(): PageContract
Expand Down

0 comments on commit 9fe9ea1

Please sign in to comment.