generated from Drassil/git-wiki-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild-search-indexes.ps1
244 lines (204 loc) · 8.39 KB
/
build-search-indexes.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
# Script reads text files from specified path(s) then generates new Javascript arrays for the search index
# ---------
# Functions
# ---------
function Get-MetadataBlock {
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$InputContent,
[Parameter()]
[bool]$IsVirtualPage = $false
)
process {
# Regex capturing everything between the first two lines that contain the metadata block characters
$pattern = '(?sm)^---\s*(.*?)\s*^---'
if ($IsVirtualPage) {
$pattern = '(?sm)^<!--\s*(.*?)\s*^-->'
}
if ($InputContent -match $pattern) {
Write-Output $matches[1]
} else {
Write-Warning "No metadata block found"
}
}
}
function Get-ContentAfterMetadata {
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$InputContent,
[Parameter()]
[bool]$IsVirtualPage = $false
)
process {
# Regex to match up until the metadata block end and capture everything following
$pattern = '(?sm)^---\s*(.*?)\s*^---\s*(?<after>.*)$'
if ($IsVirtualPage) {
$pattern = '(?sm)^<!--\s*(.*?)\s*^-->\s*(?<after>.*)$'
}
if ($InputContent -match $pattern) {
Write-Output $matches['after']
} else {
Write-Warning "Metadata block not found; cannot extract content after metadata"
}
}
}
function Parse-Metadata {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$MetaBlock
)
$metadata = @{}
$lines = $MetaBlock -split "`n"
# Tag names of YAML arrays to handle
$arrayTagNames = [System.Collections.ArrayList]@(
"tags"
)
for ($i = 0; $i -lt $lines.Count; $i++) {
$line = $lines[$i].Trim()
if (![string]::IsNullOrWhiteSpace($line)) {
if ($line -match '^(?<key>\w+):\s*(?<value>.*)$') {
$key = $matches['key']
$value = $matches['value']
if ($arrayTagNames.Contains($key)) {
$array = [System.Collections.ArrayList]@()
# If single line syntax
if ($value -match '\[.*\]') {
$trimmed = $value.Trim('[',']')
if (![string]::IsNullOrEmpty($trimmed)) {
$array += @($trimmed -split ',' | ForEach-Object { $_.Trim() })
}
$metadata[$key] = $array
# If multi-line syntax
} elseif ($value -eq '' -or $value -notmatch '\S') {
# Look ahead for lines beginning with `-`
while (($i + 1) -lt $lines.Count) {
$next = $lines[$i+1]
if ($next.Trim() -match '^-') {
$array += $next.Trim() -replace '^- ',''
$i++ # increment outer loop index so after while loop ends the outer loop skips already parsed value lines
} else {
break
}
}
$metadata[$key] = $array
} else {
$metadata[$key] = $value
}
} else {
$metadata[$key] = $value
}
}
}
}
return $metadata
}
function Generate-Index {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[object[]]$PathDirs,
[Parameter()]
[object[]]$PathExtraFiles,
[Parameter()]
[bool]$IsVirtualPage = $false
)
$files = [System.Collections.ArrayList]@()
$items = [System.Collections.ArrayList]@()
$fileExt = '.md'
if ($IsVirtualPage) {
$fileExt = '.txt'
}
ForEach($path in $rootDirs) {
$list = (Get-ChildItem -Path $path -Recurse -Include *$fileExt).FullName
$files.AddRange($list) # concatenate
}
if ($PathExtraFiles) {
ForEach($path in $PathExtraFiles) {
$files.Add($path) | Out-Null
}
}
ForEach($file in $files) {
if ($file) {
$fileContent = Get-Content $file -Raw
$metadataBlock = $fileContent | Get-MetadataBlock -IsVirtualPage $IsVirtualPage
if ($metadataBlock) {
$metadata = Parse-Metadata -MetaBlock $metadataBlock
# Capture everything following the metadata and remove some unnecessary sub-strings
$body = ($fileContent | Get-ContentAfterMetadata -IsVirtualPage $IsVirtualPage) -replace '`n',' ' -replace '`r',' ' -replace '\s+',' ' -replace '``',''
$filePath = $file | Select-String -Pattern '(wiki/.*)' -AllMatches | % { $_.Matches.groups[1] } | % { '/' + $_.Value }
$item = [PSCustomObject]@{}
ForEach ($entry in $metadata.GetEnumerator()) {
$name = $entry.Key
$value = $entry.Value
if ($name -eq "title") {
if ([string]::IsNullOrWhiteSpace($value)) {
$string = Split-Path $file -Leaf # fallback to filename if only contains whitespace
} else {
# Capture everything between single quotes (if present in title), after any prefixed exclamation marks (character escaping) and ignore leading/trailing whitespace
$string = $value | Select-String -Pattern "^[!'\s]*(.*?)['\s]*$" -AllMatches | % { $_.Matches.groups[1] } | % { $_.Value }
}
$item | Add-Member -Name "title" -Value $string -Type NoteProperty
}
if ($name -eq "permalink") {
$string = $value.Trim()
# Force trailing forwardslash if lacking it for lookup consistency
if (!$string.EndsWith('/')) {
$string = $string + '/'
}
$item | Add-Member -Name "url" -Value $string -Type NoteProperty
}
if ($name -eq "tags") {
$item | Add-Member -Name "tags" -Value $value -Type NoteProperty
}
}
# Add fallback values for missing keys
if (!$item.PSObject.Properties['tags']) {
$item | Add-Member -Name "tags" -Value @() -Type NoteProperty
}
if (!$item.PSObject.Properties['url']) {
# Add URL for home page if not present
if ($file -eq 'README.md') {
$item | Add-Member -Name "url" -Value '/' -Type NoteProperty
}
}
# Add remaining keys
$item | Add-Member -Name "content" -value $body -Type NoteProperty
if ($IsVirtualPage) {
$item | Add-Member -Name "filePath" -value $filePath -Type NoteProperty
$item | Add-Member -Name "virtualPage" -value $true -Type NoteProperty
}
$items.Add($item) | Out-Null
}
}
}
return $items
}
# -------------------
# Generate main index
# -------------------
$rootDirs = [System.Collections.ArrayList]@(
"wiki/"
)
$extraFiles = [System.Collections.ArrayList]@(
"README.md" # home page
)
# Output to final rendered site directory
# Note: requires full output directory structure exists prior to script running, hence why the Jekyll hook from `run-index-build.rb` plugin, that calls this script, is set to run after the rest of the site has been generated.
$indexFile = "_site/assets/js/mainindex.js"
$items = Generate-Index -PathDirs $rootDirs -PathExtraFiles $extraFiles
$json = $items | ConvertTo-Json
"export const mainIndex = {0}" -f $json | Set-Content -Path $indexFile
# ----------------------
# Generate virtual index
# ----------------------
$rootDirs = [System.Collections.ArrayList]@(
# Additional array items don't require comma delimiters
"wiki/Entity_Reference/"
)
$indexFile = "_site/assets/js/virtualindex.js"
$items = Generate-Index -PathDirs $rootDirs -IsVirtualPage $true
$json = $items | ConvertTo-Json
"export const virtualIndex = {0}" -f $json | Set-Content -Path $indexFile