Esc: make the panel key when shown (closeWithEscape, default on) without activating the app; the source app stays frontmost and Replace still targets it. Handle Esc with SwiftUI's exit command, which also works from the popover and after clicking controls. Add TranslationPanel.canBecomeKey and amend ADR-009.\n\nMarkdown: add MarkdownPresentation in SprachmittlerText (Foundation inline parser plus a conservative block parser for headings, lists, quotes, fenced code and rules), a SwiftUI MarkdownTranslationView, and a renderMarkdown setting (default on). JSON, HTML and code sources are never rendered; only http/https links open; images become alt text; Copy and Replace keep the raw Markdown (ADR-021).\n\nVerified: make verify green; synthetic Esc closes the panel and closes an open language popover first; a live Markdown translation rendered heading, bold/italic, inline code, list, quote, code block and link; Copy returned the raw Markdown; Replace still changed the TextEdit selection with the panel key.
107 lines
4.3 KiB
Swift
107 lines
4.3 KiB
Swift
import Foundation
|
|
import Testing
|
|
@testable import SprachmittlerText
|
|
|
|
@Suite("Markdown presentation")
|
|
struct MarkdownPresentationTests {
|
|
@Test func parsesHeadingsParagraphsAndRules() {
|
|
let blocks = MarkdownPresentation.blocks(in: """
|
|
# Title
|
|
|
|
First paragraph.
|
|
Second line.
|
|
|
|
---
|
|
""")
|
|
#expect(blocks == [
|
|
.heading(level: 1, text: "Title"),
|
|
.paragraph("First paragraph.\nSecond line."),
|
|
.rule,
|
|
])
|
|
}
|
|
|
|
@Test func parsesOrderedAndUnorderedLists() {
|
|
let blocks = MarkdownPresentation.blocks(in: """
|
|
- one
|
|
* two
|
|
3. three
|
|
4) four
|
|
""")
|
|
#expect(blocks == [
|
|
.listItem(ordered: false, number: 0, indent: 0, text: "one"),
|
|
.listItem(ordered: false, number: 0, indent: 0, text: "two"),
|
|
.listItem(ordered: true, number: 3, indent: 0, text: "three"),
|
|
.listItem(ordered: true, number: 4, indent: 0, text: "four"),
|
|
])
|
|
}
|
|
|
|
@Test func parsesQuotesAndFencedCode() {
|
|
let blocks = MarkdownPresentation.blocks(in: """
|
|
> quoted **text**
|
|
> second
|
|
|
|
```swift
|
|
let value = 1
|
|
```
|
|
""")
|
|
#expect(blocks == [
|
|
.quote("quoted **text**\nsecond"),
|
|
.codeBlock(language: "swift", text: "let value = 1"),
|
|
])
|
|
}
|
|
|
|
@Test func keepsUnclosedFenceAsCode() {
|
|
let blocks = MarkdownPresentation.blocks(in: "```\nstill code\n")
|
|
#expect(blocks == [.codeBlock(language: nil, text: "still code")])
|
|
}
|
|
|
|
@Test func recognisesInlineFormatting() {
|
|
let attributed = MarkdownPresentation.inline("**bold** and *italic* and `code`")
|
|
let intents = attributed.runs.compactMap(\.inlinePresentationIntent)
|
|
#expect(intents.contains { $0.contains(.stronglyEmphasized) })
|
|
#expect(intents.contains { $0.contains(.emphasized) })
|
|
#expect(intents.contains { $0.contains(.code) })
|
|
}
|
|
|
|
@Test func parsesLinksButNeverImages() throws {
|
|
let attributed = MarkdownPresentation
|
|
.inline("See [docs](https://example.com) and ")
|
|
let links = attributed.runs.compactMap(\.link)
|
|
#expect(try links == [#require(URL(string: "https://example.com"))])
|
|
#expect(String(attributed.characters).contains("alt"))
|
|
#expect(!String(attributed.characters).contains("x.png"))
|
|
}
|
|
|
|
@Test func malformedMarkdownFallsBackToText() {
|
|
let attributed = MarkdownPresentation.inline("**unclosed and <unterminated")
|
|
#expect(String(attributed.characters).contains("unclosed"))
|
|
}
|
|
|
|
@Test func onlyHTTPLinksAreAllowed() throws {
|
|
#expect(try MarkdownPresentation.isAllowedLink(#require(URL(string: "https://example.com"))))
|
|
#expect(try MarkdownPresentation.isAllowedLink(#require(URL(string: "http://example.com"))))
|
|
#expect(try !MarkdownPresentation.isAllowedLink(#require(URL(string: "file:///etc/passwd"))))
|
|
#expect(try !MarkdownPresentation.isAllowedLink(#require(URL(string: "javascript:alert(1)"))))
|
|
#expect(try !MarkdownPresentation.isAllowedLink(#require(URL(string: "sprachmittler://open"))))
|
|
}
|
|
|
|
@Test func rendersMarkdownSourcesButNotStructuredData() {
|
|
#expect(MarkdownPresentation.shouldRender(source: "# Title\n\n- item", translation: "x"))
|
|
#expect(MarkdownPresentation.shouldRender(source: "A **bold** sentence.", translation: "x"))
|
|
#expect(MarkdownPresentation.shouldRender(source: "plain", translation: "translated **bold**"))
|
|
#expect(!MarkdownPresentation.shouldRender(source: #"{"key": "value"}"#, translation: "x"))
|
|
#expect(!MarkdownPresentation.shouldRender(source: "<div><p>Hello</p></div>", translation: "x"))
|
|
#expect(!MarkdownPresentation.shouldRender(source: "```\nlet x = 1\n```", translation: "x"))
|
|
#expect(!MarkdownPresentation.shouldRender(source: "Just prose, no markers.", translation: "Nur Prosa."))
|
|
#expect(!MarkdownPresentation.shouldRender(source: "", translation: "**bold**"))
|
|
}
|
|
|
|
@Test func headingsWithDifferentLevelsAndClosingHashes() {
|
|
let blocks = MarkdownPresentation.blocks(in: "### Sub heading ###\n\n####### not a heading")
|
|
#expect(blocks == [
|
|
.heading(level: 3, text: "Sub heading"),
|
|
.paragraph("####### not a heading"),
|
|
])
|
|
}
|
|
}
|