Skip to content

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

1.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 model

import (
	"strings"
	"testing"

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

func TestExportMarkdownTaskMarkers(t *testing.T) {
	previousConf := Conf
	Conf = NewAppConf()
	{
		confSectionValue0, confSectionValue1 := conf.NewEditor(), conf.NewExport()
		Conf.Editor.Store(confSectionValue0)
		Conf.Export.Store(confSectionValue1)
	}
	t.Cleanup(func() { Conf = previousConf })
	config := *conf.NewExport()
	config.AddTitle = false
	for _, preserve := range []bool{false, true} {
		engine := NewLute()
		tree := parse.Parse("", []byte("- [ ] todo\n- [/] progress\n  - [-] canceled\n- [X] done\n- [!] custom\n"), engine.ParseOptions)
		output := exportMarkdownContent0(tree.Root.ID, tree, markdownExportOptions{
			Config: config, Ext: ".md", PreserveTaskMarkers: preserve,
		})
		for _, task := range []struct{ marker, text string }{{" ", "todo"}, {"/", "progress"}, {"-", "canceled"}, {"X", "done"}, {"!", "custom"}} {
			marker := task.marker
			if !preserve && marker != " " {
				marker = "X"
			}
			if !strings.Contains(output, "["+marker+"] "+task.text) {
				t.Fatalf("preserve=%v: missing marker for %s: %s", preserve, task.text, output)
			}
		}
		markers := ""
		ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
			if entering && node.Type == ast.NodeTaskListItemMarker {
				markers += node.EffectiveTaskListItemMarker()
			}
			return ast.WalkContinue
		})
		if markers != " /-X!" {
			t.Fatalf("source task markers changed: %q", markers)
		}
	}
}