Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit cd837a0

Browse files
committed
Added powershell-files-to-pages example
1 parent 482b772 commit cd837a0

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

Diff for: powershell-files-to-pages/docker-compose.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '3'
2+
services:
3+
powershell:
4+
image: mcr.microsoft.com/powershell:latest
5+
volumes:
6+
- .:/app
7+
working_dir: /app
8+
command: ["pwsh"]

Diff for: powershell-files-to-pages/files-to-pages.ps1

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# We receive the book ID as an argument from when the script is ran.
2+
# This will be the ID of the book we're uploaded pages into.
3+
param(
4+
[Parameter(Mandatory=$true)]
5+
[string]$parentBookId
6+
)
7+
8+
# BookStack API variables
9+
# Uses values from the environment otherwise could be hardcoded here
10+
$baseUrl = $env:BS_URL
11+
$tokenId = $env:BS_TOKEN_ID
12+
$tokenSecret = $env:BS_TOKEN_SECRET
13+
14+
# Function to create a page in BookStack
15+
function Create-BookstackPage {
16+
param(
17+
[Parameter(Mandatory=$true)]
18+
[string]$Name,
19+
20+
[Parameter(Mandatory=$true)]
21+
[string]$Html,
22+
23+
[Parameter(Mandatory=$true)]
24+
[int]$BookId
25+
)
26+
27+
# Create the data to send to the API
28+
$body = @{
29+
name = $Name
30+
html = $Html
31+
book_id = $BookId
32+
} | ConvertTo-Json
33+
34+
# Ready the HTTP headers, including our auth header
35+
$authHeader = "Token {0}:{1}" -f $tokenId, $tokenSecret
36+
$headers = @{
37+
"Content-Type" = "application/json"
38+
"Authorization" = $authHeader
39+
}
40+
41+
# Send the request to our API endpoint as a POST request
42+
$url = "{0}/api/pages" -f $baseUrl
43+
Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
44+
}
45+
46+
# Get the html files in the relative "./files" directory
47+
$files = Get-ChildItem -Path ".\files" -Filter "*.html" -File
48+
49+
# For each of our HTML files, get the file data and name
50+
# then create a page with name matching the filename
51+
# and HTML content using the contents of the file.
52+
foreach ($file in $files) {
53+
$fileContent = Get-Content -Path $file.FullName
54+
Create-BookStackPage $file.Name $fileContent $parentBookId
55+
}
56+

Diff for: powershell-files-to-pages/files/example-a.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>This is exmaple A!</p>

Diff for: powershell-files-to-pages/files/example-b.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<blockquote>This is example B!</blockquote>

Diff for: powershell-files-to-pages/readme.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Create BookStack Pages from HTML Files
2+
3+
This script will scan through a local `files/` directory for `*.html` files then create pages for each within BookStack, where the name of the files is used for the name of the page and the contents of the file is used for the BookStack page content.
4+
5+
**This is a very simplistic single-script-file example of sending data via PowerShell** and was written with little prior PowerShell knowledge.
6+
7+
## Requirements
8+
9+
You will need PowerShell available.
10+
You will also need BookStack API credentials (TOKEN_ID & TOKEN_SECRET) at the ready.
11+
12+
**This script was written using PowerShell (Core) 7.2.10 on Linux".
13+
14+
A `docker-compose.yml` file exists just as a convenient way to run PowerShell, particularly for Linux users.
15+
16+
## Running
17+
18+
First of all, download the `files-to-pages.ps1` script file.
19+
Then you'll need to setup your environment to point to your BookStack instance with the right credentials:
20+
21+
```powershell
22+
# Environment Setup
23+
# ALTERNATIVELY: Open the script and edit the variables at the top
24+
Set-Item -Path env:BS_URL -Value "https://bookstack.example.com" # Set to be your BookStack base URL
25+
Set-Item -Path env:BS_TOKEN_ID -Value "abc123" # Set to be your API token_id
26+
Set-Item -Path env:BS_TOKEN_SECRET -Value "123abc" # Set to be your API token_secret
27+
```
28+
29+
Once configured, then create a `files` folder containing HTML files you want to upload as pages. See the `files` directory of this repo as a very basic example.
30+
Now it's time to run the script like so:
31+
32+
```powershell
33+
./files-to-pages.ps1 <target_book_id>
34+
```
35+
36+
## Examples
37+
38+
```powershell
39+
# Upload HTML files in the relative `files` directory as BookStack pages
40+
# into the existing BookStack "Book" that has an ID of 5:
41+
./files-to-pages.ps1 5
42+
```

0 commit comments

Comments
 (0)