Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions App/SwiftMarkItDownApp/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import Foundation
import SwiftMarkItDown
import SwiftUI

struct ContentView: View {
@State private var selectedFormat = DemoFormat.html
@State private var input = DemoFormat.html.sampleInput
@State private var output = ""
@State private var errorMessage: String?

var body: some View {
NavigationStack {
Form {
Section("Input") {
Picker("Format", selection: $selectedFormat) {
ForEach(DemoFormat.allCases) { format in
Text(format.label).tag(format)
}
}
.pickerStyle(.segmented)
.onChange(of: selectedFormat) { newValue in
input = newValue.sampleInput
output = ""
errorMessage = nil
}

TextEditor(text: $input)
.font(.system(.body, design: .monospaced))
.frame(minHeight: 180)
.accessibilityIdentifier("conversionInput")
}

Section {
Button("Convert to Markdown", action: convert)
.buttonStyle(.borderedProminent)
.accessibilityIdentifier("convertButton")
}

if let errorMessage {
Section("Error") {
Text(errorMessage)
.foregroundStyle(.red)
}
}

Section("Markdown Output") {
TextEditor(text: .constant(output))
.font(.system(.body, design: .monospaced))
.frame(minHeight: 180)
.accessibilityIdentifier("conversionOutput")
}
}
.navigationTitle("SwiftMarkItDown")
}
}

private func convert() {
do {
let request = ConversionRequest(
data: Data(input.utf8),
fileName: "sample.\(selectedFormat.fileExtension)",
formatHint: selectedFormat.documentFormat
)
output = try MarkItDown().convert(request).markdown
errorMessage = nil
} catch {
output = ""
errorMessage = error.localizedDescription
}
}
}

private enum DemoFormat: String, CaseIterable, Identifiable {
case plainText
case html
case csv
case json

var id: String { rawValue }

var label: String {
switch self {
case .plainText: "Text"
case .html: "HTML"
case .csv: "CSV"
case .json: "JSON"
}
}

var fileExtension: String {
switch self {
case .plainText: "txt"
case .html: "html"
case .csv: "csv"
case .json: "json"
}
}

var documentFormat: DocumentFormat {
switch self {
case .plainText: .plainText
case .html: .html
case .csv: .csv
case .json: .json
}
}

var sampleInput: String {
switch self {
case .plainText:
"""
Hello SwiftMarkItDown
This text is passed through as Markdown.
"""
case .html:
"""
<html>
<head><title>Ignored title</title></head>
<body>
<h1>Hello from iOS</h1>
<p>Convert <strong>native Swift</strong> content.</p>
<a href="https://example.com">Example</a>
</body>
</html>
"""
case .csv:
"""
Name,Note
Swift,Native
"Mark, It Down","CSV | escaped"
"""
case .json:
#"{"title":"Roadmap","formats":["txt","html","csv","json"]}"#
}
}
}

#Preview {
ContentView()
}
10 changes: 10 additions & 0 deletions App/SwiftMarkItDownApp/SwiftMarkItDownApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SwiftUI

@main
struct SwiftMarkItDownDemoApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SwiftMarkItDown

SwiftMarkItDown is the start of a native Swift/iOS document-to-Markdown pipeline inspired by Microsoft MarkItDown. The repository is structured as a Swift Package so the same core can be embedded in an iOS app, a macOS utility, or a server-side Swift service.
SwiftMarkItDown is the start of a native Swift/iOS document-to-Markdown pipeline inspired by Microsoft MarkItDown. The repository is structured around a Swift Package so the same core can be embedded in an iOS app, a macOS utility, or a server-side Swift service. The repo also includes a minimal SwiftUI demo app that exercises the package on iOS.

## Current scope

Expand All @@ -17,11 +17,16 @@ PDF, DOCX, PPTX, and XLSX are intentionally represented in the format model but

```text
Package.swift
SwiftMarkItDownApp.xcodeproj/ Xcode project for the iOS demo app
App/
SwiftMarkItDownApp/ SwiftUI app target that imports the package
Sources/
SwiftMarkItDown/ Core library and converter protocols
swift-markitdown/ Minimal CLI wrapper around the library
SwiftMarkItDown/ Core library and converter protocols
swift-markitdown/ Minimal CLI wrapper around the library
Tests/
SwiftMarkItDownTests/ Core conversion tests
SwiftMarkItDownTests/ Core conversion tests
Fixtures/ CLI smoke-test inputs
Expected/ CLI smoke-test expected Markdown
```

## Library usage
Expand Down
Loading
Loading