diff --git a/bookstack.el b/bookstack.el index 88eb5c2..ff4cb71 100644 --- a/bookstack.el +++ b/bookstack.el @@ -84,19 +84,24 @@ executing CALLBACK with the decoded result." 'utf-8))) (bookstack--retrieve (format "pages/%d" id) (lambda (_))))) -(defun bookstack--read-page (pages) - "Reads a page from the user with `completing-read', returning its ID. -PAGES should be a sequence of pages (as returned by `bookstack-pages')." +(defun bookstack--read (prompt items) + "Reads an item from the user with `completing-read', returning its ID. ITEMS +should be a data sequence of items returned from a listing endpoint such as +`bookstack-pages'." (let* ((opts (seq-reduce - (lambda (acc page) - (push `(,(alist-get 'name page) . ,(alist-get 'id page)) acc)) - pages + (lambda (acc item) + (push `(,(alist-get 'name item) . ,(alist-get 'id item)) acc)) + items nil)) (page-name (progn (switch-to-minibuffer) - (completing-read "Page: " opts)))) + (completing-read prompt opts)))) (cdr (assoc page-name opts)))) +(defun bookstack--read-book (books) (bookstack--read "Book: " books)) +(defun bookstack--read-chapter (chapters) (bookstack--read "Chapter: " chapters)) +(defun bookstack--read-page (pages) (bookstack--read "Page: " pages)) + (defun bookstack--edit-page (id) "Asynchronously loads a page by ID for editing into a new buffer." (bookstack-page @@ -116,6 +121,18 @@ PAGES should be a sequence of pages (as returned by `bookstack-pages')." (switch-to-buffer buffer) (bookstack-mode +1)))))) +(defun bookstack-books (callback) + "Retrieve all books from `bookstack-server'." + (bookstack--retrieve + "books" + (lambda (res) (apply callback (list (alist-get 'data res)))))) + +(defun bookstack-chapters (callback) + "Retrieve all chapters from `bookstack-server'." + (bookstack--retrieve + "chapters" + (lambda (res) (apply callback (list (alist-get 'data res)))))) + (defun bookstack-pages (callback) "Retrieve all pages from `bookstack-server'." (bookstack--retrieve