Skip to content

Fix links in generated JSON #84

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

Merged
merged 1 commit into from
Mar 22, 2021
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
26 changes: 13 additions & 13 deletions src/Generator/JsonGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ private function determineNext(string $parserFilename, array $flattenedTocTree):
return null;
}

$meta = $this->getMetaEntry($nextFileName);

return [
'title' => $meta->getTitle(),
'link' => $meta->getUrl(),
];
return $this->makeRelativeLink($parserFilename, $nextFileName);
}

private function determinePrev(string $parserFilename, array $flattenedTocTree): ?array
Expand All @@ -167,12 +162,7 @@ private function determinePrev(string $parserFilename, array $flattenedTocTree):
return null;
}

$meta = $this->getMetaEntry($previousFileName);

return [
'title' => $meta->getTitle(),
'link' => $meta->getUrl(),
];
return $this->makeRelativeLink($parserFilename, $previousFileName);
}

private function getMetaEntry(string $parserFilename, bool $throwOnMissing = false): ?MetaEntry
Expand Down Expand Up @@ -266,7 +256,7 @@ private function determineParents(string $parserFilename, array $tocTreeHierarch
return $parents;
}

$subParents = $this->determineParents($parserFilename, $tocTree, $parents + [$filename]);
$subParents = $this->determineParents($parserFilename, $tocTree, $parents + [$this->makeRelativeLink($parserFilename, $filename)]);

if (null !== $subParents) {
// the item WAS found and the parents were returned
Expand All @@ -277,4 +267,14 @@ private function determineParents(string $parserFilename, array $tocTreeHierarch
// item was not found
return null;
}

private function makeRelativeLink(string $currentFilename, string $filename): array
{
$meta = $this->getMetaEntry($filename);

return [
'title' => $meta->getTitle(),
'link' => str_repeat('../', substr_count($currentFilename, '/')).$meta->getUrl(),
];
}
}
18 changes: 14 additions & 4 deletions tests/JsonIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ public function getJsonTests()
yield 'crud' => [
'file' => 'crud',
'data' => [
'parents' => ['design'],
'parents' => [
[
'title' => 'Design',
'link' => 'design.html',
],
],
'prev' => [
'title' => 'Design',
'link' => 'design.html',
Expand All @@ -96,14 +101,19 @@ public function getJsonTests()
yield 'design/sub-page' => [
'file' => 'design/sub-page',
'data' => [
'parents' => ['design'],
'parents' => [
[
'title' => 'Design',
'link' => '../design.html',
],
],
'prev' => [
'title' => 'CRUD',
'link' => 'crud.html',
'link' => '../crud.html',
],
'next' => [
'title' => 'Fields',
'link' => 'fields.html',
'link' => '../fields.html',
],
'title' => 'Design Sub-Page',
]
Expand Down