diff --git a/Sources/Shared/Publication/Services/Search/StringSearchService.swift b/Sources/Shared/Publication/Services/Search/StringSearchService.swift index 1dfd488db..c350bcaf2 100644 --- a/Sources/Shared/Publication/Services/Search/StringSearchService.swift +++ b/Sources/Shared/Publication/Services/Search/StringSearchService.swift @@ -102,37 +102,37 @@ public class StringSearchService: SearchService { private var index = -1 func next() async -> SearchResult { - guard index < publication.readingOrder.count - 1 else { - return .success(nil) - } + while index < publication.readingOrder.count - 1 { + index += 1 + + let link = publication.readingOrder[index] + + guard + let resource = publication.get(link), + let mediaType = link.mediaType, + let extractor = extractorFactory.makeExtractor(for: resource, mediaType: mediaType) + else { + log(.warning, "Cannot extract text from resource: \(link.href)") + continue + } - index += 1 + switch await extractor.extractText(of: resource) { + case let .success(text): + let locators = await findLocators(in: link, resourceIndex: index, text: text) + // If no occurrences were found in the current resource, skip to the next one automatically. + guard !locators.isEmpty else { + continue + } - let link = publication.readingOrder[index] + resultCount = (resultCount ?? 0) + locators.count + return .success(LocatorCollection(locators: locators)) - guard - let resource = publication.get(link), - let mediaType = link.mediaType, - let extractor = extractorFactory.makeExtractor(for: resource, mediaType: mediaType) - else { - log(.warning, "Cannot extract text from resource: \(link.href)") - return await next() - } - - switch await extractor.extractText(of: resource) { - case let .success(text): - let locators = await findLocators(in: link, resourceIndex: index, text: text) - // If no occurrences were found in the current resource, skip to the next one automatically. - guard !locators.isEmpty else { - return await next() + case let .failure(error): + return .failure(.reading(error)) } - - resultCount = (resultCount ?? 0) + locators.count - return .success(LocatorCollection(locators: locators)) - - case let .failure(error): - return .failure(.reading(error)) } + + return .success(nil) } private func findLocators(in link: Link, resourceIndex: Int, text: String) async -> [Locator] {