Skip to content

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

1.8 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 model

import (
	"net/url"
	"strings"

	"github.com/88250/lute"
	"github.com/88250/lute/ast"
	"github.com/88250/lute/parse"
	"github.com/siyuan-note/siyuan/kernel/util"
)

// imageAssetMetadataHits 按图片引用归并标题和提示文本命中,同一资源的多个引用取最高权重。
func imageAssetMetadataHits(markdowns, keywords []string) map[string]int {
	ret := map[string]int{}
	luteEngine := lute.New()
	for _, markdown := range markdowns {
		tree := parse.Parse("", []byte(markdown), luteEngine.ParseOptions)
		ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
			if !entering || node.Type != ast.NodeImage {
				return ast.WalkContinue
			}
			dest := node.ChildByType(ast.NodeLinkDest)
			if dest == nil {
				return ast.WalkSkipChildren
			}
			assetPath, _, _ := strings.Cut(util.UnescapeHTML(dest.TokensStr()), "?")
			if decoded, err := url.PathUnescape(assetPath); err == nil {
				assetPath = decoded
			}
			if !strings.HasPrefix(assetPath, "assets/") {
				return ast.WalkSkipChildren
			}
			hits := assetMetadataTextHits(util.UnescapeHTML(node.Content()), keywords)
			if title := node.ChildByType(ast.NodeLinkTitle); title != nil {
				hits += assetMetadataTextHits(util.UnescapeHTML(title.TokensStr()), keywords)
			}
			ret[assetPath] = max(ret[assetPath], hits)
			return ast.WalkSkipChildren
		})
	}
	return ret
}

func assetMetadataTextHits(text string, keywords []string) int {
	text = strings.ToLower(text)
	hits := 0
	for i, keyword := range keywords {
		if strings.TrimSpace(keyword) == "" {
			continue
		}
		keyword = strings.ToLower(keyword)
		if strings.Contains(text, keyword) {
			if i == 0 {
				hits += 32
			}
			hits += strings.Count(text, keyword)
		}
	}
	return hits
}