-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListRenderer.m
More file actions
70 lines (58 loc) · 2.06 KB
/
ListRenderer.m
File metadata and controls
70 lines (58 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#import "ListRenderer.h"
#import "BlockquoteRenderer.h"
#import "MarkdownASTNode.h"
#import "ParagraphStyleUtils.h"
#import "RenderContext.h"
#import "RendererFactory.h"
#import "StyleConfig.h"
@implementation ListRenderer {
__weak RendererFactory *_rendererFactory;
StyleConfig *_config;
BOOL _isOrdered;
}
- (instancetype)initWithRendererFactory:(RendererFactory *)factory config:(StyleConfig *)config isOrdered:(BOOL)ordered
{
if (self = [super init]) {
_rendererFactory = factory;
_config = config;
_isOrdered = ordered;
}
return self;
}
- (void)renderNode:(MarkdownASTNode *)node into:(NSMutableAttributedString *)output context:(RenderContext *)context
{
if (!context)
return;
const NSInteger prevDepth = context.listDepth;
const ListType prevType = context.listType;
const NSInteger prevNum = context.listItemNumber;
const NSUInteger startLocation = output.length;
NSUInteger contentStart = startLocation;
if (prevDepth == 0) {
// Apply top margin for root-level list
contentStart += applyBlockSpacingBefore(output, startLocation, _config.listStyleMarginTop);
} else if (output.length > 0 && ![output.string hasSuffix:@"\n"]) {
// Ensure nested lists start on a new line
[output appendAttributedString:kNewlineAttributedString];
}
context.listDepth = prevDepth + 1;
context.listType = _isOrdered ? ListTypeOrdered : ListTypeUnordered;
context.listItemNumber = 0; // Reset counter for this specific list level
[context setBlockStyle:_isOrdered ? BlockTypeOrderedList : BlockTypeUnorderedList
font:_config.listStyleFont
color:_config.listStyleColor
headingLevel:0];
@try {
[_rendererFactory renderChildrenOfNode:node into:output context:context];
} @finally {
context.listDepth = prevDepth;
context.listType = prevType;
context.listItemNumber = prevNum;
if (prevDepth == 0) {
[context clearBlockStyle];
// Apply bottom margin for root-level list
applyBlockSpacingAfter(output, _config.listStyleMarginBottom);
}
}
}
@end