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

Adding icons on menu #85

Merged
merged 5 commits into from
Oct 26, 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to add docs contexts
# Adding docs contexts

You can add several contexts of the documentation.

Expand Down
2 changes: 1 addition & 1 deletion docs/how-to-use.md → docs/basic-usage.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to use
# Basic usage

Markdown templates are on `docs` directory.

Expand Down
76 changes: 76 additions & 0 deletions docs/customizing-menu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Customizing menu

## Customizing positions of your pages

You can customize pages into a directory. Add a `pages.php` in this directory and just return a list of files.
This will apply the position you choose in that file.

### Example on the main directory
```php
<?php
// docs/pages.php

return [
'my-first-page.md',
'my-second-page.md',
];
```

### Example on a subdirectory
```php
<?php
// docs/bar/pages.php

return [
'my-first-subdirectory-page.md',
'my-second-subdirectory-page.md',
];
```

### Note
When a page is not on your custom list, it is placed after those on your `pages.php`.

## Adding icons

You can customize icons on the menu with the same `pages.php` you use to sort your pages into a directory.
Just add the attributes to set your icon

### Example
```php
<?php
// docs/pages.php

return [
'chart.md' => [
'icon' => ['data-feather' => 'bar-chart']
],
];
```

Then the following template will be added to your menu.

```html
<span data-feather="bar-chart"></span>
```

By default, [Feather icons](https://feathericons.com/) is added to the layout.
But you can also use the icons you want.
See the [Customizing templates](customizing-templates.md) to add stylesheets and scripts for your icon package.

### Example with Font-awesome
```php
<?php
// docs/pages.php

return [
'cookbook.md' => [
'icon' => ['class' => 'fas fa-book']
],
];
```

Then the following template will be added to your menu.

```html
<span class="fas fa-book"></span>
```
20 changes: 20 additions & 0 deletions docs/customizing-templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Customizing templates

```html
<!-- templates/bundles/MobizelMarkdownDocsBundle/base.html.twig -->
{% extends '@!MobizelMarkdownDocs/base.html.twig' %}

{% block title %}
Custom title - {{ parent() }}
{% endblock %}

{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="path/to/your/custom/css/file"/>
{% endblock %}

{% block javascripts %}
{{ parent() }}
<script src="path/to/your/custom/js/file"></script>
{% endblock %}
```
2 changes: 1 addition & 1 deletion docs/how-to-draw-charts.md → docs/drawing-charts.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to draw charts
# Drawing charts

You can draw charts using [Mermaid.js](https://mermaid-js.github.io/)

Expand Down
29 changes: 0 additions & 29 deletions docs/how-to-customize-pages-positions.md

This file was deleted.

17 changes: 12 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Documentation

## Getting started
* [Setup](setup.md)
* [How to use](how-to-use.md)
* [How to add docs contexts](how-to-add-docs-contexts.md)
* [How to add warning and notes](how-to-add-warning-and-notes.md)
* [How to draw charts](how-to-draw-charts.md)
* [How to localize your documentation](how-to-localize-your-documentation.md)
* [Basic usage](basic-usage.md)
* [Warning and notes](warning-and-notes.md)
* [Drawing charts](drawing-charts.md)

## Configuration
* [Adding docs contexts](adding-docs-contexts.md)
* [Localizing your documentation](localizing-your-documentation.md)

## Customization
* [Customizing templates](customizing-templates.md)
* [Customizing menu](customizing-menu.md)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to localize your documentation
# Localizing your documentation

You can configure your docs context for localization.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to add warning and notes
# Warning and notes

## Warning
```html
Expand Down
11 changes: 10 additions & 1 deletion spec/DataProvider/PageCollectionDataProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function it_returns_pages_map(
'cookbook' => 'Cookbook',
'cookbook/bdd' => 'BDD - Behaviour-driven development',
'cookbook/bdd/phpspec' => 'Phpspec',
'empty' => 'Empty',
'foo' => 'Foo fighters',
'empty' => 'Empty',
'products' => 'Products',
'products/books' => 'Book homepage',
'products/books/nicolas-beuglet' => 'Nicolas Beuglet',
Expand All @@ -108,36 +108,45 @@ function it_returns_pages_as_tree(
$tree['index']->shouldReturn([
'slug' => 'index',
'title' => 'Documentation',
'metadata' => [],
'children' => [],
]);

$tree['products']->shouldReturn([
'slug' => 'products',
'title' => 'Products',
'metadata' => [
'icon' => ['data-feather' => 'box'],
],
'children' => [
'products/books' => [
'slug' => 'products/books',
'title' => 'Book homepage',
'metadata' => [],
'children' => [
'products/books/nicolas-beuglet' => [
'slug' => 'products/books/nicolas-beuglet',
'title' => 'Nicolas Beuglet',
'metadata' => [],
'children' => [],
],
'products/books/stephen-king' => [
'slug' => 'products/books/stephen-king',
'title' => 'Stephen King Books',
'metadata' => [],
'children' => [],
],
],
],
'products/board-games' => [
'slug' => 'products/board-games',
'title' => 'Boardgames',
'metadata' => [],
'children' => [
'products/board-games/puerto-rico' => [
'slug' => 'products/board-games/puerto-rico',
'title' => 'Puerto-rico',
'metadata' => [],
'children' => [],
],
],
Expand Down
26 changes: 17 additions & 9 deletions src/DataProvider/PageCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function getRootPages(): iterable
{
$docsDir = $this->getDocsDir();
$finder = new Finder();
$pagesCustomData = $this->getPagesCustomData($docsDir);

$finder
->files()
Expand All @@ -43,11 +44,12 @@ public function getRootPages(): iterable
//->notName('index.md')
->depth(0)
->append($this->createDirectoryIndexFinder($docsDir))
->sort(PageSorter::sort($this->getPageSorterContents($docsDir)));
->sort(PageSorter::sort($this->getPagesCustomData($docsDir)));

$pages = [];

foreach ($finder as $file) {
$customData = $pagesCustomData[$file->getRelativePathname()] ?? [];
$pageInfo = new PageInfo($file->getPathname(), $file->getRelativePath(), $file->getRelativePathname());

/** @var string $slug */
Expand All @@ -57,7 +59,8 @@ public function getRootPages(): iterable
$pages[] = $this->createPage(
(string) $slug,
$pageInfo->getTitle(),
$pageInfo->getContentWithoutTitle()
$pageInfo->getContentWithoutTitle(),
$customData ?? []
);
}

Expand All @@ -68,6 +71,7 @@ public function getChildrenPages(string $parentSlug): iterable
{
$docsDir = $this->getDocsDir();
$finder = new Finder();
$pagesCustomData = $this->getPagesCustomData($docsDir.'/'.$parentSlug);

try {
$finder
Expand All @@ -77,14 +81,15 @@ public function getChildrenPages(string $parentSlug): iterable
->notName('index.md')
->depth(0)
->append($this->createDirectoryIndexFinder($docsDir.'/'.$parentSlug))
->sort(PageSorter::sort($this->getPageSorterContents($docsDir.'/'.$parentSlug)));
->sort(PageSorter::sort($this->getPagesCustomData($docsDir.'/'.$parentSlug)));
} catch (DirectoryNotFoundException $exception) {
return [];
}

$pages = [];

foreach ($finder as $file) {
$customData = $pagesCustomData[$file->getRelativePathname()] ?? [];
$pageInfo = new PageInfo($file->getPathname(), $file->getRelativePath(), $file->getRelativePathname());

$slug = $parentSlug.'/'.preg_replace('/\.md$/', '', $file->getRelativePathName());
Expand All @@ -93,7 +98,8 @@ public function getChildrenPages(string $parentSlug): iterable
$pages[] = $this->createPage(
(string) $slug,
$pageInfo->getTitle(),
$pageInfo->getContentWithoutTitle()
$pageInfo->getContentWithoutTitle(),
$customData ?? []
);
}

Expand Down Expand Up @@ -126,6 +132,7 @@ public function getPagesAsTree(): array
$tree[$page->slug] = [
'slug' => $page->slug,
'title' => $page->title,
'metadata' => $page->metadata,
'children' => $this->addChildrenOnTreeNode($page->slug),
];
}
Expand All @@ -141,16 +148,17 @@ private function addChildrenOnTreeNode(string $parentSlug): array
$children[$page->slug] = [
'slug' => $page->slug,
'title' => $page->title,
'metadata' => $page->metadata,
'children' => $this->addChildrenOnTreeNode($page->slug),
];
}

return $children;
}

private function createPage(string $slug, string $title, string $content): PageOutput
private function createPage(string $slug, string $title, string $content, array $metadata = []): PageOutput
{
return new PageOutput($slug, $title, $content);
return new PageOutput($slug, $title, $content, null, $metadata);
}

private function getRootPagesMap(): array
Expand All @@ -165,7 +173,7 @@ private function getRootPagesMap(): array
->in($docsDir)
->depth(0)
->append($this->createDirectoryIndexFinder($docsDir))
->sort(PageSorter::sort($this->getPageSorterContents($docsDir)))
->sort(PageSorter::sort($this->getPagesCustomData($docsDir)))
;

$pages = [];
Expand Down Expand Up @@ -197,7 +205,7 @@ private function getChildrenPagesMap(array $row, array $pages, int &$currentPosi
->notName('index.md')
->depth(0)
->append($this->createDirectoryIndexFinder($docsDir.'/'.$parentSlug))
->sort(PageSorter::sort($this->getPageSorterContents($docsDir.'/'.$parentSlug)))
->sort(PageSorter::sort($this->getPagesCustomData($docsDir.'/'.$parentSlug)))
;
} catch (DirectoryNotFoundException $exception) {
return $pages;
Expand Down Expand Up @@ -238,7 +246,7 @@ private function createDirectoryIndexFinder(string $dir): Finder
->depth(1);
}

private function getPageSorterContents(string $dir): array
private function getPagesCustomData(string $dir): array
{
$pageSorterFile = $dir.'/pages.php';

Expand Down
5 changes: 4 additions & 1 deletion src/Dto/PageOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ final class PageOutput
public string $title;
public string $content;
public ?string $tableOfContents = null;
public array $metadata;

public function __construct(
string $slug,
string $title,
string $content,
?string $tableOfContents = null
?string $tableOfContents = null,
array $metadata = []
) {
$this->slug = $slug;
$this->title = $title;
$this->content = $content;
$this->tableOfContents = $tableOfContents;
$this->metadata = $metadata;
}
}
Loading