From 6e24c27fdc69702ac1d19dbd036f37554eb94e3a Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Thu, 1 Dec 2022 15:39:39 +0100 Subject: [PATCH] Determine elements for news section --- Sources/Generator/Content/Element.swift | 19 ++++++++++++++++++- .../Generator/Content/GenericMetadata.swift | 8 ++++++++ .../Generators/OverviewSectionGenerator.swift | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Sources/Generator/Content/Element.swift b/Sources/Generator/Content/Element.swift index b6a1e28..176016c 100644 --- a/Sources/Generator/Content/Element.swift +++ b/Sources/Generator/Content/Element.swift @@ -126,6 +126,12 @@ struct Element { */ let headerType: HeaderType + /** + Indicate that the overview section should contain a `Newest Content` section before the other sections. + - Note: If not specified, this property defaults to `false` + */ + let showMostRecentSection: Bool + /** The localized metadata for each language. */ @@ -318,6 +324,17 @@ extension Element { } } + func mostRecentElements(_ count: Int) -> [Element] { + guard self.containsElements else { + return [self] + } + let all = shownItems + .reduce(into: [Element]()) { $0 += $1.mostRecentElements(count) } + .filter { $0.date != nil } + .sorted { $0.date! > $1.date! } + return Array(all.prefix(count)) + } + var sortedItems: [Element] { if useManualSorting { return shownItems.sorted { $0.sortIndex! < $1.sortIndex! } @@ -365,7 +382,7 @@ extension Element { // The relative path needs to go down to the first common folder, // before going up to the target page let allParts = [String](repeating: "..", count: ownParts.count-index) - + pageParts.dropFirst(index) + + pageParts.dropFirst(index) return allParts.joined(separator: "/") } diff --git a/Sources/Generator/Content/GenericMetadata.swift b/Sources/Generator/Content/GenericMetadata.swift index 9954905..186fd4b 100644 --- a/Sources/Generator/Content/GenericMetadata.swift +++ b/Sources/Generator/Content/GenericMetadata.swift @@ -125,6 +125,12 @@ struct GenericMetadata { */ let headerType: String? + /** + Indicate that the overview section should contain a `Newest Content` section before the other sections. + - Note: If not specified, this property defaults to `false` + */ + let showMostRecentSection: Bool? + /** The localized metadata for each language. */ @@ -150,6 +156,7 @@ extension GenericMetadata: Codable { .useManualSorting, .overviewItemCount, .headerType, + .showMostRecentSection, .languages, ] } @@ -221,6 +228,7 @@ extension GenericMetadata { useManualSorting: false, overviewItemCount: 6, headerType: "left", + showMostRecentSection: false, languages: [.full]) } } diff --git a/Sources/Generator/Generators/OverviewSectionGenerator.swift b/Sources/Generator/Generators/OverviewSectionGenerator.swift index 12876c5..cb733f6 100644 --- a/Sources/Generator/Generators/OverviewSectionGenerator.swift +++ b/Sources/Generator/Generators/OverviewSectionGenerator.swift @@ -15,6 +15,21 @@ struct OverviewSectionGenerator { } func generate(sections: [Element], in parent: Element, language: String, sectionItemCount: Int) -> String { + let content = sectionsContent(sections, in: parent, language: language, sectionItemCount: sectionItemCount) + if parent.showMostRecentSection { + let news = newsSectionContent(for: parent, language: language, sectionItemCount: sectionItemCount) + return news + "\n" + content + } else { + return content + } + } + + private func newsSectionContent(for element: Element, language: String, sectionItemCount: Int) -> String { + let shownElements = element.mostRecentElements(sectionItemCount) + return "" + } + + private func sectionsContent(_ sections: [Element], in parent: Element, language: String, sectionItemCount: Int) -> String { sections.map { section in let metadata = section.localized(for: language) let fullUrl = section.fullPageUrl(for: language)