-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplug_tasks.go
More file actions
704 lines (621 loc) · 23.7 KB
/
Copy pathplug_tasks.go
File metadata and controls
704 lines (621 loc) · 23.7 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
package obsidian
import (
"fmt"
"maps"
"math"
"regexp"
"strings"
"time"
"unicode/utf8"
"github.com/yuin/goldmark"
gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"github.com/powerman/goldmark-obsidian/obsast"
)
// Default attributes used by [NewPlugTasksParser].
const (
DefaultPlugTasksListClass = "contains-task-list"
DefaultPlugTasksListItemNotCheckedClass = "task-list-item"
DefaultPlugTasksListItemCheckedClass = "task-list-item is-checked"
DefaultPlugTasksListItemStatusAttr = "data-task"
DefaultPlugTasksCheckboxClass = "task-list-item-checkbox"
)
// Task status symbols used by [NewPlugTasksParser] by default.
// Any other (unknown) symbols will have status TODO.
//
// NOTE: Upper case 'X' is unknown (i.e. has TODO status) in default Tasks configuration.
const (
DefaultPlugTasksTODOSymbol = ' '
DefaultPlugTasksInProgressSymbol = '/'
DefaultPlugTasksDoneSymbol = 'x'
DefaultPlugTasksCancelledSymbol = '-'
)
type plugTasksConfig struct {
StatusType map[rune]obsast.PlugTasksStatusType
ListClass []byte
ListItemNotCheckedClass []byte
ListItemCheckedClass []byte
ListItemStatusAttr []byte
CheckboxClass []byte
}
func newPlugTasksConfig() plugTasksConfig {
return plugTasksConfig{
StatusType: map[rune]obsast.PlugTasksStatusType{
DefaultPlugTasksTODOSymbol: obsast.PlugTasksStatusTypeTODO,
DefaultPlugTasksInProgressSymbol: obsast.PlugTasksStatusTypeInProgress,
DefaultPlugTasksDoneSymbol: obsast.PlugTasksStatusTypeDone,
DefaultPlugTasksCancelledSymbol: obsast.PlugTasksStatusTypeCancelled,
},
ListClass: []byte(DefaultPlugTasksListClass),
ListItemNotCheckedClass: []byte(DefaultPlugTasksListItemNotCheckedClass),
ListItemCheckedClass: []byte(DefaultPlugTasksListItemCheckedClass),
ListItemStatusAttr: []byte(DefaultPlugTasksListItemStatusAttr),
CheckboxClass: []byte(DefaultPlugTasksCheckboxClass),
}
}
// A PlugTasksOption configures a [PlugTasksParser].
type PlugTasksOption func(*plugTasksConfig)
// WithPlugTasksStatusType sets a statusType for a symbol.
// If you need to set many symbols then using [WithPlugTasksStatusTypes] may be more
// convenient than multiple calls to WithPlugTasksStatusType.
//
// To "disable" one of default symbols set it statusType to ast.PlugTasksStatusTypeTODO,
// which is used by default for all unknown symbols.
func WithPlugTasksStatusType(symbol rune, statusType obsast.PlugTasksStatusType) PlugTasksOption {
return func(config *plugTasksConfig) {
config.StatusType[symbol] = statusType
}
}
// WithPlugTasksStatusTypes sets a statusTypes for a symbols.
//
// To "disable" one of default symbols set it statusType to ast.PlugTasksStatusTypeTODO,
// which is used by default for all unknown symbols.
func WithPlugTasksStatusTypes(statusTypes map[rune]obsast.PlugTasksStatusType) PlugTasksOption {
return func(config *plugTasksConfig) {
maps.Copy(config.StatusType, statusTypes)
}
}
// WithPlugTasksListClass sets a class for a list containing task(s).
func WithPlugTasksListClass[T []byte | string](class T) PlugTasksOption {
return func(config *plugTasksConfig) {
config.ListClass = []byte(class)
}
}
// WithPlugTasksListItemNotCheckedClass sets a class for a list item containing
// a task without status symbol: [ ].
func WithPlugTasksListItemNotCheckedClass[T []byte | string](class T) PlugTasksOption {
return func(config *plugTasksConfig) {
config.ListItemNotCheckedClass = []byte(class)
}
}
// WithPlugTasksListItemCheckedClass sets a class for a list item containing
// a task with any status symbol (i.e. not a space).
func WithPlugTasksListItemCheckedClass[T []byte | string](class T) PlugTasksOption {
return func(config *plugTasksConfig) {
config.ListItemCheckedClass = []byte(class)
}
}
// WithPlugTasksListItemStatusAttr sets a name for a list item attribute which will contain
// a task status symbol (one within [ ]).
// Attribute will be set to empty string if status symbol is a space.
func WithPlugTasksListItemStatusAttr[T []byte | string](name T) PlugTasksOption {
return func(config *plugTasksConfig) {
config.ListItemStatusAttr = []byte(name)
}
}
// WithPlugTasksCheckboxClass sets a class for an <input type=checkbox>.
func WithPlugTasksCheckboxClass[T []byte | string](class T) PlugTasksOption {
return func(config *plugTasksConfig) {
config.CheckboxClass = []byte(class)
}
}
// PlugTasksParser is an Obsidian plugin Tasks's task status parser.
//
// This parser must take precedence over the parser.LinkParser.
type PlugTasksParser struct {
plugTasksConfig
}
// NewPlugTasksParser returns a new PlugTasksParser.
func NewPlugTasksParser(opts ...PlugTasksOption) *PlugTasksParser {
s := &PlugTasksParser{
plugTasksConfig: newPlugTasksConfig(),
}
for _, opt := range opts {
opt(&s.plugTasksConfig)
}
return s
}
// Trigger implements [parser.InlineParser].
func (*PlugTasksParser) Trigger() []byte {
return []byte{'['}
}
var plugTasksStatusRegexp = regexp.MustCompile(`^\[(.)\]\s*`)
// Parse implements [parser.InlineParser].
func (p *PlugTasksParser) Parse(parent gast.Node, block text.Reader, _ parser.Context) gast.Node {
// Given AST structure must be like
// - List
// - ListItem : parent.Parent
// - TextBlock|Paragraph : parent
// (current line)
if parent.Parent() == nil || parent.Parent().FirstChild() != parent {
return nil
}
if parent.HasChildren() {
return nil
}
if _, ok := parent.Parent().(*gast.ListItem); !ok {
return nil
}
line, _ := block.PeekLine()
m := plugTasksStatusRegexp.FindSubmatchIndex(line)
if m == nil {
return nil
}
symbol, _ := utf8.DecodeRune(line[m[2]:m[3]])
block.Advance(m[1])
statusType, ok := p.StatusType[symbol]
if !ok { // https://publish.obsidian.md/tasks/Getting+Started/Statuses#Unknown+Statuses
statusType = obsast.PlugTasksStatusTypeTODO
}
node := obsast.NewPlugTasksStatus(symbol, statusType)
itemNode := parent.Parent()
listNode := itemNode.Parent()
if symbol == ' ' {
itemNode.SetAttribute(p.ListItemStatusAttr, []byte{})
} else {
itemNode.SetAttribute(p.ListItemStatusAttr, []byte(string(symbol)))
}
appendClass(node, p.CheckboxClass)
if node.IsChecked() {
appendClass(itemNode, p.ListItemCheckedClass)
} else {
appendClass(itemNode, p.ListItemNotCheckedClass)
}
appendClass(listNode, p.ListClass)
return node
}
const (
// Plugin Tasks has own definition for Obsidian tags, which differs from Obsidian one:
// - it does not require at least one non-numeric symbol
// - it accepts extra symbols: '+;=[\]`~
plugTasksTagRe = `#[-a-zA-Z0-9/_'+;=[\\\]` + "`" + `~\x{000080}-\x{10FFFF}]+(?:\s+|$)`
plugTasksBlockIDRe = `\^[A-Za-z0-9-]+$`
// Plugin Tasks parses any numbers in this format, but after parsing marks wrong
// dates as "invalid".
plugTasksDateRe = `\d\d\d\d-\d\d-\d\d`
// Plugin Tasks parses any phrase where words consists of A-Za-z0-9!, and then
// try to parse it as an instruction like "every week on Monday, Thuesday".
plugTasksRecurringRe = `[A-Za-z0-9!,](?:[A-Za-z0-9!, ]*[A-Za-z0-9!,])?`
plugTasksIDRe = `[A-Za-z0-9_-]+`
plugTasksIDsRe = plugTasksIDRe + `(?:,` + plugTasksIDRe + `)*`
// Plugin Tasks parses any word which consists only from letters, and then
// try to parse it (case-insensitive) as one of actions "keep" or "delete".
plugTasksOnCompletionActionRe = `[A-Za-z]+`
plugTasksPrioLowestEmoji = `⏬`
plugTasksPrioLowEmoji = `🔽`
plugTasksPrioMediumEmoji = `🔼`
plugTasksPrioHighEmoji = `⏫`
plugTasksPrioHighestEmoji = `🔺`
plugTasksIDEmoji = `🆔`
plugTasksDependsOnEmoji = `⛔`
plugTasksDueEmoji = `📅`
plugTasksScheduledEmoji = `⏳`
plugTasksStartEmoji = `🛫`
plugTasksCreatedEmoji = `➕`
plugTasksDoneEmoji = `✅`
plugTasksCancelledEmoji = `❌`
plugTasksRecurringEmoji = `🔁`
plugTasksOnCompletionEmoji = `🏁`
)
var plugTasksPropRegexp = regexp.MustCompile(`^\s*(` + // Group 1 is for block.Advance().
// Starts with one of known emoji.
`([` + // Group 2 is an emoji without any args.
plugTasksPrioLowestEmoji +
plugTasksPrioLowEmoji +
plugTasksPrioMediumEmoji +
plugTasksPrioHighEmoji +
plugTasksPrioHighestEmoji +
`])\s*|` +
`([` + // Group 3 is an emoji with ID arg.
plugTasksIDEmoji +
`])\s*(` + plugTasksIDRe + `)\s*|` + // Group 4 is an ID.
`([` + // Group 5 is an emoji with ID list arg.
plugTasksDependsOnEmoji +
`])\s*(` + plugTasksIDsRe + `)\s*|` + // Group 6 is an ID list.
`([` + // Group 7 is an emoji with date arg.
plugTasksDueEmoji +
plugTasksScheduledEmoji +
plugTasksStartEmoji +
plugTasksCreatedEmoji +
plugTasksDoneEmoji +
plugTasksCancelledEmoji +
`])\s*(` + plugTasksDateRe + `)\s*|` + // Group 8 is a date.
`([` + // Group 9 is an emoji with recurring schedule arg.
plugTasksRecurringEmoji +
`])\s*(` + plugTasksRecurringRe + `)\s*|` + // Group 10 is a recurring schedule.
`([` + // Group 11 is an emoji with an action arg.
plugTasksOnCompletionEmoji +
`])\s*(` + plugTasksOnCompletionActionRe + `)\s*` + // Group 12 is an action.
`)` + // End of group 1.
// Followed by any amount of same emoji or tag.
`(?:` +
plugTasksTagRe + `\s*|` +
plugTasksPrioLowestEmoji + `\s*|` +
plugTasksPrioLowEmoji + `\s*|` +
plugTasksPrioMediumEmoji + `\s*|` +
plugTasksPrioHighEmoji + `\s*|` +
plugTasksPrioHighestEmoji + `\s*|` +
plugTasksIDEmoji + `\s*` + plugTasksIDRe + `\s*|` +
plugTasksDependsOnEmoji + `\s*` + plugTasksIDsRe + `\s*|` +
plugTasksDueEmoji + `\s*` + plugTasksDateRe + `\s*|` +
plugTasksScheduledEmoji + `\s*` + plugTasksDateRe + `\s*|` +
plugTasksStartEmoji + `\s*` + plugTasksDateRe + `\s*|` +
plugTasksCreatedEmoji + `\s*` + plugTasksDateRe + `\s*|` +
plugTasksDoneEmoji + `\s*` + plugTasksDateRe + `\s*|` +
plugTasksCancelledEmoji + `\s*` + plugTasksDateRe + `\s*|` +
plugTasksRecurringEmoji + `\s*` + plugTasksRecurringRe + `\s*|` +
plugTasksOnCompletionEmoji + `\s*` + plugTasksOnCompletionActionRe + `\s*` +
`)*` +
// Followed by optional block id and end of line.
`(?:` + plugTasksBlockIDRe + `)?$`,
)
var plugTasksPropTrigger = func(emojis ...string) map[byte]bool { // Const.
firstByte := make(map[byte]bool)
for _, s := range emojis {
firstByte[s[0]] = true
}
return firstByte
}(
plugTasksIDEmoji,
plugTasksPrioLowestEmoji,
plugTasksPrioLowEmoji,
plugTasksPrioMediumEmoji,
plugTasksPrioHighEmoji,
plugTasksPrioHighestEmoji,
plugTasksRecurringEmoji,
plugTasksDueEmoji,
plugTasksScheduledEmoji,
plugTasksStartEmoji,
plugTasksDependsOnEmoji,
plugTasksCreatedEmoji,
plugTasksDoneEmoji,
plugTasksCancelledEmoji,
plugTasksOnCompletionEmoji,
)
// PlugTasksPropParser is an Obsidian plugin Tasks's task properties parser.
//
// Current implementation supports only properties defined in emoji style.
//
// This parser must have lowest precedence than all other inline parsers (e.g. 1000).
type PlugTasksPropParser struct{}
// NewPlugTasksPropParser returns a new PlugTasksPropParser.
func NewPlugTasksPropParser() PlugTasksPropParser {
return PlugTasksPropParser{}
}
// Trigger implements [parser.InlineParser].
func (PlugTasksPropParser) Trigger() []byte {
// Only space and punct bytes works here, so we can't just return first byte of emoji.
// To make it trigger Parse() on emoji we have to trigger on everything possible
// (to ensure we won't miss anything) and then in Parse() skip up to (but not including)
// next emoji, to make that emoji a start of next "line".
var puncts []byte
for b := range byte(math.MaxUint8) {
if util.IsPunct(b) {
puncts = append(puncts, b)
}
}
return append(puncts, '\t', ' ') // ' ' means space or start of line.
}
// Parse implements [parser.InlineParser].
func (PlugTasksPropParser) Parse(parent gast.Node, block text.Reader, _ parser.Context) gast.Node { //nolint:funlen,gocognit // Split will hurt readability.
// Skip parsing task properties if it's not a task.
preceding := parent.FirstChild()
for preceding != nil && preceding.Kind() != obsast.KindPlugTasksStatus {
preceding = preceding.NextSibling()
}
if preceding == nil {
return nil // Not a task: there is no PlugTasksStatus in preceding nodes.
}
line, seg := block.PeekLine()
// Obsidian plugin Tasks parse properties only in first line of a task.
// See https://publish.obsidian.md/tasks/Getting+Started/Getting+Started#Multi-line+checklist+items.
if seg.Stop != parent.Lines().At(0).Stop {
return nil
}
// Regexp will match first task property only if rest of line contains only task
// properties and/or tags and/or block ID.
// See https://publish.obsidian.md/tasks/Getting+Started/Getting+Started#Order+of+metadata/emojis.
m := plugTasksPropRegexp.FindSubmatchIndex(line)
if m == nil {
// goldmark can call inline parser only at start of "line" or space or punct.
// So, to make it call parser at emoji we must ensure emoji will be at start
// of line. For this we need to move prefix of current line (up to but not
// including next emoji) into own Text node.
// To ensure this Text node won't overwrite other inline parsers we also avoid
// including space and punct. Plus run this parser with lowest priority (1000).
i := int(util.UTF8Len(line[0])) // Advance by at least 1 rune.
for i < len(line) && !util.IsSpace(line[i]) && !util.IsPunct(line[i]) {
if plugTasksPropTrigger[line[i]] { // Our emoji may start at this byte.
block.Advance(i)
return gast.NewTextSegment(seg.WithStop(seg.Start + i))
}
i += int(util.UTF8Len(line[i]))
}
return nil
}
// Start indexes in m for each group.
// Only Advance and one of Emoji with it arg (if any) will be set.
const (
idxAdvance = iota*2 + 2
idxEmojiWithoutArgs
idxEmojiWithID
idxID
idxEmojiWithIDs
idxIDs
idxEmojiWithDate
idxDate
idxEmojiWithRecurring
idxRecurring
idxEmojiWithOnCompletionAction
idxOnCompletionAction
)
block.Advance(m[idxAdvance+1])
var node gast.Node
switch {
case m[idxEmojiWithoutArgs] >= 0:
switch string(line[m[idxEmojiWithoutArgs]:m[idxEmojiWithoutArgs+1]]) {
case plugTasksPrioLowestEmoji:
node = obsast.NewPlugTasksPrio(obsast.PlugTasksPrioLowest)
case plugTasksPrioLowEmoji:
node = obsast.NewPlugTasksPrio(obsast.PlugTasksPrioLow)
case plugTasksPrioMediumEmoji:
node = obsast.NewPlugTasksPrio(obsast.PlugTasksPrioMedium)
case plugTasksPrioHighEmoji:
node = obsast.NewPlugTasksPrio(obsast.PlugTasksPrioHigh)
case plugTasksPrioHighestEmoji:
node = obsast.NewPlugTasksPrio(obsast.PlugTasksPrioHighest)
default:
panic("unknown emoji without args")
}
case m[idxEmojiWithID] >= 0:
id := string(line[m[idxID]:m[idxID+1]])
switch string(line[m[idxEmojiWithID]:m[idxEmojiWithID+1]]) {
case plugTasksIDEmoji:
node = obsast.NewPlugTasksID(id)
default:
panic("unknown emoji with ID")
}
case m[idxEmojiWithIDs] >= 0:
ids := strings.Split(string(line[m[idxIDs]:m[idxIDs+1]]), ",")
switch string(line[m[idxEmojiWithIDs]:m[idxEmojiWithIDs+1]]) {
case plugTasksDependsOnEmoji:
node = obsast.NewPlugTasksDependsOn(ids)
default:
panic("unknown emoji with IDs")
}
case m[idxEmojiWithDate] >= 0:
date, _ := time.Parse(time.DateOnly, string(line[m[idxDate]:m[idxDate+1]]))
switch string(line[m[idxEmojiWithDate]:m[idxEmojiWithDate+1]]) {
case plugTasksDueEmoji:
node = obsast.NewPlugTasksDue(date)
case plugTasksScheduledEmoji:
node = obsast.NewPlugTasksScheduled(date)
case plugTasksStartEmoji:
node = obsast.NewPlugTasksStart(date)
case plugTasksCreatedEmoji:
node = obsast.NewPlugTasksCreated(date)
case plugTasksDoneEmoji:
node = obsast.NewPlugTasksDone(date)
case plugTasksCancelledEmoji:
node = obsast.NewPlugTasksCancelled(date)
default:
panic("unknown emoji with date")
}
case m[idxEmojiWithRecurring] >= 0:
rule := string(line[m[idxRecurring]:m[idxRecurring+1]])
switch string(line[m[idxEmojiWithRecurring]:m[idxEmojiWithRecurring+1]]) {
case plugTasksRecurringEmoji:
node = obsast.NewPlugTasksRecurring(rule)
default:
panic("unknown emoji with recurring schedule")
}
case m[idxEmojiWithOnCompletionAction] >= 0:
var action obsast.PlugTasksOnCompletionAction
switch strings.ToLower(string(line[m[idxOnCompletionAction]:m[idxOnCompletionAction+1]])) {
case "keep":
action = obsast.PlugTasksOnCompletionKeep
case "delete":
action = obsast.PlugTasksOnCompletionDelete
}
switch string(line[m[idxEmojiWithOnCompletionAction]:m[idxEmojiWithOnCompletionAction+1]]) {
case plugTasksOnCompletionEmoji:
node = obsast.NewPlugTasksOnCompletion(action)
default:
panic("unknown emoji with on completion action")
}
default:
panic("unknown emoji")
}
// Only first property of each kind is used (even if it has invalid arg).
for preceding != nil && preceding.Kind() != node.Kind() {
preceding = preceding.NextSibling()
}
if preceding != nil { // Current property was already defined, so ignore following ones.
return gast.NewText() // Must return non-nil to apply block.Advance() call.
}
return node
}
// PlugTasksHTMLRenderer is an HTML renderer for Obsidian plugin Tasks's tasks.
//
// It renders task status checkbox and task properties in format similar to produced by saving
// Obsidian plugin Tasks's modal window with task details (add missing spaces, drop invalid
// properties, etc.).
type PlugTasksHTMLRenderer struct {
html.Config // Embed to implement renderer.SetOptioner and apply global html options.
}
// NewPlugTasksHTMLRenderer returns a new PlugTasksHTMLRenderer.
func NewPlugTasksHTMLRenderer() *PlugTasksHTMLRenderer {
r := &PlugTasksHTMLRenderer{
Config: html.NewConfig(),
}
return r
}
// RegisterFuncs implements [renderer.NodeRenderer].
func (r *PlugTasksHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(obsast.KindPlugTasksStatus, entering(r.renderStatus))
reg.Register(obsast.KindPlugTasksPrio, entering(r.renderPrio))
reg.Register(obsast.KindPlugTasksID, entering(r.renderID))
reg.Register(obsast.KindPlugTasksDependsOn, entering(r.renderDependsOn))
reg.Register(obsast.KindPlugTasksDue, entering(r.renderDate))
reg.Register(obsast.KindPlugTasksScheduled, entering(r.renderDate))
reg.Register(obsast.KindPlugTasksStart, entering(r.renderDate))
reg.Register(obsast.KindPlugTasksCreated, entering(r.renderDate))
reg.Register(obsast.KindPlugTasksDone, entering(r.renderDate))
reg.Register(obsast.KindPlugTasksCancelled, entering(r.renderDate))
reg.Register(obsast.KindPlugTasksRecurring, entering(r.renderRecurring))
reg.Register(obsast.KindPlugTasksOnCompletion, entering(r.renderOnCompletion))
}
// PlugTasksCheckboxAttributeFilter defines attribute names which <input type=checkbox>
// elements can have.
var PlugTasksCheckboxAttributeFilter = html.GlobalAttributeFilter //nolint:gochecknoglobals // By design.
func (r *PlugTasksHTMLRenderer) renderStatus(w util.BufWriter, _ []byte, node gast.Node) (gast.WalkStatus, error) {
n := node.(*obsast.PlugTasksStatus)
if n.IsChecked() {
_, _ = w.WriteString(`<input checked="" disabled="" type="checkbox"`)
} else {
_, _ = w.WriteString(`<input disabled="" type="checkbox"`)
}
if n.Attributes() != nil {
html.RenderAttributes(w, n, PlugTasksCheckboxAttributeFilter)
}
if r.XHTML {
_, _ = w.WriteString(` /> `)
} else {
_, _ = w.WriteString(`> `)
}
return gast.WalkContinue, nil
}
func (*PlugTasksHTMLRenderer) renderPrio(w util.BufWriter, _ []byte, node gast.Node) (gast.WalkStatus, error) {
n := node.(*obsast.PlugTasksPrio)
if n.Prio == obsast.PlugTasksPrioDefault {
return gast.WalkContinue, nil
}
_ = w.WriteByte(' ')
switch n.Prio {
case obsast.PlugTasksPrioLowest:
_, _ = w.WriteString(plugTasksPrioLowestEmoji)
case obsast.PlugTasksPrioLow:
_, _ = w.WriteString(plugTasksPrioLowEmoji)
case obsast.PlugTasksPrioDefault: // Make linter exhaustive happy without disabling it.
panic("never here")
case obsast.PlugTasksPrioMedium:
_, _ = w.WriteString(plugTasksPrioMediumEmoji)
case obsast.PlugTasksPrioHigh:
_, _ = w.WriteString(plugTasksPrioHighEmoji)
case obsast.PlugTasksPrioHighest:
_, _ = w.WriteString(plugTasksPrioHighestEmoji)
default:
panic(fmt.Sprintf("unknown node priority: %v", n.Prio))
}
return gast.WalkContinue, nil
}
func (*PlugTasksHTMLRenderer) renderID(w util.BufWriter, _ []byte, node gast.Node) (gast.WalkStatus, error) {
n := node.(*obsast.PlugTasksID)
_ = w.WriteByte(' ')
_, _ = w.WriteString(plugTasksIDEmoji)
_ = w.WriteByte(' ')
_, _ = w.WriteString(n.ID)
return gast.WalkContinue, nil
}
func (*PlugTasksHTMLRenderer) renderDependsOn(w util.BufWriter, _ []byte, node gast.Node) (gast.WalkStatus, error) {
n := node.(*obsast.PlugTasksDependsOn)
_ = w.WriteByte(' ')
_, _ = w.WriteString(plugTasksDependsOnEmoji)
_ = w.WriteByte(' ')
_, _ = w.WriteString(strings.Join(n.IDs, ","))
return gast.WalkContinue, nil
}
func (*PlugTasksHTMLRenderer) renderDate(w util.BufWriter, _ []byte, node gast.Node) (gast.WalkStatus, error) {
// Do not output properties with invalid date.
// This isn't actually match behaviour of Obsidian plugin Tasks's modal window
// because it's impossible to save with invalid dates at all.
// But this behaviour is consistent with handling all other errors.
if !node.(interface{ IsValid() bool }).IsValid() {
return gast.WalkContinue, nil
}
var date time.Time
_ = w.WriteByte(' ')
switch n := node.(type) {
case *obsast.PlugTasksDue:
_, _ = w.WriteString(plugTasksDueEmoji)
date = n.Date
case *obsast.PlugTasksScheduled:
_, _ = w.WriteString(plugTasksScheduledEmoji)
date = n.Date
case *obsast.PlugTasksStart:
_, _ = w.WriteString(plugTasksStartEmoji)
date = n.Date
case *obsast.PlugTasksCreated:
_, _ = w.WriteString(plugTasksCreatedEmoji)
date = n.Date
case *obsast.PlugTasksDone:
_, _ = w.WriteString(plugTasksDoneEmoji)
date = n.Date
case *obsast.PlugTasksCancelled:
_, _ = w.WriteString(plugTasksCancelledEmoji)
date = n.Date
default:
panic(fmt.Sprintf("unknown node with date: %T", node))
}
_ = w.WriteByte(' ')
_, _ = w.WriteString(date.Format(time.DateOnly))
return gast.WalkContinue, nil
}
func (*PlugTasksHTMLRenderer) renderRecurring(w util.BufWriter, _ []byte, node gast.Node) (gast.WalkStatus, error) {
n := node.(*obsast.PlugTasksRecurring)
_ = w.WriteByte(' ')
_, _ = w.WriteString(plugTasksRecurringEmoji)
_ = w.WriteByte(' ')
_, _ = w.WriteString(n.Rule)
return gast.WalkContinue, nil
}
func (*PlugTasksHTMLRenderer) renderOnCompletion(w util.BufWriter, _ []byte, node gast.Node) (gast.WalkStatus, error) {
n := node.(*obsast.PlugTasksOnCompletion)
if !n.IsValid() {
return gast.WalkContinue, nil
}
_ = w.WriteByte(' ')
_, _ = w.WriteString(plugTasksOnCompletionEmoji)
_ = w.WriteByte(' ')
_, _ = w.WriteString(n.Action.String())
return gast.WalkContinue, nil
}
// PlugTasks is an extension that helps to setup Obsidian plugin [Tasks] parser and HTML renderer.
//
// [Tasks]: https://publish.obsidian.md/tasks/Introduction
type PlugTasks struct {
opts []PlugTasksOption
}
// NewPlugTasks returns a new PlugTasks extension.
func NewPlugTasks(opts ...PlugTasksOption) PlugTasks {
return PlugTasks{opts: opts}
}
// Extend implements [goldmark.Extender].
func (e PlugTasks) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithInlineParsers(
// Not sure why it's important to use so high priority, but
// official extension.TaskList uses this priority.
util.Prioritized(NewPlugTasksParser(e.opts...), prioHighest),
// This parser creates Text nodes for everything up to next emoji,
// so run it with lowest priority to make sure it won't turn into Text node
// something what should be handled by some other parser.
util.Prioritized(NewPlugTasksPropParser(), prioInlineParserLowest),
))
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(NewPlugTasksHTMLRenderer(), prioHTMLRenderer),
))
}