diff --git a/app/Controllers/DirectoryController.php b/app/Controllers/DirectoryController.php index 69a1f1b..6d0ce0c 100644 --- a/app/Controllers/DirectoryController.php +++ b/app/Controllers/DirectoryController.php @@ -3,6 +3,7 @@ namespace App\Controllers; use DI\Container; +use Parsedown; use PHLAK\Config\Config; use Slim\Psr7\Request; use Slim\Psr7\Response; @@ -19,6 +20,9 @@ class DirectoryController /** @var Container Application container */ protected $container; + /** @var Parsedown Parsedown component */ + protected $parsedown; + /** @var Twig Twig templating component */ protected $view; @@ -29,10 +33,15 @@ class DirectoryController * @param \PHLAK\Config\Config $config * @param \Slim\Views\Twig $view */ - public function __construct(Container $container, Config $config, Twig $view) - { + public function __construct( + Container $container, + Config $config, + Parsedown $parsedown, + Twig $view + ) { $this->container = $container; $this->config = $config; + $this->parsedown = $parsedown; $this->view = $view; } @@ -65,11 +74,20 @@ class DirectoryController $files->depth(0); } + $readmeFiles = Finder::create()->in($path)->depth(0)->name('/^README\.md$/i'); + if ($readmeFiles->hasResults() && ! $search) { + $readmeArray = iterator_to_array($readmeFiles); + $readme = $this->parsedown->parse( + array_shift($readmeArray)->getContents() + ); + } + return $this->view->render($response, 'index.twig', [ 'breadcrumbs' => $this->breadcrumbs($path), 'files' => $files, 'is_root' => $this->isRoot($path), 'search' => $search ?? null, + 'readme' => $readme ?? null, ]); } diff --git a/app/config/app.php b/app/config/app.php index 362c65e..ad95e92 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -49,4 +49,11 @@ return [ * Defualt value: 1000000000 */ 'max_hash_size' => Helpers::env('MAX_HASH_SIZE'), + + /** + * Parse and render README files inline. + * + * Default value: true + */ + 'render_readme' => Helpers::env('RENDER_README', true), ]; diff --git a/app/resources/sass/app.scss b/app/resources/sass/app.scss index 2cebffe..51ba35d 100644 --- a/app/resources/sass/app.scss +++ b/app/resources/sass/app.scss @@ -12,4 +12,5 @@ $fa-font-path: "./webfonts"; // Fonts @import url("https://fonts.googleapis.com/css?family=Work+Sans:200,400&display=swap"); +@import "markdown.scss"; @import "dark-mode.scss"; diff --git a/app/resources/sass/markdown.scss b/app/resources/sass/markdown.scss new file mode 100644 index 0000000..8f09635 --- /dev/null +++ b/app/resources/sass/markdown.scss @@ -0,0 +1,104 @@ +.markdown { + @apply font-sans; + + a { + @apply text-blue-600; + @apply underline; + + &:hover { + @apply text-blue-800; + } + } + + p, + blockquote, + ul, + ol, + dl, + table, + pre { + @apply my-4; + } + + ul, + ol { + @apply pl-8; + } + + ul { + @apply list-disc; + } + + ol { + @apply list-decimal; + } + + h1 { + @apply text-4xl; + @apply border-b; + @apply border-gray-400; + } + + h2 { + @apply text-3xl; + @apply border-b; + @apply border-gray-400; + } + + h3 { + @apply text-2xl; + } + + h4 { + @apply text-xl; + } + + h5 { + @apply text-base; + } + + h6 { + @apply text-base; + @apply text-gray-600; + } + + h1, + h2, + h3, + h4, + h5, + h6 { + @apply font-bold; + @apply leading-relaxed; + @apply mt-8; + @apply mb-4; + } + + h1 + p, + h2 + p, + h3 + p { + @apply mt-4; + } + + pre { + @apply bg-gray-200; + @apply rounded-sm; + @apply p-4; + @apply font-mono; + @apply text-sm; + @apply whitespace-pre; + } + + hr { + @apply border-t-2; + @apply border-gray-400; + } + + & > :first-child { + @apply mt-0; + } + + & > :last-child { + @apply mb-0; + } +} diff --git a/app/resources/views/index.twig b/app/resources/views/index.twig index 5383b94..29dbeb0 100644 --- a/app/resources/views/index.twig +++ b/app/resources/views/index.twig @@ -29,6 +29,18 @@ {{ include('components/file.twig') }} {% endfor %} + + {% if readme %} +