@@ -139,10 +139,24 @@ public enum RenderBlockContent: Equatable {
139
139
public struct CodeBlockOptions : Equatable {
140
140
public var language : String ?
141
141
public var copyToClipboard : Bool
142
- public var wrap : Int
143
- public var highlight : [ Int ]
144
142
public var showLineNumbers : Bool
145
- public var strikeout : [ Int ]
143
+ public var wrap : Int
144
+ public var lineAnnotations : [ LineAnnotation ]
145
+
146
+ public struct Position : Equatable , Codable {
147
+ var line : Int
148
+ var character : Int ?
149
+ }
150
+
151
+ public struct Range : Equatable , Codable {
152
+ var start : Position
153
+ var end : Position
154
+ }
155
+
156
+ public struct LineAnnotation : Equatable , Codable {
157
+ var style : String
158
+ var range : Range
159
+ }
146
160
147
161
public enum OptionName : String , CaseIterable {
148
162
case _nonFrozenEnum_useDefaultCase
@@ -166,10 +180,9 @@ public enum RenderBlockContent: Equatable {
166
180
public init ( ) {
167
181
self . language = " "
168
182
self . copyToClipboard = true
169
- self . wrap = 0
170
- self . highlight = [ ]
171
183
self . showLineNumbers = false
172
- self . strikeout = [ ]
184
+ self . wrap = 0
185
+ self . lineAnnotations = [ ]
173
186
}
174
187
175
188
public init ( parsingLanguageString language: String ? ) {
@@ -186,27 +199,53 @@ public enum RenderBlockContent: Equatable {
186
199
self . wrap = 0
187
200
}
188
201
189
- if let highlightString = tokens. first ( where: { $0. name == . highlight } ) ? . value,
190
- let highlightValue = Self . parseCodeBlockOptionsArray ( highlightString) {
191
- self . highlight = highlightValue
192
- } else {
193
- self . highlight = [ ]
202
+ var annotations : [ LineAnnotation ] = [ ]
203
+
204
+ if let highlightString = tokens. first ( where: { $0. name == . highlight } ) ? . value {
205
+ let highlightValue = Self . parseCodeBlockOptionsArray ( highlightString)
206
+ for line in highlightValue {
207
+ let pos = Position ( line: line, character: nil )
208
+ let range = Range ( start: pos, end: pos)
209
+ annotations. append ( LineAnnotation ( style: " highlight " , range: range) )
210
+ }
194
211
}
195
212
196
- if let strikeoutString = tokens. first ( where: { $0. name == . strikeout } ) ? . value,
197
- let strikeoutValue = Self . parseCodeBlockOptionsArray ( strikeoutString) {
198
- self . strikeout = strikeoutValue
199
- } else {
200
- self . strikeout = [ ]
213
+ if let strikeoutString = tokens. first ( where: { $0. name == . strikeout } ) ? . value {
214
+ let strikeoutValue = Self . parseCodeBlockOptionsArray ( strikeoutString)
215
+ for line in strikeoutValue {
216
+ let pos = Position ( line: line, character: nil )
217
+ let range = Range ( start: pos, end: pos)
218
+ annotations. append ( LineAnnotation ( style: " strikeout " , range: range) )
219
+ }
201
220
}
221
+
222
+ self . lineAnnotations = annotations
202
223
}
203
224
204
- public init ( copyToClipboard: Bool , wrap: Int , highlight: [ Int ] , strikeout: [ Int ] , showLineNumbers : Bool ) {
225
+ public init ( copyToClipboard: Bool , showLineNumbers : Bool , wrap: Int , highlight: [ Int ] , strikeout: [ Int ] ) {
205
226
self . copyToClipboard = copyToClipboard
227
+ self . showLineNumbers = showLineNumbers
206
228
self . wrap = wrap
207
- self . highlight = highlight
229
+
230
+ var annotations : [ LineAnnotation ] = [ ]
231
+ for line in highlight {
232
+ let pos = Position ( line: line, character: nil )
233
+ let range = Range ( start: pos, end: pos)
234
+ annotations. append ( LineAnnotation ( style: " highlight " , range: range) )
235
+ }
236
+ for line in strikeout {
237
+ let pos = Position ( line: line, character: nil )
238
+ let range = Range ( start: pos, end: pos)
239
+ annotations. append ( LineAnnotation ( style: " strikeout " , range: range) )
240
+ }
241
+ self . lineAnnotations = annotations
242
+ }
243
+
244
+ public init ( copyToClipboard: Bool , showLineNumbers: Bool , wrap: Int , lineAnnotations: [ LineAnnotation ] ) {
245
+ self . copyToClipboard = copyToClipboard
208
246
self . showLineNumbers = showLineNumbers
209
- self . strikeout = strikeout
247
+ self . wrap = wrap
248
+ self . lineAnnotations = lineAnnotations
210
249
}
211
250
212
251
/// A function that parses array values on code block options from the language line string
@@ -863,7 +902,7 @@ extension RenderBlockContent.Table: Codable {
863
902
extension RenderBlockContent : Codable {
864
903
private enum CodingKeys : CodingKey {
865
904
case type
866
- case inlineContent, content, caption, style, name, syntax, code, level, text, items, media, runtimePreview, anchor, summary, example, metadata, start, copyToClipboard, wrap , highlight , strikeout , showLineNumbers
905
+ case inlineContent, content, caption, style, name, syntax, code, level, text, items, media, runtimePreview, anchor, summary, example, metadata, start, copyToClipboard, showLineNumbers , wrap , lineAnnotations
867
906
case request, response
868
907
case header, rows
869
908
case numberOfColumns, columns
@@ -887,13 +926,12 @@ extension RenderBlockContent: Codable {
887
926
case . codeListing:
888
927
let copy = FeatureFlags . current. isExperimentalCodeBlockAnnotationsEnabled
889
928
let options : CodeBlockOptions ?
890
- if !Set( container. allKeys) . isDisjoint ( with: [ . copyToClipboard, . wrap , . highlight , . strikeout , . showLineNumbers ] ) {
929
+ if !Set( container. allKeys) . isDisjoint ( with: [ . copyToClipboard, . showLineNumbers , . wrap , . lineAnnotations ] ) {
891
930
options = try CodeBlockOptions (
892
931
copyToClipboard: container. decodeIfPresent ( Bool . self, forKey: . copyToClipboard) ?? copy,
932
+ showLineNumbers: container. decodeIfPresent ( Bool . self, forKey: . showLineNumbers) ?? false ,
893
933
wrap: container. decodeIfPresent ( Int . self, forKey: . wrap) ?? 0 ,
894
- highlight: container. decodeIfPresent ( [ Int ] . self, forKey: . highlight) ?? [ ] ,
895
- strikeout: container. decodeIfPresent ( [ Int ] . self, forKey: . strikeout) ?? [ ] ,
896
- showLineNumbers: container. decodeIfPresent ( Bool . self, forKey: . showLineNumbers) ?? false
934
+ lineAnnotations: container. decodeIfPresent ( [ CodeBlockOptions . LineAnnotation ] . self, forKey: . lineAnnotations) ?? [ ]
897
935
)
898
936
} else {
899
937
options = nil
@@ -1007,10 +1045,9 @@ extension RenderBlockContent: Codable {
1007
1045
try container. encode ( l. code, forKey: . code)
1008
1046
try container. encodeIfPresent ( l. metadata, forKey: . metadata)
1009
1047
try container. encodeIfPresent ( l. options? . copyToClipboard, forKey: . copyToClipboard)
1010
- try container. encodeIfPresent ( l. options? . wrap, forKey: . wrap)
1011
- try container. encodeIfPresent ( l. options? . highlight, forKey: . highlight)
1012
- try container. encodeIfPresent ( l. options? . strikeout, forKey: . strikeout)
1013
1048
try container. encodeIfPresent ( l. options? . showLineNumbers, forKey: . showLineNumbers)
1049
+ try container. encodeIfPresent ( l. options? . wrap, forKey: . wrap)
1050
+ try container. encodeIfPresent ( l. options? . lineAnnotations, forKey: . lineAnnotations)
1014
1051
case . heading( let h) :
1015
1052
try container. encode ( h. level, forKey: . level)
1016
1053
try container. encode ( h. text, forKey: . text)
0 commit comments