1 <?php namespace App\Http\Controllers;
5 class DocsController extends Controller
9 * Show the index page for all documents.
12 public function index()
14 return view('docs/index');
18 * Show a page of documentation
19 * @param string $type Documentation Category
20 * @param string $page Documentation Page Name
23 public function showPage($type, $page)
25 $type = strtolower($type);
26 if ($type !== 'admin' && $type !== 'user') abort(404);
28 $cacheKey = $type . '-' . $page;
29 if ($this->cache->has($cacheKey) && !app()->environment('local')) {
30 $html = $this->cache->get($cacheKey);
32 $html = $this->getDocumentFromMarkdown($type, $page);
33 $this->cache->forever($cacheKey, $html);
36 if ($this->cache->has($cacheKey . '_title') && !app()->environment('local')) {
37 $title = $this->cache->get($cacheKey . '_title');
39 $title = $this->getTitleFromHtml($html);
40 $this->cache->forever($cacheKey . '_title', $title);
43 return view('docs/' . $type, [
52 * Get a page title from the HTML content.
53 * @param string $html Page HTML content
56 protected function getTitleFromHtml($html)
58 $titleRegex = '/<h1>(.*?)<\/h1>/';
60 preg_match_all($titleRegex, $html, $matches);
62 if (isset($matches[1]) && isset($matches[1][0])) {
63 return $matches[1][0];
70 * Get document HTML from markdown content.
71 * @param string $type Documentation Category
72 * @param string $page Documentation Page Name
73 * @return string Page HTML
75 protected function getDocumentFromMarkdown($type, $page)
77 $filePath = app()->basePath('resources/docs/' . strtolower($type) . '/' . strtolower($page) . '.md');
79 if (!file_exists($filePath) || !is_readable($filePath)) {
83 $markdown = file_get_contents($filePath);
84 $parseDown = new Parsedown();
85 $html = $parseDown->text($markdown);