Skip to content

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

3.5 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 (
	"encoding/json"
	"fmt"
	"os"
	"text/tabwriter"

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

	"github.com/spf13/cobra"
)

var refCmd = &cobra.Command{
	Use:   "ref",
	Short: "Backlinks and references",
}

var refBacklinksCmd = &cobra.Command{
	Use:   "backlinks --id <id>",
	Short: "Get backlinks for a block",
	RunE: func(cmd *cobra.Command, args []string) error {
		id, _ := cmd.Flags().GetString("id")
		if id == "" {
			return fmt.Errorf("--id is required")
		}

		keyword, _ := cmd.Flags().GetString("keyword")
		sortMode, _ := cmd.Flags().GetInt("sort")

		_, backlinks, _, count, _ := model.GetBacklink2(id, keyword, "", sortMode, 0, model.Conf.Editor.Load().BacklinkContainChildren)

		switch outputFormat {
		case "json":
			data, _ := json.MarshalIndent(backlinks, "", "  ")
			fmt.Println(string(data))
		default:
			printPathTable(backlinks, false)
		}

		fmt.Printf("\n%d backlink(s)\n", count)
		return nil
	},
}

var refMentionsCmd = &cobra.Command{
	Use:   "mentions --id <id>",
	Short: "Get mentions for a block",
	RunE: func(cmd *cobra.Command, args []string) error {
		id, _ := cmd.Flags().GetString("id")
		if id == "" {
			return fmt.Errorf("--id is required")
		}

		keyword, _ := cmd.Flags().GetString("keyword")
		sortMode, _ := cmd.Flags().GetInt("sort")

		_, _, backmentions, _, count := model.GetBacklink2(id, "", keyword, 0, sortMode, model.Conf.Editor.Load().BacklinkContainChildren)

		switch outputFormat {
		case "json":
			data, _ := json.MarshalIndent(backmentions, "", "  ")
			fmt.Println(string(data))
		default:
			printPathTable(backmentions, false)
		}

		fmt.Printf("\n%d mention(s)\n", count)
		return nil
	},
}

var refRefreshCmd = &cobra.Command{
	Use:   "refresh --id <id>",
	Short: "Refresh backlinks for a block",
	RunE: func(cmd *cobra.Command, args []string) error {
		id, _ := cmd.Flags().GetString("id")
		if id == "" {
			return fmt.Errorf("--id is required")
		}

		if dryRun {
			fmt.Printf("[dry-run] Would refresh backlinks for block %s\n", id)
			return nil
		}

		model.RefreshBacklink(id)
		fmt.Println("ok")
		return nil
	},
}

func printPathTable(paths []*model.Path, showBox bool) {
	if len(paths) == 0 {
		fmt.Println("(none)")
		return
	}

	w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
	if showBox {
		fmt.Fprintln(w, "ID\tTYPE\tNAME\tHPATH\tCOUNT")
	} else {
		fmt.Fprintln(w, "ID\tTYPE\tNAME\tCOUNT")
	}
	for _, p := range paths {
		name := p.Name
		if name == "" {
			name = truncate(p.HPath, 40)
		}
		if showBox {
			fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", p.ID, p.NodeType, name, p.HPath, p.Count)
		} else {
			fmt.Fprintf(w, "%s\t%s\t%s\t%d\n", p.ID, p.NodeType, name, p.Count)
		}
	}
	w.Flush()
}

func init() {
	refBacklinksCmd.Flags().String("id", "", "block ID")
	refBacklinksCmd.Flags().String("keyword", "", "filter by keyword")
	refBacklinksCmd.Flags().Int("sort", 0, "sort: 0=updated-desc 1=updated-asc 2=created-desc 3=created-asc 4=name-desc 5=name-asc 6=alphanum-desc 7=alphanum-asc")

	refMentionsCmd.Flags().String("id", "", "block ID")
	refMentionsCmd.Flags().String("keyword", "", "filter by keyword")
	refMentionsCmd.Flags().Int("sort", 0, "sort: 0=updated-desc 1=updated-asc 2=created-desc 3=created-asc 4=name-desc 5=name-asc 6=alphanum-desc 7=alphanum-asc")

	refRefreshCmd.Flags().String("id", "", "block ID")

	rootCmd.AddCommand(refCmd)
	refCmd.AddCommand(refBacklinksCmd)
	refCmd.AddCommand(refMentionsCmd)
	refCmd.AddCommand(refRefreshCmd)
}