Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Add method to apply text transformations in a document
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Feb 21, 2021
1 parent 663f0e0 commit f1575c3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/CommonMark/Core/Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,23 @@ public enum Block: Equatable {
fatalError("Unhandled cmark node '\(node.typeString)'")
}
}

/// Returns a new block created by applying the specified transform to this block's text elements.
public func applyingTransform(_ transform: (String) -> String) -> Block {
switch self {
case let .blockQuote(blocks):
return .blockQuote(blocks.map { $0.applyingTransform(transform) })
case let .list(list):
return .list(list.applyingTransform(transform))
case let .paragraph(inlines):
return .paragraph(inlines.map { $0.applyingTransform(transform) })
case let .heading(inlines, level):
return .heading(
inlines.map { $0.applyingTransform(transform) },
level: level
)
case .code, .html, .thematicBreak:
return self
}
}
}
5 changes: 5 additions & 0 deletions Sources/CommonMark/Core/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public struct Document {
self.init(blocks: content())
}
#endif

/// Returns a new document created by applying the specified transform to this document's text elements.
public func applyingTransform(_ transform: (String) -> String) -> Document {
Document(blocks: blocks.map { $0.applyingTransform(transform) })
}
}

extension Document: Equatable {
Expand Down
26 changes: 26 additions & 0 deletions Sources/CommonMark/Core/Inline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,30 @@ public enum Inline: Equatable {
fatalError("Unhandled cmark node '\(node.typeString)'")
}
}

/// Returns a new inline created by applying the specified transform to this inline's text elements.
public func applyingTransform(_ transform: (String) -> String) -> Inline {
switch self {
case let .text(text):
return .text(transform(text))
case let .emphasis(inlines):
return .emphasis(inlines.map { $0.applyingTransform(transform) })
case let .strong(inlines):
return .strong(inlines.map { $0.applyingTransform(transform) })
case let .link(inlines, url, title):
return .link(
inlines.map { $0.applyingTransform(transform) },
url: url,
title: title
)
case let .image(inlines, url, title):
return .image(
inlines.map { $0.applyingTransform(transform) },
url: url,
title: title
)
case .softBreak, .lineBreak, .code, .html:
return self
}
}
}
5 changes: 5 additions & 0 deletions Sources/CommonMark/Core/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public struct Item: Equatable {
guard case CMARK_NODE_ITEM = node.type else { return nil }
self.init(blocks: node.children.map(Block.init))
}

/// Returns a new list item created by applying the specified transform to this item's text elements.
public func applyingTransform(_ transform: (String) -> String) -> Item {
Item(blocks: blocks.map { $0.applyingTransform(transform) })
}
}

#if swift(>=5.4)
Expand Down
9 changes: 9 additions & 0 deletions Sources/CommonMark/Core/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public struct List: Equatable {
items: node.children.compactMap(Item.init)
)
}

/// Returns a new list created by applying the specified transform to this list's text elements.
public func applyingTransform(_ transform: (String) -> String) -> List {
List(
style: style,
spacing: spacing,
items: items.map { $0.applyingTransform(transform) }
)
}
}

#if swift(>=5.4)
Expand Down
48 changes: 48 additions & 0 deletions Tests/CommonMarkTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,54 @@ final class DocumentTests: XCTestCase {
XCTAssertEqual([], result)
}

func testApplyTransform() {
// given
let text = """
## Try CommonMark
You can try CommonMark here. This dingus is powered by
[commonmark.js](https://github.com/jgm/commonmark.js), the
`JavaScript` reference implementation.
```swift
let a = b
```
1. item one
2. item two
- sublist
- sublist
"""

// when
let result = Document(text).applyingTransform { text in
text.uppercased()
}

// then
XCTAssertEqual(
Document(
"""
## TRY COMMONMARK
YOU CAN TRY COMMONMARK HERE. THIS DINGUS IS POWERED BY
[COMMONMARK.JS](https://github.com/jgm/commonmark.js), THE
`JavaScript` REFERENCE IMPLEMENTATION.
``` swift
let a = b
```
1. ITEM ONE
2. ITEM TWO
- SUBLIST
- SUBLIST
"""
),
result
)
}

func testBlockQuote() {
// given
let text = """
Expand Down

0 comments on commit f1575c3

Please sign in to comment.