code16 / sharp-doc-reader
Display Markdown docs inside Sharp
Fund package maintenance!
Requires
- php: ^8.3
- code16/sharp: ^9.0||^10.0
- illuminate/contracts: ^11.0||^12.0||^13.0
- league/commonmark: ^2.4
- spatie/laravel-package-tools: ^1.16
- symfony/yaml: ^6.4||^7.0||^8.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^11.0.0||^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Display a directory of Markdown files as docs inside Sharp: a list of docs, and a page for each one with its table of contents.
Supports Sharp 9 and 10.
Installation
composer require code16/sharp-doc-reader
Optionally, publish the config file:
php artisan vendor:publish --tag="sharp-docs-config"
Usage
Create an entity extending DocsEntity, and give it the path of your Markdown files:
namespace App\Sharp\Entities; use Code16\SharpDocs\Sharp\DocsEntity; class AdminDocsEntity extends DocsEntity { protected string $label = 'Docs'; protected function docsPath(): string { return base_path('docs/sharp'); } }
There is no list or show class to write. Then declare the entity as any other Sharp entity (discoverEntities() or declareEntity()), and add it to your menu:
$menu->addEntityLink(AdminDocsEntity::class, 'Docs');
Several docs sets
Create one entity per docs set: each has its own path, label, menu entry and policy.
class UserDocsEntity extends DocsEntity { protected string $label = 'User guide'; protected ?string $policy = UserDocsPolicy::class; protected function docsPath(): string { return base_path('docs/users'); } }
Docs files
- Every
.mdfile of the path is a doc, subfolders included (the list shows the folder). - The title is the
titleof the front-matter if any, else the first# h1, else the headline of the file name. - The first h1 is the page title and is not repeated in the content; the other headings move up a level (
##is rendered as a<h1>), and the<h1>ones make the table of contents. - Docs are sorted by folder, then by
order(front-matter), then by title. - A
guides/index.mdfile is theguidesdoc: its files (e.g. images) can live next to it. - Relative images (
) are resolved from the folder of the doc and served by the package route. - Relative links to another doc's file (
[install](../install/index.md#usage)) lead to the show page of that doc (and to its#usageheading).
--- title: Getting started order: 1 --- # Ignored when the front-matter has a title ## Installation ... 
The doc ids are the path of the file without extension, / being replaced by . (guides/install.md is guides.install, guides/index.md is guides).
Images route
The files are served by GET /{sharp url segment}/docs-files/{entityKey}/{path}, only for files inside the entity's docs path. The prefix and the middleware can be changed in the config file; by default only logged-in Sharp users can access it.
Markdown rendering
The rendering uses league/commonmark. Options are set in the markdown key of the config file, or per entity:
protected function markdownOptions(): array { return [ // CommonMark configuration, merged over the defaults 'commonmark' => ['html_input' => 'strip'], // Additional CommonMark extensions (instances or class names) 'extensions' => [\League\CommonMark\Extension\Attributes\AttributesExtension::class], // Set to false to keep the headings as they are, the first h1 included 'title_from_h1' => false, ]; }
By default raw HTML is allowed (html_input: allow): only point the entities to docs you trust, or set it to strip.
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.