From eb587b7a575aca72b88666ff7fe87eff9e2ee9dc Mon Sep 17 00:00:00 2001 From: Jayaram Rajgopal Date: Fri, 6 Oct 2023 17:01:33 -0400 Subject: [PATCH] #65 Added and used swift-markdown package to parse markdown text for announcements --- Package.swift | 8 +++++ Sources/App/MarkDownTextExtractor.swift | 11 +++++++ Sources/App/Models/Announcement.swift | 22 +++++++++++++- Sources/App/routes.swift | 40 ++++++++++++++++++++----- 4 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 Sources/App/MarkDownTextExtractor.swift diff --git a/Package.swift b/Package.swift index fd8887d..9a3728f 100644 --- a/Package.swift +++ b/Package.swift @@ -55,6 +55,10 @@ let package = Package( .package( url: "https://github.com/vincentneo/CoreGPX.git", from: "0.9.2" + ), + .package( + url: "https://github.com/apple/swift-markdown.git", + branch: "main" ) ], targets: [ @@ -108,6 +112,10 @@ let package = Package( .product( name: "Turf", package: "turf-swift" + ), + .product( + name: "Markdown", + package: "swift-markdown" ) ] ), diff --git a/Sources/App/MarkDownTextExtractor.swift b/Sources/App/MarkDownTextExtractor.swift new file mode 100644 index 0000000..98147f2 --- /dev/null +++ b/Sources/App/MarkDownTextExtractor.swift @@ -0,0 +1,11 @@ +import Markdown + +struct MarkdownTextExtractor: MarkupWalker { + + private(set) var rawText = "" + + mutating func visitText(_ text: Text) { + rawText.append(text.plainText) + } + +} \ No newline at end of file diff --git a/Sources/App/Models/Announcement.swift b/Sources/App/Models/Announcement.swift index 541abfb..7e41d80 100644 --- a/Sources/App/Models/Announcement.swift +++ b/Sources/App/Models/Announcement.swift @@ -113,7 +113,27 @@ final class Announcement: VersionedModel, Content { } } - init() { } + // Announcement object initializer + init{} ( + id: UUID?, + subject: String, + body: String, + start: String, + end: String, + scheduleType: ScheduleType, + interruptionLevel: InterruptionLevel, + signature: Data + ) { + self.id = id + self.subject = subject + self.body = body + self.start = start + self.end = end + self.scheduleType = scheduleType + self.interruptionLevel = interruptionLevel + self.signature = signature + } + } diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 1429b8c..39d333f 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -8,6 +8,7 @@ import Algorithms import APNSCore import Fluent +import Markdown import UAParserSwift import Vapor import VaporAPNS @@ -140,13 +141,38 @@ func routes(_ application: Application) throws { } } - // Get the current announcements - application.get("announcements") { (request) in - return try await Announcement - .query(on: request.db(.psql)) - .all() - } - + // Get the current announcements with formatting + application.get("announcements", "formatted") { (request) in + return try await Announcement + .query(on: request.db(.psql)) + .all() + } + + //Get the current announcements in raw text format + application.get("announcements") { (request) in + let announcementList = try await Announcement + .query(on: request db(.psql)) + .all() + //strips markdown formatting from each announcement + for oldAnnouncement in announcementList { + let document = Document(parsing: oldAnnouncement.body) + //print(document.debugDescription()) + var extractor = MarkdownTextExtractor() + extractor.visit(document) + //print(extractor.rawText) + var newAnnouncement = Announcement( + oldAnnouncement.id, + oldAnnouncement.subject, + extractor.rawText, + oldAnnouncement.start + oldAnnouncement.end, + oldAnnouncement.scheduleType, + oldAnnouncement.interruptionLevel, + oldAnnouncement.signature + ) + } + } + // Post a new announcement after verifying the request application.post("announcements") { (request) -> Announcement in let announcement = try request.content.decode(Announcement.self, using: decoder)