Skip to content

store ordered list start index from cmark #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 5, 2022
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
6 changes: 3 additions & 3 deletions Sources/Markdown/Base/RawMarkup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum RawMarkupData: Equatable {
case thematicBreak
case htmlBlock(String)
case listItem(checkbox: Checkbox?)
case orderedList
case orderedList(startIndex: UInt = 1)
case unorderedList
case paragraph
case blockDirective(name: String, nameLocation: SourceLocation?, arguments: DirectiveArgumentText)
Expand Down Expand Up @@ -228,8 +228,8 @@ final class RawMarkup: ManagedBuffer<RawMarkupHeader, RawMarkup> {
return .create(data: .listItem(checkbox: checkbox), parsedRange: parsedRange, children: children)
}

static func orderedList(parsedRange: SourceRange?, _ children: [RawMarkup]) -> RawMarkup {
return .create(data: .orderedList, parsedRange: parsedRange, children: children)
static func orderedList(parsedRange: SourceRange?, _ children: [RawMarkup], startIndex: UInt = 1) -> RawMarkup {
return .create(data: .orderedList(startIndex: startIndex), parsedRange: parsedRange, children: children)
}

static func unorderedList(parsedRange: SourceRange?, _ children: [RawMarkup]) -> RawMarkup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ public extension OrderedList {
try! self.init(.orderedList(parsedRange: nil, items.map { $0.raw.markup }))
}

/// The starting index for the list.
///
/// The default starting index in CommonMark is 1. In this case, clients may use the default
/// ordered-list start index of their desired rendering format. For example, when rendering to
/// HTML, clients may omit the `start` attribute of the rendered list when this returns 1.
var startIndex: UInt {
get {
guard case let .orderedList(start) = _data.raw.markup.data else {
fatalError("\(self) markup wrapped unexpected \(_data.raw)")
}
return start
}
set {
guard startIndex != newValue else {
return
}
_data = _data.replacingSelf(.orderedList(parsedRange: nil, _data.raw.markup.copyChildren(), startIndex: newValue))
}
}

// MARK: Visitation

func accept<V: MarkupVisitor>(_ visitor: inout V) -> V.Result {
Expand Down
3 changes: 2 additions & 1 deletion Sources/Markdown/Parser/CommonMarkConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ struct MarkupParser {
case CMARK_BULLET_LIST:
return MarkupConversion(state: childConversion.state.next(), result: .unorderedList(parsedRange: parsedRange, childConversion.result))
case CMARK_ORDERED_LIST:
return MarkupConversion(state: childConversion.state.next(), result: .orderedList(parsedRange: parsedRange, childConversion.result))
let cmarkStart = UInt(cmark_node_get_list_start(state.node))
return MarkupConversion(state: childConversion.state.next(), result: .orderedList(parsedRange: parsedRange, childConversion.result, startIndex: cmarkStart))
default:
fatalError("cmark reported a list node but said its list type is CMARK_NO_LIST?")
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Markdown/Walker/Walkers/MarkupFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ public struct MarkupFormatter: MarkupWalker {
return nil
}
let numeral: UInt
// FIXME: allow `orderedListNumerals` to defer to the user-authored starting index (#76, rdar://99970544)
switch formattingOptions.orderedListNumerals {
case let .allSame(n):
numeral = n
Expand Down
8 changes: 8 additions & 0 deletions Sources/Markdown/Walker/Walkers/MarkupTreeDumper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ struct MarkupTreeDumper: MarkupWalker {
dump(heading, customDescription: "level: \(heading.level)")
}

mutating func visitOrderedList(_ orderedList: OrderedList) {
if orderedList.startIndex != 1 {
dump(orderedList, customDescription: "startIndex: \(orderedList.startIndex)")
} else {
defaultVisit(orderedList)
}
}

mutating func visitCodeBlock(_ codeBlock: CodeBlock) {
let lines = indentLiteralBlock(codeBlock.code, from: codeBlock, countLines: false)
dump(codeBlock, customDescription: "language: \(codeBlock.language ?? "none")\n\(lines)")
Expand Down
3 changes: 3 additions & 0 deletions Tests/MarkdownTests/Visitors/Everything.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

> BlockQuote

2. flour
2. sugar

```swift
func foo() {
let x = 1
Expand Down
37 changes: 32 additions & 5 deletions Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ class MarkupFormatterSingleElementTests: XCTestCase {
}
}

func testPrintOrderedListCustomStart() {
let options = MarkupFormatter.Options(orderedListNumerals: .allSame(2))
do { // no checkbox
let expected = "2. A list item."
var renderedList = OrderedList(ListItem(Paragraph(Text("A list item."))))
renderedList.startIndex = 2
let printed = renderedList.format(options: options)
XCTAssertEqual(expected, printed)
}
do { // unchecked
let expected = "2. [ ] A list item."
var renderedList = OrderedList(ListItem(checkbox: .unchecked,
Paragraph(Text("A list item."))))
renderedList.startIndex = 2
let printed = renderedList.format(options: options)
XCTAssertEqual(expected, printed)
}
do { // checked
let expected = "2. [x] A list item."
var renderedList = OrderedList(ListItem(checkbox: .checked,
Paragraph(Text("A list item."))))
renderedList.startIndex = 2
let printed = renderedList.format(options: options)
XCTAssertEqual(expected, printed)
}
}

func testPrintParagraph() {
let expected = "A paragraph."
let printed = Paragraph(Text("A paragraph.")).format()
Expand Down Expand Up @@ -426,13 +453,13 @@ class MarkupFormatterOptionsTests: XCTestCase {
3. C
"""
let allSame = """
0. A
0. B
0. C
1. A
1. B
1. C
"""
do {
let document = Document(parsing: incrementing)
let printed = document.format(options: .init(orderedListNumerals: .allSame(0)))
let printed = document.format(options: .init(orderedListNumerals: .allSame(1)))
XCTAssertEqual(allSame, printed)
}

Expand Down Expand Up @@ -917,7 +944,7 @@ class MarkupFormatterLineSplittingTests: XCTestCase {

let expectedTreeDump = """
Document
└─ OrderedList
└─ OrderedList startIndex: 1000
└─ ListItem
└─ Paragraph
├─ Text "Really really"
Expand Down
57 changes: 32 additions & 25 deletions Tests/MarkdownTests/Visitors/MarkupTreeDumperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest
final class MarkupTreeDumperTests: XCTestCase {
func testDumpEverything() {
let expectedDump = """
Document @1:1-39:90 Root #\(everythingDocument.raw.metadata.id.rootId) #0
Document @1:1-42:90 Root #\(everythingDocument.raw.metadata.id.rootId) #0
├─ Heading @1:1-1:9 #1 level: 1
│ └─ Text @1:3-1:9 #2 "Header"
├─ Paragraph @3:1-3:65 #3
Expand Down Expand Up @@ -55,37 +55,44 @@ final class MarkupTreeDumperTests: XCTestCase {
├─ BlockQuote @13:1-13:13 #38
│ └─ Paragraph @13:3-13:13 #39
│ └─ Text @13:3-13:13 #40 "BlockQuote"
├─ CodeBlock @15:1-19:4 #41 language: swift
├─ OrderedList @15:1-17:1 #41 startIndex: 2
│ ├─ ListItem @15:1-15:9 #42
│ │ └─ Paragraph @15:4-15:9 #43
│ │ └─ Text @15:4-15:9 #44 "flour"
│ └─ ListItem @16:1-17:1 #45
│ └─ Paragraph @16:4-16:9 #46
│ └─ Text @16:4-16:9 #47 "sugar"
├─ CodeBlock @18:1-22:4 #48 language: swift
│ func foo() {
│ let x = 1
│ }
├─ CodeBlock @21:5-22:1 #42 language: none
├─ CodeBlock @24:5-25:1 #49 language: none
│ // Is this real code? Or just fantasy?
├─ Paragraph @23:1-23:31 #43
│ ├─ Text @23:1-23:12 #44 "This is an "
│ ├─ Link @23:12-23:30 #45 destination: "topic://autolink"
│ │ └─ Text @23:13-23:29 #46 "topic://autolink"
│ └─ Text @23:30-23:31 #47 "."
├─ ThematicBreak @25:1-26:1 #48
├─ HTMLBlock @27:1-29:5 #49
├─ Paragraph @26:1-26:31 #50
│ ├─ Text @26:1-26:12 #51 "This is an "
│ ├─ Link @26:12-26:30 #52 destination: "topic://autolink"
│ │ └─ Text @26:13-26:29 #53 "topic://autolink"
│ └─ Text @26:30-26:31 #54 "."
├─ ThematicBreak @28:1-29:1 #55
├─ HTMLBlock @30:1-32:5 #56
│ <a href="foo.png">
│ An HTML Block.
│ </a>
├─ Paragraph @31:1-31:33 #50
│ ├─ Text @31:1-31:14 #51 "This is some "
│ ├─ InlineHTML @31:14-31:17 #52 <p>
│ ├─ Text @31:17-31:28 #53 "inline html"
│ ├─ InlineHTML @31:28-31:32 #54 </p>
│ └─ Text @31:32-31:33 #55 "."
├─ Paragraph @33:1-34:6 #56
│ ├─ Text @33:1-33:7 #57 "line"
│ ├─ LineBreak #58
│ └─ Text @34:1-34:6 #59 "break"
├─ Paragraph @36:1-37:6 #60
│ ├─ Text @36:1-36:5 #61 "soft"
│ ├─ SoftBreak #62
│ └─ Text @37:1-37:6 #63 "break"
└─ HTMLBlock @39:1-39:90 #64
├─ Paragraph @34:1-34:33 #57
│ ├─ Text @34:1-34:14 #58 "This is some "
│ ├─ InlineHTML @34:14-34:17 #59 <p>
│ ├─ Text @34:17-34:28 #60 "inline html"
│ ├─ InlineHTML @34:28-34:32 #61 </p>
│ └─ Text @34:32-34:33 #62 "."
├─ Paragraph @36:1-37:6 #63
│ ├─ Text @36:1-36:7 #64 "line"
│ ├─ LineBreak #65
│ └─ Text @37:1-37:6 #66 "break"
├─ Paragraph @39:1-40:6 #67
│ ├─ Text @39:1-39:5 #68 "soft"
│ ├─ SoftBreak #69
│ └─ Text @40:1-40:6 #70 "break"
└─ HTMLBlock @42:1-42:90 #71
<!-- Copyright (c) 2021 Apple Inc and the Swift Project authors. All Rights Reserved. -->
"""
print(everythingDocument.debugDescription(options: [.printEverything]))
Expand Down