-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMarkdownviewerController.php
More file actions
54 lines (43 loc) · 1.97 KB
/
MarkdownviewerController.php
File metadata and controls
54 lines (43 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Plugins\markdownviewer;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Typemill\Models\Navigation;
use Typemill\Models\Content;
use Typemill\Models\StorageWrapper;
use Typemill\Controllers\Controller;
class MarkdownviewerController extends Controller
{
public function index(Request $request, Response $response, $args)
{
$url = isset($args['path']) ? '/' . $args['path'] : '/';
$urlinfo = $this->c->get('urlinfo');
$langattr = $this->settings['langattr'];
// Also strip .md from urlinfo paths
$urlinfo['currentpath'] = substr($urlinfo['currentpath'], 0, -3);
$urlinfo['route'] = substr($urlinfo['route'], 0, -3);
$urlinfo['currenturl'] = substr($urlinfo['currenturl'], 0, -3);
// Use Navigation to find the page
$navigation = new Navigation();
$draftNavigation = $navigation->getFullDraftNavigation($urlinfo, $langattr);
$pageinfo = $navigation->getPageInfoForUrl($url, $urlinfo, $langattr);
if (!$pageinfo) {
$response->getBody()->write('Page not found');
return $response->withStatus(404);
}
$keyPathArray = explode(".", $pageinfo['keyPath']);
$item = $navigation->getItemWithKeyPath($draftNavigation, $keyPathArray);
if (!$item) {
$response->getBody()->write('Item not found');
return $response->withStatus(404);
}
// Use Content to get the markdown
$content = new Content($urlinfo['baseurl'], $this->settings, $this->c->get('dispatcher'));
$liveMarkdown = $content->getLiveMarkdown($item);
$response->getBody()->write($liveMarkdown);
return $response
->withHeader('Content-Type', 'text/markdown; charset=utf-8')
->withHeader('Content-Disposition', 'inline')
->withStatus(200);
}
}