Skip to content

Commit

Permalink
Provide access to the list delimiter (johnxnguyen#275)
Browse files Browse the repository at this point in the history
* Provide access to the list delimiter

* Fix test
  • Loading branch information
5sw authored Oct 18, 2021
1 parent 3b07bb5 commit e754ab1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
32 changes: 31 additions & 1 deletion Sources/Down/AST/Nodes/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,29 @@ public class List: BaseNode {

public lazy var isTight: Bool = cmark_node_get_list_tight(cmarkNode) == 1

/// The list delimiter.

public lazy var delimiter: Delimiter? = Delimiter(cmarkNode.listDelimiter)
}

// MARK: - List Type

public extension List {

enum Delimiter {
case period
case paren

init?(_ cmark: cmark_delim_type) {
switch cmark {
case CMARK_NO_DELIM: return nil
case CMARK_PERIOD_DELIM: self = .period
case CMARK_PAREN_DELIM: self = .paren
default: preconditionFailure("Invalid delim type")
}
}
}

enum ListType: CustomDebugStringConvertible {
case bullet
case ordered(start: Int)
Expand Down Expand Up @@ -71,7 +88,20 @@ public extension List {
extension List: CustomDebugStringConvertible {

public var debugDescription: String {
return "List - type: \(listType), isTight: \(isTight)"
var result = "List - type: \(listType), isTight: \(isTight)"
if let delim = delimiter {
result += ", delimiter: \(delim)"
}
return result
}

}

extension List.Delimiter: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .paren: return "paren"
case .period: return "period"
}
}
}
4 changes: 4 additions & 0 deletions Sources/Down/AST/Nodes/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public extension CMarkNode {
return Int(cmark_node_get_list_start(self))
}

var listDelimiter: cmark_delim_type {
return cmark_node_get_list_delim(self)
}

var url: String? {
return String(cString: cmark_node_get_url(self))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Document
↳ Paragraph
↳ Text - Text text.
↳ List - type: Ordered (start: 3), isTight: true
↳ List - type: Ordered (start: 3), isTight: true, delimiter: period
↳ Item
↳ Paragraph
↳ Text - One
Expand Down

0 comments on commit e754ab1

Please sign in to comment.