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