runner.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package memopayload
  2. import (
  3. "context"
  4. "log/slog"
  5. "slices"
  6. "github.com/pkg/errors"
  7. "github.com/usememos/gomark/ast"
  8. "github.com/usememos/gomark/parser"
  9. "github.com/usememos/gomark/parser/tokenizer"
  10. storepb "github.com/usememos/memos/proto/gen/store"
  11. "github.com/usememos/memos/store"
  12. )
  13. type Runner struct {
  14. Store *store.Store
  15. }
  16. func NewRunner(store *store.Store) *Runner {
  17. return &Runner{
  18. Store: store,
  19. }
  20. }
  21. // RunOnce rebuilds the payload of all memos.
  22. func (r *Runner) RunOnce(ctx context.Context) {
  23. memos, err := r.Store.ListMemos(ctx, &store.FindMemo{})
  24. if err != nil {
  25. slog.Error("failed to list memos", "err", err)
  26. return
  27. }
  28. for _, memo := range memos {
  29. if err := RebuildMemoPayload(memo); err != nil {
  30. slog.Error("failed to rebuild memo payload", "err", err)
  31. continue
  32. }
  33. if err := r.Store.UpdateMemo(ctx, &store.UpdateMemo{
  34. ID: memo.ID,
  35. Payload: memo.Payload,
  36. }); err != nil {
  37. slog.Error("failed to update memo", "err", err)
  38. }
  39. }
  40. }
  41. func RebuildMemoPayload(memo *store.Memo) error {
  42. nodes, err := parser.Parse(tokenizer.Tokenize(memo.Content))
  43. if err != nil {
  44. return errors.Wrap(err, "failed to parse content")
  45. }
  46. if memo.Payload == nil {
  47. memo.Payload = &storepb.MemoPayload{}
  48. }
  49. tags := []string{}
  50. references := []string{}
  51. property := &storepb.MemoPayload_Property{}
  52. TraverseASTNodes(nodes, func(node ast.Node) {
  53. switch n := node.(type) {
  54. case *ast.Tag:
  55. tag := n.Content
  56. if !slices.Contains(tags, tag) {
  57. tags = append(tags, tag)
  58. }
  59. case *ast.Link, *ast.AutoLink:
  60. property.HasLink = true
  61. case *ast.TaskListItem:
  62. property.HasTaskList = true
  63. if !n.Complete {
  64. property.HasIncompleteTasks = true
  65. }
  66. case *ast.Code, *ast.CodeBlock:
  67. property.HasCode = true
  68. case *ast.EmbeddedContent:
  69. // TODO: validate references.
  70. references = append(references, n.ResourceName)
  71. }
  72. })
  73. memo.Payload.Tags = tags
  74. memo.Payload.References = references
  75. memo.Payload.Property = property
  76. return nil
  77. }
  78. func TraverseASTNodes(nodes []ast.Node, fn func(ast.Node)) {
  79. for _, node := range nodes {
  80. fn(node)
  81. switch n := node.(type) {
  82. case *ast.Paragraph:
  83. TraverseASTNodes(n.Children, fn)
  84. case *ast.Heading:
  85. TraverseASTNodes(n.Children, fn)
  86. case *ast.Blockquote:
  87. TraverseASTNodes(n.Children, fn)
  88. case *ast.List:
  89. TraverseASTNodes(n.Children, fn)
  90. case *ast.OrderedListItem:
  91. TraverseASTNodes(n.Children, fn)
  92. case *ast.UnorderedListItem:
  93. TraverseASTNodes(n.Children, fn)
  94. case *ast.TaskListItem:
  95. TraverseASTNodes(n.Children, fn)
  96. case *ast.Bold:
  97. TraverseASTNodes(n.Children, fn)
  98. }
  99. }
  100. }