-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script to fix search index, as described in #26
- Loading branch information
1 parent
a93ab97
commit 38bd888
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/swift | ||
|
||
import Foundation | ||
|
||
struct SearchIndex: Codable { | ||
let config: SearchIndexConfig | ||
var docs: [SearchIndexDocs] | ||
} | ||
|
||
struct SearchIndexConfig: Codable { | ||
let indexing: String | ||
let lang: [String] | ||
let minSearchLength: Int | ||
let prebuildIndex: Bool | ||
let separator: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case indexing | ||
case lang | ||
case minSearchLength = "min_search_length" | ||
case prebuildIndex = "prebuild_index" | ||
case separator | ||
} | ||
} | ||
|
||
struct SearchIndexDocs: Codable { | ||
let location: String | ||
let text: String | ||
let title: String | ||
} | ||
|
||
let searchIndexPath = "site/search/search_index.json" | ||
|
||
let fileURL = URL(fileURLWithPath: searchIndexPath) | ||
let indexData = try Data(contentsOf: fileURL) | ||
let searchIndex = try JSONDecoder().decode(SearchIndex.self, from: indexData) | ||
var newSearchIndex = searchIndex | ||
var searchIndexDocs = [SearchIndexDocs]() | ||
|
||
for doc in newSearchIndex.docs { | ||
if !doc.location.starts(with: "en/") | ||
&& !doc.location.starts(with: "zh/") | ||
&& !doc.location.starts(with: "de/") | ||
&& !doc.location.starts(with: "fr/") | ||
&& !doc.location.starts(with: "nl/") { | ||
searchIndexDocs.append(doc) | ||
} | ||
} | ||
|
||
newSearchIndex.docs = searchIndexDocs | ||
|
||
try JSONEncoder().encode(newSearchIndex).write(to: fileURL) |