Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions plugin/markdown/extensions/memos_ref_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package extensions

import (
"bytes"

"github.com/yuin/goldmark"
gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)

type memosRefExtension struct{}

// MemosRefExtension marks Link nodes whose destination contains a `memos/<id>` segment.
//
// It annotates the existing goldmark `*ast.Link` node with an attribute
var MemosRefExtension = &memosRefExtension{}

const (
// AttrMemosRefID is stored on `*ast.Link` when a `memos/<id>` segment is detected.
AttrMemosRefID = "memosRefId"
)

func (*memosRefExtension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithASTTransformers(
// Run after the built-in link parser has produced Link nodes.
util.Prioritized(&memosRefLinkMarker{}, 200),
),
)
}

type memosRefLinkMarker struct{}

func (*memosRefLinkMarker) Transform(doc *gast.Document, _ text.Reader, _ parser.Context) {
_ = gast.Walk(doc, func(n gast.Node, entering bool) (gast.WalkStatus, error) {
if !entering {
return gast.WalkContinue, nil
}

link, ok := n.(*gast.Link)
if !ok {
return gast.WalkContinue, nil
}

// Don't overwrite if already set.
if _, found := link.AttributeString(AttrMemosRefID); found {
return gast.WalkContinue, nil
}

id, ok := extractMemosIDFromDest(link.Destination)
if !ok {
return gast.WalkContinue, nil
}

// Store as string (detached from the parser buffer).
link.SetAttributeString(AttrMemosRefID, id)
return gast.WalkContinue, nil
})
}

func extractMemosIDFromDest(dest []byte) (string, bool) {
const prefix = "memos/"
idx := bytes.Index(dest, []byte(prefix))
if idx < 0 {
return "", false
}

start := idx + len(prefix)
if start >= len(dest) {
return "", false
}

end := start
for end < len(dest) && isValidMemosIDByte(dest[end]) {
end++
}
if end == start {
return "", false
}

return string(dest[start:end]), true
}

func isValidMemosIDByte(b byte) bool {
return (b >= '0' && b <= '9') ||
(b >= 'a' && b <= 'z') ||
(b >= 'A' && b <= 'Z') ||
b == '-' || b == '_'
}
50 changes: 50 additions & 0 deletions plugin/markdown/extensions/memos_ref_node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package extensions

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/yuin/goldmark"
gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/text"
)

func TestMemosRefExtensionMarksLinkNodes(t *testing.T) {
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
MemosRefExtension,
),
)

src := []byte("[memo](memos/abc-XYZ_9) and [other](https://example.com)")
doc := md.Parser().Parse(text.NewReader(src))

var links []*gast.Link
err := gast.Walk(doc, func(n gast.Node, entering bool) (gast.WalkStatus, error) {
if !entering {
return gast.WalkContinue, nil
}
if l, ok := n.(*gast.Link); ok {
links = append(links, l)
}
return gast.WalkContinue, nil
})
require.NoError(t, err)
require.Len(t, links, 2)

id0, ok0 := links[0].AttributeString(AttrMemosRefID)
id1, ok1 := links[1].AttributeString(AttrMemosRefID)

// One of them should be marked as memos ref, the other should not.
if ok0 {
assert.Equal(t, "abc-XYZ_9", id0)
assert.False(t, ok1)
} else {
require.True(t, ok1)
assert.Equal(t, "abc-XYZ_9", id1)
assert.False(t, ok0)
}
}
40 changes: 37 additions & 3 deletions plugin/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package markdown

import (
"bytes"
"fmt"
"strings"

"github.com/yuin/goldmark"
Expand All @@ -21,6 +22,10 @@ import (
type ExtractedData struct {
Tags []string
Property *storepb.MemoPayload_Property

// MemoRefNames contains related memo resource names in the form `memos/<id>` extracted
// from markdown links.
MemoRefNames []string
}

// Service handles markdown metadata extraction.
Expand Down Expand Up @@ -62,7 +67,8 @@ type service struct {
type Option func(*config)

type config struct {
enableTags bool
enableTags bool
enableMemosRef bool
}

// WithTagExtension enables #tag parsing.
Expand All @@ -72,6 +78,13 @@ func WithTagExtension() Option {
}
}

// WithMemosRefExtension enables memos/<id> reference parsing.
func WithMemosRefExtension() Option {
return func(c *config) {
c.enableMemosRef = true
}
}

// NewService creates a new markdown service with the given options.
func NewService(opts ...Option) Service {
cfg := &config{}
Expand All @@ -88,6 +101,10 @@ func NewService(opts ...Option) Service {
exts = append(exts, extensions.TagExtension)
}

if cfg.enableMemosRef {
exts = append(exts, extensions.MemosRefExtension)
}

md := goldmark.New(
goldmark.WithExtensions(exts...),
goldmark.WithParserOptions(
Expand Down Expand Up @@ -293,10 +310,13 @@ func (s *service) ExtractAll(content []byte) (*ExtractedData, error) {
}

data := &ExtractedData{
Tags: []string{},
Property: &storepb.MemoPayload_Property{},
Tags: []string{},
Property: &storepb.MemoPayload_Property{},
MemoRefNames: []string{},
}

memoRefSeen := map[string]bool{}

// Single walk to collect all data
err = gast.Walk(root, func(n gast.Node, entering bool) (gast.WalkStatus, error) {
if !entering {
Expand All @@ -313,6 +333,20 @@ func (s *service) ExtractAll(content []byte) (*ExtractedData, error) {
case gast.KindLink:
data.Property.HasLink = true

// If this link is an internal memos reference, the memos ref extension will
// have annotated it with an attribute.
if link, ok := n.(*gast.Link); ok {
if v, found := link.AttributeString(extensions.AttrMemosRefID); found {
if id, ok := v.(string); ok && id != "" {
name := fmt.Sprintf("memos/%s", id)
if !memoRefSeen[name] {
memoRefSeen[name] = true
data.MemoRefNames = append(data.MemoRefNames, name)
}
}
}
}

case gast.KindCodeBlock, gast.KindFencedCodeBlock, gast.KindCodeSpan:
data.Property.HasCode = true

Expand Down
45 changes: 45 additions & 0 deletions plugin/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,51 @@ func TestTruncateAtWord(t *testing.T) {
}
}

func TestExtractAllExtractsMemoRefNames(t *testing.T) {
svc := NewService(WithMemosRefExtension())

tests := []struct {
content string
expected []string
}{
{
content: "See [a](memos/abc)",
expected: []string{"memos/abc"},
},
{
content: "See [a](memos/abc) and [b](memos/xyz)",
expected: []string{"memos/abc", "memos/xyz"},
},
{
content: "See [a](https://example.com/memos/abc)",
expected: []string{"memos/abc"},
},
{
content: "See [a](https://example.com/memos/abc) and [b](https://example.com/memos/xyz)",
expected: []string{"memos/abc", "memos/xyz"},
},
{
content: "See \n [a](memos/abc) and \n [b](https://example.com/memos/123) and \n [c](memos/abc) and \n [d](memos/xyz)",
expected: []string{"memos/abc", "memos/xyz", "memos/123"},
},
{
content: "See [a](https://example.com/no-memos-link) and [b](https://another.com/memos/456)",
expected: []string{"memos/456"},
},
{
content: "See [a](memos/123) and also [b](memos/123)",
expected: []string{"memos/123"},
},
}

for _, tt := range tests {
data, err := svc.ExtractAll([]byte(tt.content))
require.NoError(t, err)
require.NotNil(t, data)
assert.ElementsMatch(t, tt.expected, data.MemoRefNames)
}
}

// Benchmark tests.
func BenchmarkGenerateSnippet(b *testing.B) {
svc := NewService()
Expand Down
23 changes: 17 additions & 6 deletions proto/gen/store/memo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions proto/store/memo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ message MemoPayload {

repeated string tags = 3;

// Related memo resource names extracted from markdown links.
// Format: memos/{memo}
repeated string memo_ref_names = 4;

// The calculated properties from the memo content.
message Property {
bool has_link = 1;
Expand Down
17 changes: 17 additions & 0 deletions server/router/api/v1/memo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ func (s *APIV1Service) CreateMemo(ctx context.Context, request *v1pb.CreateMemoR
if err := memopayload.RebuildMemoPayload(create, s.MarkdownService); err != nil {
return nil, status.Errorf(codes.Internal, "failed to rebuild memo payload: %v", err)
}

// If markdown extraction found memo references, convert them into relations.
if create.Payload != nil {
if refs := create.Payload.GetMemoRefNames(); len(refs) > 0 {
relations := make([]*v1pb.MemoRelation, 0, len(refs))
for _, name := range refs {
if name == "" {
continue
}
relations = append(relations, &v1pb.MemoRelation{
RelatedMemo: &v1pb.MemoRelation_Memo{Name: name},
Type: v1pb.MemoRelation_REFERENCE,
})
}
request.Memo.Relations = relations
}
}
if request.Memo.Location != nil {
create.Payload.Location = convertLocationToStore(request.Memo.Location)
}
Expand Down
Loading