Skip to content

save custom ordered list start index in render JSON #53

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 2 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
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions Sources/SwiftDocC/Model/Rendering/Content/RenderBlockContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,13 @@ public enum RenderBlockContent: Equatable {
public struct OrderedList: Equatable {
/// The items in this list.
public var items: [ListItem]
/// The starting index for items in this list.
public var startIndex: UInt

/// Creates a new ordered list with the given items.
public init(items: [ListItem]) {
public init(items: [ListItem], startIndex: UInt = 1) {
self.items = items
self.startIndex = startIndex
}
}

Expand Down Expand Up @@ -693,7 +696,7 @@ extension RenderBlockContent.Table: Codable {
extension RenderBlockContent: Codable {
private enum CodingKeys: CodingKey {
case type
case inlineContent, content, caption, style, name, syntax, code, level, text, items, media, runtimePreview, anchor, summary, example, metadata
case inlineContent, content, caption, style, name, syntax, code, level, text, items, media, runtimePreview, anchor, summary, example, metadata, start
case request, response
case header, rows
case numberOfColumns, columns
Expand Down Expand Up @@ -723,7 +726,10 @@ extension RenderBlockContent: Codable {
case .heading:
self = try .heading(.init(level: container.decode(Int.self, forKey: .level), text: container.decode(String.self, forKey: .text), anchor: container.decodeIfPresent(String.self, forKey: .anchor)))
case .orderedList:
self = try .orderedList(.init(items: container.decode([ListItem].self, forKey: .items)))
self = try .orderedList(.init(
items: container.decode([ListItem].self, forKey: .items),
startIndex: container.decodeIfPresent(UInt.self, forKey: .start) ?? 1
))
case .unorderedList:
self = try .unorderedList(.init(items: container.decode([ListItem].self, forKey: .items)))
case .step:
Expand Down Expand Up @@ -821,6 +827,9 @@ extension RenderBlockContent: Codable {
try container.encode(h.text, forKey: .text)
try container.encode(h.anchor, forKey: .anchor)
case .orderedList(let l):
if l.startIndex != 1 {
try container.encode(l.startIndex, forKey: .start)
}
try container.encode(l.items, forKey: .items)
case .unorderedList(let l):
try container.encode(l.items, forKey: .items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ struct RenderContentCompiler: MarkupVisitor {

mutating func visitOrderedList(_ orderedList: OrderedList) -> [RenderContent] {
let renderListItems = orderedList.listItems.reduce(into: [], { result, item in result.append(contentsOf: visitListItem(item))})
return [RenderBlockContent.orderedList(.init(items: renderListItems as! [RenderBlockContent.ListItem]))]
return [RenderBlockContent.orderedList(.init(
items: renderListItems as! [RenderBlockContent.ListItem],
startIndex: orderedList.startIndex
))]
}

mutating func visitUnorderedList(_ unorderedList: UnorderedList) -> [RenderContent] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@
"orderedList"
]
},
"start": {
"type": "integer"
},
"items": {
"type": "array",
"items": {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftDocCTests/Indexing/IndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class IndexingTests: XCTestCase {
title: "MyProtocol",
summary: "An abstract of a protocol using a String id value.",
headings: ["Return Value", "Discussion"],
rawIndexableTextContent: "An abstract of a protocol using a String id value. A name of the item to find. Return Value A String id value. Discussion Further discussion. Exercise links to symbols: relative MyClass and absolute MyClass. Exercise unresolved symbols: unresolved MyUnresolvedSymbol. Exercise known unresolvable symbols: know unresolvable NSCodable. Exercise external references: doc://com.test.external/ExternalPage One ordered Two ordered Three ordered One unordered Two unordered Three unordered",
rawIndexableTextContent: "An abstract of a protocol using a String id value. A name of the item to find. Return Value A String id value. Discussion Further discussion. Exercise links to symbols: relative MyClass and absolute MyClass. Exercise unresolved symbols: unresolved MyUnresolvedSymbol. Exercise known unresolvable symbols: know unresolvable NSCodable. Exercise external references: doc://com.test.external/ExternalPage One ordered Two ordered Three ordered One unordered Two unordered Three unordered Two ordered with custom start Three ordered with custom start Four ordered with custom start",
platforms: expectedPlatformInformation),
indexingRecords[0])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,17 @@ class DocumentationContextTests: XCTestCase {
└─ ListItem
└─ Paragraph
└─ Text "Three unordered"

OrderedList startIndex: 2
├─ ListItem
│ └─ Paragraph
│ └─ Text "Two ordered with custom start"
├─ ListItem
│ └─ Paragraph
│ └─ Text "Three ordered with custom start"
└─ ListItem
└─ Paragraph
└─ Text "Four ordered with custom start"
""")

XCTAssertEqual(myProtocolSymbol.declaration.values.first?.declarationFragments.map({ $0.spelling }), ["protocol", " ", "MyProtocol", " : ", "Hashable"])
Expand Down
15 changes: 15 additions & 0 deletions Tests/SwiftDocCTests/Rendering/RenderNodeTranslatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class RenderNodeTranslatorTests: XCTestCase {

XCTAssert(discussion.content.contains(where: { block in
if case .orderedList(let l) = block,
l.startIndex == 1,
l.items.count == 3,
l.items[0].content.first == .paragraph(.init(inlineContent: [.text("One ordered")])),
l.items[1].content.first == .paragraph(.init(inlineContent: [.text("Two ordered")])),
Expand All @@ -126,6 +127,20 @@ class RenderNodeTranslatorTests: XCTestCase {
return false
}
}))

XCTAssert(discussion.content.contains(where: { block in
if case .orderedList(let l) = block,
l.startIndex == 2,
l.items.count == 3,
l.items[0].content.first == .paragraph(.init(inlineContent: [.text("Two ordered with custom start")])),
l.items[1].content.first == .paragraph(.init(inlineContent: [.text("Three ordered with custom start")])),
l.items[2].content.first == .paragraph(.init(inlineContent: [.text("Four ordered with custom start")]))
{
return true
} else {
return false
}
}))
}

func testAutomaticOverviewAndDiscussionHeadings() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Exercise external references: <doc://com.test.external/ExternalPage>
- Two unordered
- Three unordered

2. Two ordered with custom start
3. Three ordered with custom start
4. Four ordered with custom start

- Returns: A `String` id value.

- Parameter input: A name of the item to find.
Expand Down