Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to import markdown to bookstack book

This is a simple script to import multiple markdown files from a single directory to BookStack's book.

  • You can get your API Token and ID from the user's setting.

  • In order to get your BOOK_ID, use the following command

    curl --request GET \
      --url https://<YOUR_BOOKSTACK_DOMAIN>/api/books \
      --header 'Authorization: Token <YOUR_ID_AND_TOKEN_HERE>'

    Ex:

    curl --request GET \
      --url https://bookstack.example.com/api/books \
      --header 'Authorization: Token MY_TOKEN_ID:MY_TOKEN_SECRET'
    

    You'll get a json which will have book id as an integer.

  • Add the path of directory you'd like to import markdown files from.

import requests
from glob import glob
from pathlib import Path
from os import path
BOOKSTACK_URL = "" # Your Bookstack URL
API_ID_TOKEN = "" # API ID and TOKEN in "ID:TOKEN" format
BOOK_ID = 8 #Your Book ID
Directory_path = "/home/notes/Web" # Path to the directory having all the files which needs to be uploaded as new pages
headers = {"Authorization": "Token {}".format(API_ID_TOKEN)}
md_files = glob("{}/*.md".format(Directory_path))
page_url = BOOKSTACK_URL + "/api/pages"
for file in md_files:
with open(file, "r") as f:
data = f.read()
page_name = Path(file).stem
page_create = {"book_id": BOOK_ID, "name": page_name, "markdown": data, "tags": []}
r = requests.post(page_url, data=page_create, headers=headers)
print(r.status_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment