Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

3.7 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package cmd

import (
	"fmt"

	"github.com/siyuan-note/siyuan/kernel/model"
	"github.com/siyuan-note/siyuan/kernel/util"

	"github.com/spf13/cobra"
)

var dailynoteCmd = &cobra.Command{
	Use:   "dailynote",
	Short: "Daily note (dailynote) operations",
}

var dailynoteCreateCmd = &cobra.Command{
	Use:   "create --notebook <id>",
	Short: "Create today's daily note",
	RunE: func(cmd *cobra.Command, args []string) error {
		notebook, _ := cmd.Flags().GetString("notebook")
		if notebook == "" {
			return fmt.Errorf("--notebook is required")
		}

		if dryRun {
			fmt.Printf("[dry-run] Would create daily note in notebook %s\n", notebook)
			return nil
		}

		p, _, err := model.CreateDailyNote(notebook)
		if err != nil {
			return err
		}

		model.FlushTxQueue()
		model.AppendPushReloadFiletreeEntry()

		id := util.GetTreeID(p)
		fmt.Println(id)
		return nil
	},
}

var dailynoteAppendCmd = &cobra.Command{
	Use:   "append --notebook <id> [--data <markdown> | --file <path>]",
	Short: "Append block to today's daily note",
	RunE: func(cmd *cobra.Command, args []string) error {
		notebook, _ := cmd.Flags().GetString("notebook")
		if notebook == "" {
			return fmt.Errorf("--notebook is required")
		}

		if dryRun {
			fmt.Printf("[dry-run] Would ensure daily note in notebook %s and append block\n", notebook)
			return nil
		}

		data, err := resolveData(cmd)
		if err != nil {
			return err
		}

		p, _, err := model.CreateDailyNote(notebook)
		if err != nil {
			return err
		}

		parentID := util.GetTreeID(p)
		dom := markdownToBlockDOM(data)
		transactions := []*model.Transaction{{
			DoOperations: []*model.Operation{{
				Action:   "appendInsert",
				Data:     dom,
				ParentID: parentID,
			}},
		}}
		model.PerformTransactions(&transactions)
		model.WaitForTransactions(transactions)
		model.AppendPushReloadProtyleEntry(parentID)
		fmt.Println(parentID)
		return nil
	},
}

var dailynotePrependCmd = &cobra.Command{
	Use:   "prepend --notebook <id> [--data <markdown> | --file <path>]",
	Short: "Prepend block to today's daily note",
	RunE: func(cmd *cobra.Command, args []string) error {
		notebook, _ := cmd.Flags().GetString("notebook")
		if notebook == "" {
			return fmt.Errorf("--notebook is required")
		}

		if dryRun {
			fmt.Printf("[dry-run] Would ensure daily note in notebook %s and prepend block\n", notebook)
			return nil
		}

		data, err := resolveData(cmd)
		if err != nil {
			return err
		}

		p, _, err := model.CreateDailyNote(notebook)
		if err != nil {
			return err
		}

		parentID := util.GetTreeID(p)
		dom := markdownToBlockDOM(data)
		transactions := []*model.Transaction{{
			DoOperations: []*model.Operation{{
				Action:   "prependInsert",
				Data:     dom,
				ParentID: parentID,
			}},
		}}
		model.PerformTransactions(&transactions)
		model.WaitForTransactions(transactions)
		model.AppendPushReloadProtyleEntry(parentID)
		fmt.Println(parentID)
		return nil
	},
}

func init() {
	dailynoteCreateCmd.Flags().String("notebook", "", "notebook ID")

	dailynoteAppendCmd.Flags().String("notebook", "", "notebook ID")
	dailynoteAppendCmd.Flags().String("data", "", "markdown content")
	dailynoteAppendCmd.Flags().String("file", "", "read content from file path (- for stdin)")

	dailynotePrependCmd.Flags().String("notebook", "", "notebook ID")
	dailynotePrependCmd.Flags().String("data", "", "markdown content")
	dailynotePrependCmd.Flags().String("file", "", "read content from file path (- for stdin)")

	rootCmd.AddCommand(dailynoteCmd)
	dailynoteCmd.AddCommand(dailynoteCreateCmd)
	dailynoteCmd.AddCommand(dailynoteAppendCmd)
	dailynoteCmd.AddCommand(dailynotePrependCmd)
}