actuel-inactuel/site/plugins/epub-export/index.php

50 lines
1.5 KiB
PHP

<?php
Kirby::plugin('tonnom/epub-export', [
'api' => [
'routes' => [
[
'pattern' => 'epub-export/(:any)',
'method' => 'POST',
'action' => function () {
$pageId = get('id');
$page = page($pageId);
throw new Exception($page);
if (!$page) {
throw new Exception("Page introuvable");
}
// Markdown du champ Body
$markdown = $page->body()->toMarkdown();
// Emplacement du fichier
$root = kirby()->root('media') . '/exports';
Dir::make($root);
$filename = $page->id() . '.md';
$filepath = $root . '/' . $filename;
file_put_contents($filepath, $markdown);
return [
'message' => 'OK',
'file' => $filename,
'path' => $filepath,
'url' => kirby()->url('media') . '/exports/' . $filename,
];
}
]
]
],
'sections' => [
'epubexport' => [
'props' => [
'pageId' => function () {
return $this->model()->id();
},
],
'component' => 'export-button'
]
]
]);