Skip to content

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

:bug: Preserve heading fragments in imported Markdown links https://github.com/siyuan-note/siyuan/issues/20955

Daniel6c918165bf10
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 46e186a7d4..f72d6399eb 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -2400,11 +2400,14 @@ func convertMdHyperlinks2WikiLinks(tree *parse.Tree) {
if strings.HasPrefix(linkDest, "assets/") {
return ast.WalkContinue
}
- if !strings.HasSuffix(linkDest, ".md") && !strings.HasSuffix(linkDest, ".markdown") {
+ linkPath, fragment, hasFragment := strings.Cut(linkDest, "#")
+ if !strings.HasSuffix(linkPath, ".md") && !strings.HasSuffix(linkPath, ".markdown") {
return ast.WalkContinue
}
- linkDest = strings.TrimSuffix(linkDest, ".md")
- linkDest = strings.TrimSuffix(linkDest, ".markdown")
+ linkDest = strings.TrimSuffix(strings.TrimSuffix(linkPath, ".md"), ".markdown")
+ if hasFragment {
+ linkDest += "#" + fragment
+ }
buf := bytes.Buffer{}
buf.WriteString("[[")
diff --git a/kernel/model/import_markdown_spool_test.go b/kernel/model/import_markdown_spool_test.go
index 4fc3afdf12..475e9a79d4 100644
--- a/kernel/model/import_markdown_spool_test.go
+++ b/kernel/model/import_markdown_spool_test.go
@@ -110,6 +110,40 @@ func TestMarkdownImportSpoolPreservesConversion(t *testing.T) {
}
}
+func TestMarkdownImportHeadingFragments(t *testing.T) {
+ setupMarkdownSpoolTest(t)
+ target := markdownSpoolFixture("# Section\n\nTarget", "two", 2)
+ headingID := target.Root.ChildrenByType(ast.NodeHeading)[0].ID
+ links := map[string]string{}
+ addImportSearchLinks(target, links)
+ for _, extension := range []string{".md", ".markdown"} {
+ t.Run(extension, func(t *testing.T) {
+ tree := markdownSpoolFixture("[document](two"+extension+") [section](two"+extension+"#Section) [empty](two"+extension+"#) [asset](assets/two"+extension+"#Section) [web](https://example.com/page.html#Section)", "one", 1)
+ if err := convertMarkdownImportTree(tree, links, NewLute()); err != nil {
+ t.Fatal(err)
+ }
+ refs, hrefs := map[string]string{}, map[string]string{}
+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering && treenode.IsBlockRef(n) {
+ refs[n.TextMarkTextContent] = n.TextMarkBlockRefID
+ }
+ if entering && n.IsTextMarkType("a") {
+ hrefs[n.TextMarkTextContent] = n.TextMarkAHref
+ }
+ return ast.WalkContinue
+ })
+ for label, want := range map[string]string{"document": target.ID, "section": headingID, "empty": target.ID} {
+ if refs[label] != want {
+ t.Errorf("%s target = %q, want %q", label, refs[label], want)
+ }
+ }
+ if hrefs["asset"] != "assets/two"+extension+"#Section" || hrefs["web"] != "https://example.com/page.html#Section" {
+ t.Fatalf("unrelated links changed: %v", hrefs)
+ }
+ })
+ }
+}
+
func TestMarkdownImportSpoolSeparatesFilePathsFromTitles(t *testing.T) {
setupMarkdownSpoolTest(t)
spool, err := newMarkdownImportSpool()