From 38bd888e2f336e101854eb2433a7f1042ccc7cc9 Mon Sep 17 00:00:00 2001 From: fredericsimard Date: Fri, 5 Aug 2022 09:45:31 -0400 Subject: [PATCH] Script to fix search index, as described in #26 --- fixSearchIndex.swift | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 fixSearchIndex.swift diff --git a/fixSearchIndex.swift b/fixSearchIndex.swift new file mode 100644 index 000000000..32d9b1be0 --- /dev/null +++ b/fixSearchIndex.swift @@ -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) \ No newline at end of file