Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

abstract class AbstractMarkdownPlaceholderResolver extends AbstractPlaceholderResolver
{
protected function getDocsPath(string $currentPagePath) : string
{
$rootDir = FilePathDataType::findFolderPath($currentPagePath);
$normalizedFull = FilePathDataType::normalize($rootDir);
$parts = explode("/Docs/", string: $normalizedFull);
if(count($parts) == 1) {
return $normalizedFull;
}
return $parts[0]. "/Docs/";
}

protected function scanMarkdownDirectory(string $directory, int $maxDepth = PHP_INT_MAX, int $currentDepth = 0, $relativePath = ''): array {
if ($currentDepth > $maxDepth) {
return [];
Expand Down
7 changes: 0 additions & 7 deletions Docs/documentation/placeholders/NavButtonsPlaceholder.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ The order is determined by both **numerical** and **alphabetical** sorting of th
- Sorting is done first numerically (e.g., `1. Intro`, `1.1 Usage`), then alphabetically (e.g., `GettingStarted`, `Overview`).
- This placeholder does **not** support any options.


### How It Works

- If the current file represents a **first-level heading** (e.g., a top-level topic or `1. Introduction`), the navigation buttons will link to other **first-level heading** (`2. How To Use`, `3. Summary`) files.
- If the current file represents a **second-level heading** (i.e., a sub-topic under a parent folder e.g., `1.1 Required Hardware`), the buttons will navigate only among other **second-level heading** (`1.2 Required Software`, `1.3 Required Apps`) files within the same parent.


### Example Usage

```html
Expand Down
13 changes: 1 addition & 12 deletions Facades/DocsFacade/Placeholders/ImageNumberResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(string $pagePathAbsolute, string $prefix = 'ImageCap
public function resolve(array $placeholders) : array
{
$vals = [];
$rootDirectory = $this->getDocsPath();
$rootDirectory = $this->getDocsPath($this->pagePath);
$markdownStructure = $this->getFlattenMarkdownFiles($rootDirectory);

$names = array_map(fn($ph) => $ph['name'], $placeholders);
Expand Down Expand Up @@ -66,15 +66,4 @@ function countImagesAndUpdate($files, $targetFile, $order) {
$currentAbbildungNumber += count($matches[0]);
}
}

protected function getDocsPath() : string
{
$rootDir = FilePathDataType::findFolderPath($this->pagePath);
$normalizedFull = FilePathDataType::normalize($rootDir);
$parts = explode("/Docs/", string: $normalizedFull);
if(count($parts) == 1) {
return $normalizedFull;
}
return $parts[0]. "/Docs/";
}
}
12 changes: 1 addition & 11 deletions Facades/DocsFacade/Placeholders/ImageReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(string $pagePathAbsolute, string $prefix = 'ImageRef
public function resolve(array $placeholders) : array
{
$vals = [];
$rootDirectory = $this->getDocsPath();
$rootDirectory = $this->getDocsPath($this->pagePath);
$markdownStructure = $this->getFlattenMarkdownFiles($rootDirectory);

$names = array_map(fn($ph) => $ph['name'], $placeholders);
Expand Down Expand Up @@ -77,14 +77,4 @@ protected function searchTheFile(string $filePath, string $imageId)
}
}

protected function getDocsPath() : string
{
$rootDir = FilePathDataType::findFolderPath($this->pagePath);
$normalizedFull = FilePathDataType::normalize($rootDir);
$parts = explode("/Docs/", string: $normalizedFull);
if(count($parts) == 1) {
return $normalizedFull;
}
return $parts[0]. "/Docs/";
}
}
100 changes: 37 additions & 63 deletions Facades/DocsFacade/Placeholders/NavButtonResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,86 +24,59 @@ public function __construct(string $pagePathAbsolute, string $prefix = 'NavButto
public function resolve(array $placeholders) : array
{
$vals = [];
$rootDirectory = FilePathDataType::findFolderPath($this->pagePath);
$rootDirectory = $this->getDocsPath($this->pagePath);
$names = array_map(fn($ph) => $ph['name'], $placeholders);
$filteredNames = $this->filterPlaceholders($names);
$fileList = $this->getFlattenMarkdownFiles($rootDirectory);
foreach ($placeholders as $i => $placeholder) {
if (in_array($placeholder['name'], $filteredNames)) {
$fileList = $this->getSiblings($rootDirectory);
$val = $this->createButtons($fileList);
$val = $this->createButtons($fileList, $this->pagePath);
$vals[$i] = $val;
}
}
return $vals;
}


protected function getSiblings(string $directory): array {
if (strtolower(basename($this->pagePath)) === 'index.md') {
return $this->getIndexSiblings($directory);
} else {
return $this->getRegularSiblings($directory);
}
}

protected function getIndexSiblings(string $directory): array {
$parent = dirname($directory);
$subfolders = glob($parent . '/*', GLOB_ONLYDIR);

$siblings = [];
foreach ($subfolders as $folder) {
$indexPath = $folder . DIRECTORY_SEPARATOR . 'index.md';
if (file_exists($indexPath)) {
$siblings[] = [
'title' => MarkdownDataType::findHeadOfFile($indexPath),
'link' => FilePathDataType::normalize($indexPath)
];
}
}

$sorter = new RowDataArraySorter();
$sorter->addCriteria('title', SORT_ASC);
return $sorter->sort($siblings);
}

protected function getRegularSiblings(string $directory): array {
$siblings = [];
$files = glob($directory . '/*.md');

foreach ($files as $filePath) {
if (strtolower(basename($filePath)) === 'index.md') {
continue;
}

$siblings[] = [
'title' => MarkdownDataType::findHeadOfFile($filePath),
'link' => FilePathDataType::normalize($filePath)
];
}
$sorter = new RowDataArraySorter();
$sorter->addCriteria('title', SORT_ASC);
return $sorter->sort($siblings);
}

protected function createButtons(array $files): string
protected function createButtons(array $flatList, string $currentPath): string
{
$currentIndex = array_search(FilePathDataType::normalize($this->pagePath), array_column($files, 'link'));

$normalizedCurrent = FilePathDataType::normalize($currentPath);

$normalizedList = array_map(
fn($p) => FilePathDataType::normalize($p),
$flatList
);

$currentIndex = array_search($normalizedCurrent, $normalizedList);

if ($currentIndex === false) {
"";
return '';
}
$prev = $currentIndex > 0 ? $files[$currentIndex - 1] : null;
$next = $currentIndex < count($files) - 1 ? $files[$currentIndex + 1] : null;


$buttons = [];
if ($prev) $buttons[] = $this->mdButon('Zurück',$this->getRelativePath($this->pagePath, $prev['link']));
if ($next) $buttons[] = $this->mdButon('Weiter',$this->getRelativePath($this->pagePath, $next['link']));

$buttonBlock = implode(' ', $buttons) . "\n";
return $buttonBlock;
if ($currentIndex > 0) {
$prevPath = $flatList[$currentIndex - 1];
$buttons[] = $this->mdButon(
'Zurück',
$this->getRelativePath($this->pagePath, $prevPath)
);
}

if ($currentIndex < count($flatList) - 1) {
$nextPath = $flatList[$currentIndex + 1];
$buttons[] = $this->mdButon(
'Weiter',
$this->getRelativePath($this->pagePath, $nextPath)
);
}

return implode(' ', $buttons);
}

function getRelativePath(string $from, string $to): string {

function getRelativePath(string $from, string $to): string
{
$from = is_dir($from) ? rtrim($from, '/') . '/' : dirname($from) . '/';
$from = FilePathDataType::normalize(realpath($from));
$to = FilePathDataType::normalize(realpath($to));
Expand All @@ -120,7 +93,8 @@ function getRelativePath(string $from, string $to): string {
return str_repeat('../', count($fromParts)) . implode('/', $toParts);
}

function mdButon(string $buttonText, string $path) {
function mdButon(string $buttonText, string $path): string
{
return "[<kbd> <br> " . $buttonText . " <br> </kbd>]($path)";
}

Expand Down