mirror of https://github.com/usememos/memos.git
chore: remove references handling from markdown extraction
This commit is contained in:
parent
6cb96ef65e
commit
7eec424274
|
|
@ -19,9 +19,8 @@ import (
|
|||
|
||||
// ExtractedData contains all metadata extracted from markdown in a single pass.
|
||||
type ExtractedData struct {
|
||||
Tags []string
|
||||
Property *storepb.MemoPayload_Property
|
||||
References []string
|
||||
Tags []string
|
||||
Property *storepb.MemoPayload_Property
|
||||
}
|
||||
|
||||
// Service handles markdown metadata extraction.
|
||||
|
|
@ -38,9 +37,6 @@ type Service interface {
|
|||
// ExtractProperties computes boolean properties
|
||||
ExtractProperties(content []byte) (*storepb.MemoPayload_Property, error)
|
||||
|
||||
// ExtractReferences returns all wikilink references ([[...]]) found in content
|
||||
ExtractReferences(content []byte) ([]string, error)
|
||||
|
||||
// RenderMarkdown renders goldmark AST back to markdown text
|
||||
RenderMarkdown(content []byte) (string, error)
|
||||
|
||||
|
|
@ -195,36 +191,6 @@ func (s *service) ExtractProperties(content []byte) (*storepb.MemoPayload_Proper
|
|||
return prop, nil
|
||||
}
|
||||
|
||||
// ExtractReferences returns all wikilink references found in content.
|
||||
func (s *service) ExtractReferences(content []byte) ([]string, error) {
|
||||
root, err := s.parse(content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
references := []string{} // Initialize to empty slice, not nil
|
||||
|
||||
// Walk the AST to find wikilink nodes
|
||||
err = gast.Walk(root, func(n gast.Node, entering bool) (gast.WalkStatus, error) {
|
||||
if !entering {
|
||||
return gast.WalkContinue, nil
|
||||
}
|
||||
|
||||
// Check for custom WikilinkNode
|
||||
if wikilinkNode, ok := n.(*mast.WikilinkNode); ok {
|
||||
references = append(references, string(wikilinkNode.Target))
|
||||
}
|
||||
|
||||
return gast.WalkContinue, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return references, nil
|
||||
}
|
||||
|
||||
// RenderMarkdown renders goldmark AST back to markdown text.
|
||||
func (s *service) RenderMarkdown(content []byte) (string, error) {
|
||||
root, err := s.parse(content)
|
||||
|
|
@ -338,9 +304,8 @@ func (s *service) ExtractAll(content []byte) (*ExtractedData, error) {
|
|||
}
|
||||
|
||||
data := &ExtractedData{
|
||||
Tags: []string{},
|
||||
Property: &storepb.MemoPayload_Property{},
|
||||
References: []string{},
|
||||
Tags: []string{},
|
||||
Property: &storepb.MemoPayload_Property{},
|
||||
}
|
||||
|
||||
// Single walk to collect all data
|
||||
|
|
@ -354,11 +319,6 @@ func (s *service) ExtractAll(content []byte) (*ExtractedData, error) {
|
|||
data.Tags = append(data.Tags, string(tagNode.Tag))
|
||||
}
|
||||
|
||||
// Extract references (wikilinks)
|
||||
if wikilinkNode, ok := n.(*mast.WikilinkNode); ok {
|
||||
data.References = append(data.References, string(wikilinkNode.Target))
|
||||
}
|
||||
|
||||
// Extract properties based on node kind
|
||||
switch n.Kind() {
|
||||
case gast.KindLink, mast.KindWikilink:
|
||||
|
|
|
|||
|
|
@ -302,73 +302,6 @@ func TestExtractTags(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestExtractReferences(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
withExt bool
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "no references",
|
||||
content: "Just plain text",
|
||||
withExt: false,
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
name: "single wikilink",
|
||||
content: "Check this: [[resources/101]]",
|
||||
withExt: true,
|
||||
expected: []string{"resources/101"},
|
||||
},
|
||||
{
|
||||
name: "multiple wikilinks",
|
||||
content: "[[resources/101]]\n\nAnd also: [[memos/42]]",
|
||||
withExt: true,
|
||||
expected: []string{"resources/101", "memos/42"},
|
||||
},
|
||||
{
|
||||
name: "wikilink with params",
|
||||
content: "[[resources/101?align=center]]",
|
||||
withExt: true,
|
||||
expected: []string{"resources/101"},
|
||||
},
|
||||
{
|
||||
name: "duplicate wikilinks",
|
||||
content: "[[resources/101]]\n\n[[resources/101]]",
|
||||
withExt: true,
|
||||
expected: []string{"resources/101", "resources/101"}, // Not deduplicated at this layer
|
||||
},
|
||||
{
|
||||
name: "no extension enabled",
|
||||
content: "[[resources/101]]",
|
||||
withExt: false,
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
name: "wikilink in sentence",
|
||||
content: "Check [[memos/1]] for details",
|
||||
withExt: true,
|
||||
expected: []string{"memos/1"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var svc Service
|
||||
if tt.withExt {
|
||||
svc = NewService(WithWikilinkExtension())
|
||||
} else {
|
||||
svc = NewService()
|
||||
}
|
||||
|
||||
references, err := svc.ExtractReferences([]byte(tt.content))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.expected, references)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUniqueLowercase(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
|
|||
|
|
@ -88,10 +88,8 @@ type MemoPayload_Property struct {
|
|||
HasTaskList bool `protobuf:"varint,2,opt,name=has_task_list,json=hasTaskList,proto3" json:"has_task_list,omitempty"`
|
||||
HasCode bool `protobuf:"varint,3,opt,name=has_code,json=hasCode,proto3" json:"has_code,omitempty"`
|
||||
HasIncompleteTasks bool `protobuf:"varint,4,opt,name=has_incomplete_tasks,json=hasIncompleteTasks,proto3" json:"has_incomplete_tasks,omitempty"`
|
||||
// The references of the memo. Should be a list of uuid.
|
||||
References []string `protobuf:"bytes,5,rep,name=references,proto3" json:"references,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *MemoPayload_Property) Reset() {
|
||||
|
|
@ -152,13 +150,6 @@ func (x *MemoPayload_Property) GetHasIncompleteTasks() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (x *MemoPayload_Property) GetReferences() []string {
|
||||
if x != nil {
|
||||
return x.References
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type MemoPayload_Location struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Placeholder string `protobuf:"bytes,1,opt,name=placeholder,proto3" json:"placeholder,omitempty"`
|
||||
|
|
@ -223,19 +214,16 @@ var File_store_memo_proto protoreflect.FileDescriptor
|
|||
|
||||
const file_store_memo_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\x10store/memo.proto\x12\vmemos.store\"\xc0\x03\n" +
|
||||
"\x10store/memo.proto\x12\vmemos.store\"\xa0\x03\n" +
|
||||
"\vMemoPayload\x12=\n" +
|
||||
"\bproperty\x18\x01 \x01(\v2!.memos.store.MemoPayload.PropertyR\bproperty\x12=\n" +
|
||||
"\blocation\x18\x02 \x01(\v2!.memos.store.MemoPayload.LocationR\blocation\x12\x12\n" +
|
||||
"\x04tags\x18\x03 \x03(\tR\x04tags\x1a\xb6\x01\n" +
|
||||
"\x04tags\x18\x03 \x03(\tR\x04tags\x1a\x96\x01\n" +
|
||||
"\bProperty\x12\x19\n" +
|
||||
"\bhas_link\x18\x01 \x01(\bR\ahasLink\x12\"\n" +
|
||||
"\rhas_task_list\x18\x02 \x01(\bR\vhasTaskList\x12\x19\n" +
|
||||
"\bhas_code\x18\x03 \x01(\bR\ahasCode\x120\n" +
|
||||
"\x14has_incomplete_tasks\x18\x04 \x01(\bR\x12hasIncompleteTasks\x12\x1e\n" +
|
||||
"\n" +
|
||||
"references\x18\x05 \x03(\tR\n" +
|
||||
"references\x1af\n" +
|
||||
"\x14has_incomplete_tasks\x18\x04 \x01(\bR\x12hasIncompleteTasks\x1af\n" +
|
||||
"\bLocation\x12 \n" +
|
||||
"\vplaceholder\x18\x01 \x01(\tR\vplaceholder\x12\x1a\n" +
|
||||
"\blatitude\x18\x02 \x01(\x01R\blatitude\x12\x1c\n" +
|
||||
|
|
|
|||
|
|
@ -17,8 +17,6 @@ message MemoPayload {
|
|||
bool has_task_list = 2;
|
||||
bool has_code = 3;
|
||||
bool has_incomplete_tasks = 4;
|
||||
// The references of the memo. Should be a list of uuid.
|
||||
repeated string references = 5;
|
||||
}
|
||||
|
||||
message Location {
|
||||
|
|
|
|||
|
|
@ -82,9 +82,6 @@ func RebuildMemoPayload(memo *store.Memo, markdownService markdown.Service) erro
|
|||
return errors.Wrap(err, "failed to extract markdown metadata")
|
||||
}
|
||||
|
||||
// Set references in property
|
||||
data.Property.References = data.References
|
||||
|
||||
memo.Payload.Tags = data.Tags
|
||||
memo.Payload.Property = data.Property
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Reference in New Issue