A Swift 6.2 port of Mozilla's Readability.js using SwiftSoup for DOM parsing.
- Swift 6.2
- iOS 15+, macOS 13+
Add the package dependency:
.package(url: "https://github.com/lake-of-fire/swift-readability", from: "0.1.0")
Then add SwiftReadability to your target dependencies.
Basic extraction:
import SwiftReadability
let reader = Readability(
html: htmlString,
url: URL(string: "https://example.com")!
)
let result = try reader.parse()
print(result?.title ?? "(no title)")
print(result?.contentHTML ?? "(no content)")Custom serialization (mirrors Readability.js serializer):
let result = try reader.parse { element in
// Return any type you want from the article content element.
element
}Readerable check:
let readerable = Readability.isProbablyReaderable(html: htmlString)ReadabilityOptions mirrors Readability.js:
debug(Bool)maxElemsToParse(Int)nbTopCandidates(Int)charThreshold(Int)classesToPreserve([String])keepClasses(Bool)serializer((Element) -> String)useXMLSerializer(Bool, defaults tofalsefor parity with Readability.js). The test suite enables XML serialization explicitly to match Mozilla's fixture expectations for boolean attributes.disableJSONLD(Bool)allowedVideoRegex(NSRegularExpression)linkDensityModifier(Double)
- Fixture parity: the fixture tests enable
useXMLSerializer: trueto preserve boolean attribute values (Mozilla fixtures expectitemscope="itemscope"). - Live DOM:
Readability(document:)operates directly on the passedDocument(no reparse), matching JS behavior and improving performance. - Serializer parity: default output uses HTML serialization (JS
innerHTML), while XML serialization is available viauseXMLSerializer.
The test suite ports Mozilla's test-readability.js and test-isProbablyReaderable.js,
including the full fixture corpus under Tests/SwiftReadabilityTests/Fixtures.
Ported against @mozilla/readability v0.6.0 (Readability.js) to keep behavior and fixtures aligned.
Run all tests:
swift test -q -Xswiftc -suppress-warnings
Filter fixtures:
SWIFT_READABILITY_FIXTURES=nytimes-3 swift test -q -Xswiftc -suppress-warnings
Run the local benchmark harness (fixtures-based):
swift run SwiftReadabilityBench --iterations 5 --warmup 1