// What a long text is CALLED and how its Markdown is made plain: // `MarkdownText`'s rules, case for case. The generic slip borrows a note's first // line through them (`TableDefinition.borrowedTitle`) and strips a long text's // Markdown through them (`FaceText.opening`). // // Nothing here renders. Every rule below is settled by a table. import CottonKit import Foundation import XCTest @testable import CottonUI final class MarkdownTextStripTests: XCTestCase { // ── The block markers ───────────────────────────────────────────────── func testKeepsTheWordsInsideAFencedCodeBlockAndDropsTheFence() { XCTAssertEqual(MarkdownText.stripMarkdown("```swift\nlet a = 1\n```"), "let a = 1") } func testDropsAFenceWithNoLanguageTag() { XCTAssertEqual(MarkdownText.stripMarkdown("```\nplain\n```"), "plain") } func testStripsHeadingMarkersAtEveryLevel() { XCTAssertEqual( MarkdownText.stripMarkdown("# One\n## Two\n###### Six"), "One\nTwo\nSix" ) } func testStripsHeadingsOnLinesOtherThanTheFirst() { // The rule is anchored per LINE. Without `.anchorsMatchLines` only the // first heading in a note would lose its hashes. XCTAssertEqual(MarkdownText.stripMarkdown("Opening\n## Later"), "Opening\nLater") } func testStripsBlockquoteMarkers() { XCTAssertEqual(MarkdownText.stripMarkdown("> quoted\n>tight"), "quoted\ntight") } func testStripsEveryListMarkerShape() { XCTAssertEqual( MarkdownText.stripMarkdown("- dash\n* star\n+ plus\n1. numbered"), "• dash\n• star\n• plus\n1. numbered" ) } func testStripsAnIndentedListMarker() { XCTAssertEqual(MarkdownText.stripMarkdown(" - indented"), "• indented") } func testDropsAHorizontalRuleOnItsOwnLine() { XCTAssertEqual(MarkdownText.stripMarkdown("above\n---\nbelow"), "above\nbelow") XCTAssertEqual(MarkdownText.stripMarkdown("above\n***\nbelow"), "above\nbelow") XCTAssertEqual(MarkdownText.stripMarkdown("above\n___\nbelow"), "above\nbelow") } // ── The inline markers, and their order ─────────────────────────────── func testReadsAnImageBeforeALinkSoNoStrayBangSurvives() { // Order is load bearing: the link rule would eat `[alt](url)` out of // `![alt](url)` and leave the `!` behind. XCTAssertEqual(MarkdownText.stripMarkdown("![a diagram](x.png)"), "a diagram") } func testKeepsALinksLabelAndDropsItsUrl() { XCTAssertEqual(MarkdownText.stripMarkdown("see [the page](https://x.test)"), "see the page") } func testStripsStrongEmphasisStrikethroughAndInlineCode() { XCTAssertEqual(MarkdownText.stripMarkdown("**bold**"), "bold") XCTAssertEqual(MarkdownText.stripMarkdown("__bold__"), "bold") XCTAssertEqual(MarkdownText.stripMarkdown("*italic*"), "italic") XCTAssertEqual(MarkdownText.stripMarkdown("~~gone~~"), "gone") XCTAssertEqual(MarkdownText.stripMarkdown("`code`"), "code") } // ── Whitespace, and the one rule the phone learned the hard way ─────── func testLineBreaksSurviveTheStrip() { // The bug this rule exists for: flattening a reading list with spaces // fused three titles into one sentence nobody wrote. XCTAssertEqual( MarkdownText.stripMarkdown("The Baron in the Trees\nInvisible Cities\nOn typography"), "The Baron in the Trees\nInvisible Cities\nOn typography" ) } func testDropsBlankLinesButKeepsTheBreaksAroundThem() { XCTAssertEqual(MarkdownText.stripMarkdown("one\n\n\ntwo"), "one\ntwo") } func testCollapsesRunsOfSpacesAndTabsWithinALine() { XCTAssertEqual(MarkdownText.stripMarkdown("one \t two"), "one two") } func testTrimsEachLine() { XCTAssertEqual(MarkdownText.stripMarkdown(" padded \n also "), "padded\nalso") } func testAnEmptyBodyStripsToNothing() { XCTAssertEqual(MarkdownText.stripMarkdown(""), "") XCTAssertEqual(MarkdownText.stripMarkdown(" \n\t\n"), "") } func testExoticMarkupPassesThroughUntouched() { // A preview cleaner, not a parser: honesty over cleverness. XCTAssertEqual(MarkdownText.stripMarkdown("| a | b |"), "| a | b |") } } final class BorrowedTitleTests: XCTestCase { func testANamedNoteKeepsItsName() { XCTAssertEqual(MarkdownText.displayTitle(title: "Groceries", body: "milk"), "Groceries") } func testATitleIsTrimmed() { XCTAssertEqual(MarkdownText.displayTitle(title: " Groceries \n", body: ""), "Groceries") } func testAWhitespaceOnlyTitleIsNoTitleAtAll() { XCTAssertEqual(MarkdownText.displayTitle(title: " ", body: "milk"), "milk") } func testAnUnnamedNoteBorrowsItsFirstLine() { // The bug this rule exists for: a captured note with no title read // "Untitled" while the preview underneath quoted its own first words. XCTAssertEqual( MarkdownText.displayTitle(title: nil, body: "Call the plumber\nask about Tuesday"), "Call the plumber" ) } func testTheBorrowedLineIsStrippedOfMarkdown() { XCTAssertEqual(MarkdownText.displayTitle(title: nil, body: "## **Plans**"), "Plans") } func testNilWhenThereIsGenuinelyNothingToCallIt() { // The caller owns the "Untitled" word — this stays copy-free. XCTAssertNil(MarkdownText.displayTitle(title: nil, body: "")) XCTAssertNil(MarkdownText.displayTitle(title: " ", body: " \n ")) } /// RULE: a borrowed title is the note's first line whole — the column holds the whole line and a head cuts it at a word only when it must, so a long first line is never clipped to a character count (notewords' Swift half, architect 16 Oct 2026). func testALongBorrowedLineIsKeptWhole() { let line = String(repeating: "word ", count: 30).trimmingCharacters(in: .whitespaces) XCTAssertEqual(MarkdownText.displayTitle(title: nil, body: line + "\nthe rest"), line) } func testALongNamedTitleIsNeverTruncated() { let name = String(repeating: "y", count: 120) XCTAssertEqual(MarkdownText.displayTitle(title: name, body: ""), name) } }