// Script logic
////////////////
+// Get all list of all books in the system
$books = getAllBooks();
+// Get a reference to our output location
$outDir = realpath($exportLocation);
+// Mapping for export formats to the resulting export file extensions
$extensionByFormat = [
'pdf' => 'pdf',
'html' => 'html',
'plaintext' => 'txt',
+ 'markdown' => 'md',
];
+// Loop over each book, exporting each one-by-one and saving its
+// contents into the output location, using the books slug as
+// the file name.
foreach ($books as $book) {
$id = $book['id'];
$extension = $extensionByFormat[$exportFormat] ?? $exportFormat;
/**
* Get all books from the system API.
*/
-function getAllBooks() {
+function getAllBooks(): array {
$count = 100;
$offset = 0;
$total = 0;
$endpoint = 'api/books?' . http_build_query(['count' => $count, 'offset' => $offset]);
$resp = apiGetJson($endpoint);
- // Only set total on first request, due to API bug:
- // https://github.com/BookStackApp/BookStack/issues/2043
- if ($offset == 0) {
- $total = $resp['total'] ?? 0;
- }
-
+ $total = $resp['total'] ?? 0;
$newBooks = $resp['data'] ?? [];
array_push($allBooks, ...$newBooks);
$offset += $count;
# Export All Books
-This script will export all books in your preferred format (PDF, HTML or TXT).
+This script will export all books in your preferred format (PDF, HTML, Markdown or TXT).
## Requirements
# Export as HTML to an existing "html" directory
php export-books.php html ./html
+
+# Export as Markdown to an existing "md-files" directory
+php export-books.php markdown ./md-files
```
\ No newline at end of file