-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImportFromNotion.ps1
264 lines (243 loc) · 8.07 KB
/
ImportFromNotion.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<#
.SYNOPSIS
Pulls Notion page and converts it to MDX
.DESCRIPTION
Version: 0.1
Pulls Notion page and converts it to MDX
.NOTES
Author: Robert Dyjas https://dyjas.cc
.EXAMPLE
.\ImportFromNotion.ps1 -PageId 'c7b03c7f5bfd460183e4a850d5168215'
#>
Param(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true)]
[string[]]
$PageId
)
#region Variables
$imageRootFolderPath = './src/img'
$mdxFolderPath = './blog'
$convertedTextArr = @()
$frontmatter = @()
$headers = @{}
$headers.Add("Accept", "application/json")
$headers.Add("Notion-Version", "2022-06-28")
$notionKey = $env:NOTION_KEY | ConvertTo-SecureString -AsPlainText -Force
#endregion Variables
Write-Output "::echo::on"
Write-Output "::group::Getting data"
#region Get page properties
$pageParams = @{
Uri = "https://api.notion.com/v1/pages/$PageId"
Method = 'GET'
Headers = $headers
ContentType = 'application/json'
Authentication = 'Bearer'
Token = $notionKey
}
Write-Output "Getting page properties"
$pageRes = Invoke-RestMethod @pageParams
#endregion Get page properties
#region Get slug
$slugId = $pageRes.properties.Slug.id
$slugParams = @{
Uri = "https://api.notion.com/v1/pages/$PageId/properties/$slugId"
Method = 'GET'
Headers = $headers
ContentType = 'application/json'
Authentication = 'Bearer'
Token = $notionKey
}
Write-Output "Getting page slug"
$slugRes = Invoke-RestMethod @slugParams
#endregion Get slug
#region Get title
$titleId = $pageRes.properties.Title.id
$titleParams = @{
Uri = "https://api.notion.com/v1/pages/$PageId/properties/$titleId"
Method = 'GET'
Headers = $headers
ContentType = 'application/json'
Authentication = 'Bearer'
Token = $notionKey
}
Write-Output "Getting page title"
$titleRes = Invoke-RestMethod @titleParams
#endregion Get title
#region Get tags
$tagsId = $pageRes.properties.tags.id
$tagsParams = @{
Uri = "https://api.notion.com/v1/pages/$PageId/properties/$tagsId"
Method = 'GET'
Headers = $headers
ContentType = 'application/json'
Authentication = 'Bearer'
Token = $notionKey
}
Write-Output "Getting page tags"
$tagsRes = Invoke-RestMethod @tagsParams
#endregion Get tags
#region Get description
$descriptionId = $pageRes.properties.description.id
$descriptionParams = @{
Uri = "https://api.notion.com/v1/pages/$PageId/properties/$descriptionId"
Method = 'GET'
Headers = $headers
ContentType = 'application/json'
Authentication = 'Bearer'
Token = $notionKey
}
Write-Output "Getting page description"
$descriptionRes = Invoke-RestMethod @descriptionParams
#endregion Get description
#region Get blocks
$pageChildrenParams = @{
Uri = "https://api.notion.com/v1/blocks/$PageId/children"
Method = 'GET'
Headers = $headers
ContentType = 'application/json'
Authentication = 'Bearer'
Token = $notionKey
}
Write-Output "Getting page blocks"
$pageChildrenRes = Invoke-RestMethod @pageChildrenParams
#endregion Get blocks
Write-Output "::endgroup::"
#region Calculated variables
$mdxFilePrefix = Get-Date $pageRes.last_edited_time -Format 'yyyy-MM-dd'
$pageSlug = $slugRes.results[0].rich_text.plain_text
$mdxFileName = "$mdxFilePrefix-$pageSlug.mdx"
#endegion Calculated variables
#region Processing content
[object[]]$blockArray = $pageChildrenRes.results
function ConvertFrom-RichText {
param([parameter(Mandatory, ValueFromPipeline)]
[object[]]$RichText)
process {
$blocks = $RichText.rich_text
$mdTextArr = @()
foreach ($block in $blocks) {
<#
$block = $blocks[0]
#>
$mdText = $block.text.content
if ($block.annotations.bold) {$mdText = "**$mdText**"}
if ($block.annotations.italic) { $mdText = "*$mdText*" }
if ($block.annotations.code) { $mdText = "``$mdText``"}
if ($block.text.link.url) { $mdText = "[$mdText]($($block.text.link.url))" }
$mdTextArr += $mdText
}
return $mdTextArr -join ('')
}
}
$imgSubFolderName = $pageSlug
$imageSubFolder = Join-Path $imageRootFolderPath $imgSubFolderName
foreach ($blockObj in $blockArray) {
<#
$blockObj = $blockArray[0]
#>
switch ($blockObj.Type) {
"image" {
if (-not (Test-Path $imageSubFolder)) {
New-Item -Type Directory -Path $imageSubFolder | Out-Null
}
$caption = $blockObj.image.caption.plain_text
$fileUrlNoParams = ($blockObj.image.file.url.split('?'))[0]
$fileNameWithExt = ($fileUrlNoParams.split('/'))[-1]
# TODO drop error when no caption specified
# Check if the file already exists
if (Test-Path (Join-Path $imageSubFolder $fileNameWithExt)) {
# If already exists add -0 to the name, then -1 and so on
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($fileNameWithExt)
$extension = [System.IO.Path]::GetExtension($fileNameWithExt)
$i = 0
do {
$fileNameWithExt = "$fileName-$i$extension"
$i++
} until (-not (Test-Path (Join-Path $imageSubFolder $fileNameWithExt)))
}
Invoke-RestMethod -Method GET -Uri $blockObj.image.file.url -OutFile (Join-Path $imageSubFolder $fileNameWithExt)
$convertedText = "![$caption](../../img/$imgSubFolderName/$fileNameWithExt)"
}
# H1 is converted to H2 to prevent script breaking
# H1 and H2 are hard to distinguish in Notion UI
"heading_1" {
$convertedText = "## $(ConvertFrom-RichText -RichText $blockObj.heading_1)"
}
"heading_2" {
$convertedText = "## $(ConvertFrom-RichText -RichText $blockObj.heading_2)"
}
"heading_3" {
$convertedText = "### $(ConvertFrom-RichText -RichText $blockObj.heading_3)"
}
"code" {
# Doesn't support code caption
$convertedText = @"
``````$($blockObj.code.language)
$($blockObj.code.rich_text[0].plain_text)
``````
"@
}
"quote" {
$convertedText = "> $(ConvertFrom-RichText -RichText $blockObj.quote)"
}
"bulleted_list_item" {
$convertedText = "* $(ConvertFrom-RichText -RichText $blockObj.bulleted_list_item)"
}
"numbered_list_item" {
$convertedText = "1. $(ConvertFrom-RichText -RichText $blockObj.numbered_list_item)"
}
"callout" {
if ($blockObj.callout.icon.emoji -eq '💡') {
$convertedText = "<Tip>`n`n$(ConvertFrom-RichText -RichText $blockObj.callout)`n`n</Tip>"
}
if ($blockObj.callout.icon.emoji -eq '🗒️') {
$convertedText = "<Note>`n`n$(ConvertFrom-RichText -RichText $blockObj.callout)`n`n</Note>"
}
if ($blockObj.callout.icon.emoji -eq '⚠️') {
$convertedText = "<Warning>`n`n$(ConvertFrom-RichText -RichText $blockObj.callout)`n`n</Warning>"
}
}
"paragraph" {
$convertedText = "$(ConvertFrom-RichText -RichText $blockObj.paragraph)"
}
Default { Write-Host -ForegroundColor Red $blockObj.type }
}
$shouldHaveNewLine = $true
if ($convertedTextArr.Count -eq 0) { $shouldHaveNewLine = $false }
if (
# Previous line is list item
$convertedTextArr[-1] -match "`n(1\.|\*) " -and
# Current line is list item, too
$convertedText -match "(1\.|\*) ") {
$shouldHaveNewLine = $false
}
# Convert apostrophe to the correct code
$convertedText = $convertedText.replace([char]8217, "'")
$convertedTextArr += "$($shouldHaveNewLine ? "`n" : '')$convertedText"
}
#endregion Processing content
#region Processing frontmatter
$frontmatter += '---'
$frontmatter += 'templateKey: blog-post'
$frontmatter += "title: $($titleRes.results[0].title.plain_text)"
$frontmatter += "date: $(Get-Date $pageres.last_edited_time -Format "o")"
$frontmatter += "description: $($descriptionRes.results[0].rich_text.plain_text)"
$frontmatter += 'featuredpost: false'
$frontmatter += 'tags:'
foreach ($tag in $tagsRes.multi_select.name) {
$frontmatter += " - $tag"
}
$frontmatter += "---`n"
#region Processing frontmatter
#region Exporting
$frontmatter | Out-File -FilePath (Join-Path $mdxFolderPath $mdxFileName)
$convertedTextArr | Out-File -FilePath (Join-Path $mdxFolderPath $mdxFileName) -Append
#endregion Exporting
#region Outputs
"COMMIT_MSG=Imports blog article from Notion" >> $env:GITHUB_OUTPUT
"PR_TITLE=Adds blog $($titleRes.results[0].title.plain_text)" >> $env:GITHUB_OUTPUT
"BRANCH_NAME=cms/blog/$pageSlug" >> $env:GITHUB_OUTPUT
#endregion Outputs
Write-Output "::echo::off"