From 8a3a0f1797bdb810e896a2c40898cbac6d4cc6fa Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sat, 14 Dec 2024 19:23:12 +0100 Subject: [PATCH] Warn for sources in included html --- .../Generator/PageContentAnomaly.swift | 7 +++- .../Generator/PageContentGenerator.swift | 42 +++++++++++++++---- CHDataManagement/Main/MainView.swift | 3 +- .../Views/Generic/IconButton.swift | 1 + .../Views/Posts/LocalizedPostDetailView.swift | 4 +- .../Content/Pages/PageIssueView.swift | 2 + 6 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CHDataManagement/Generator/PageContentAnomaly.swift b/CHDataManagement/Generator/PageContentAnomaly.swift index 87f85fd..ecf70a7 100644 --- a/CHDataManagement/Generator/PageContentAnomaly.swift +++ b/CHDataManagement/Generator/PageContentAnomaly.swift @@ -5,6 +5,7 @@ enum PageContentAnomaly { case missingPage(page: String, markdown: String) case missingTag(tag: String, markdown: String) case invalidCommand(command: ShorthandMarkdownKey?, markdown: String) + case warning(String) } extension PageContentAnomaly: Identifiable { @@ -21,6 +22,8 @@ extension PageContentAnomaly: Identifiable { return "missing-tag-\(string)" case .invalidCommand(_, let markdown): return "invalid-command-\(markdown)" + case .warning(let string): + return "warning-\(string)" } } } @@ -50,7 +53,7 @@ extension PageContentAnomaly { switch self { case .failedToLoadContent: return .error - case .missingFile, .missingPage, .missingTag, .invalidCommand: + case .missingFile, .missingPage, .missingTag, .invalidCommand, .warning: return .warning } } @@ -70,6 +73,8 @@ extension PageContentAnomaly: CustomStringConvertible { return "Missing tag: \(string)" case .invalidCommand(_, let markdown): return "Invalid command: \(markdown)" + case .warning(let string): + return "Warning: \(string)" } } } diff --git a/CHDataManagement/Generator/PageContentGenerator.swift b/CHDataManagement/Generator/PageContentGenerator.swift index ec52a47..7de945d 100644 --- a/CHDataManagement/Generator/PageContentGenerator.swift +++ b/CHDataManagement/Generator/PageContentGenerator.swift @@ -121,11 +121,9 @@ final class PageContentParser { private func handleHTML(_: String, markdown: Substring) -> String { let result = String(markdown) - #warning("Check HTML code in markdown for required resources") findImages(in: result) findLinks(in: result) findSourceSets(in: result) - // Things to check: return result } @@ -138,10 +136,12 @@ final class PageContentParser { let imgElements = try document.select("img") // Extract the 'src' attributes from each 'img' element - let srcAttributes = try imgElements.array().compactMap { try $0.attr("src") } + let srcAttributes = try imgElements.array() + .compactMap { try $0.attr("src") } + .filter { !$0.trimmed.isEmpty } for src in srcAttributes { - print("Found image in html: \(src)") + results.issues.insert(.warning("Found image in html: \(src)")) } } catch { print("Error parsing HTML: \(error)") @@ -157,10 +157,16 @@ final class PageContentParser { let linkElements = try document.select("a") // Extract the 'src' attributes from each 'img' element - let srcAttributes = try linkElements.array().compactMap { try $0.attr("href") } + let srcAttributes = try linkElements.array() + .compactMap { try $0.attr("href").trimmed } + .filter { !$0.isEmpty } - for src in srcAttributes { - print("Found link in html: \(src)") + for url in srcAttributes { + if url.hasPrefix("http://") || url.hasPrefix("https://") { + results.externalLinks.insert(url) + } else { + results.issues.insert(.warning("Relative link in HTML: \(url)")) + } } } catch { print("Error parsing HTML: \(error)") @@ -176,10 +182,28 @@ final class PageContentParser { let linkElements = try document.select("source") // Extract the 'src' attributes from each 'img' element - let srcAttributes = try linkElements.array().compactMap { try $0.attr("srcset") } + let srcsetAttributes = try linkElements.array() + .compactMap { try $0.attr("srcset") } + .filter { !$0.trimmed.isEmpty } + + for src in srcsetAttributes { + results.issues.insert(.warning("Found source set in html: \(src)")) + } + + let srcAttributes = try linkElements.array() + .compactMap { try $0.attr("src") } + .filter { !$0.trimmed.isEmpty } for src in srcAttributes { - print("Found source set in html: \(src)") + guard content.isValidIdForFile(src) else { + results.issues.insert(.warning("Found source in html: \(src)")) + continue + } + guard let file = content.file(src) else { + results.issues.insert(.warning("Found source in html: \(src)")) + continue + } + results.files.insert(file) } } catch { print("Error parsing HTML: \(error)") diff --git a/CHDataManagement/Main/MainView.swift b/CHDataManagement/Main/MainView.swift index 7ff13f7..7cc6e2d 100644 --- a/CHDataManagement/Main/MainView.swift +++ b/CHDataManagement/Main/MainView.swift @@ -1,9 +1,10 @@ import SwiftUI import SFSafeSymbols +#warning("Allow selection of pages as navigation bar items") +#warning("Show all warnings on page content") #warning("Button to delete file") #warning("Fix podcast") -#warning("Allow selection of pages as navigation bar items") #warning("Add link to other language") #warning("Transfer images of posts to other language") #warning("Show tag selection view for pages") diff --git a/CHDataManagement/Views/Generic/IconButton.swift b/CHDataManagement/Views/Generic/IconButton.swift index 6e44b6b..e0f283c 100644 --- a/CHDataManagement/Views/Generic/IconButton.swift +++ b/CHDataManagement/Views/Generic/IconButton.swift @@ -32,5 +32,6 @@ struct IconButton: View { .fill(background) .padding(1)) } + .buttonStyle(.plain) } } diff --git a/CHDataManagement/Views/Posts/LocalizedPostDetailView.swift b/CHDataManagement/Views/Posts/LocalizedPostDetailView.swift index 0a82b9b..24c9338 100644 --- a/CHDataManagement/Views/Posts/LocalizedPostDetailView.swift +++ b/CHDataManagement/Views/Posts/LocalizedPostDetailView.swift @@ -19,7 +19,7 @@ struct LocalizedPostDetailView: View { .font(.headline) OptionalTextField("", text: $item.linkPreviewTitle, prompt: item.title) - .textFieldStyle(.roundedBorder) + .textFieldStyle(.roundedBorder) .padding(.bottom) HStack { @@ -29,7 +29,7 @@ struct LocalizedPostDetailView: View { size: 22, color: .blue) { showImagePicker = true - } + }.padding(.bottom) IconButton(symbol: .trashCircleFill, size: 22, diff --git a/CHDataManagement/Views/Settings/Content/Pages/PageIssueView.swift b/CHDataManagement/Views/Settings/Content/Pages/PageIssueView.swift index d2505ba..4cf9b01 100644 --- a/CHDataManagement/Views/Settings/Content/Pages/PageIssueView.swift +++ b/CHDataManagement/Views/Settings/Content/Pages/PageIssueView.swift @@ -86,6 +86,8 @@ struct PageIssueView: View { private var buttons: [ButtonAction] { switch issue.message { + case .warning: + return [.init(name: "Retry", action: retryPageCheck)] case .failedToLoadContent: return [.init(name: "Retry", action: retryPageCheck)] case .missingFile(let missing, _):