back interdiff of round #2 and #1

appview: parse reference links from markdown body #760

merged
opened by boltless.me targeting master from feat/mentions

Defined refResolver which will parse useful data from markdown body like @-mentions or issue/pr/comment mentions

Signed-off-by: Seongmin Lee git@boltless.me

ERROR
appview/db/reference.go

Failed to calculate interdiff for this file.

ERROR
appview/issues/issues.go

Failed to calculate interdiff for this file.

ERROR
appview/models/reference.go

Failed to calculate interdiff for this file.

REVERTED
appview/pages/markup/markdown_at_extension.go
···
util.Prioritized(NewAtHTMLRenderer(), 500),
))
}
···
util.Prioritized(NewAtHTMLRenderer(), 500),
))
}
+
+
// FindUserMentions returns Set of user handles from given markup soruce.
+
// It doesn't guarntee unique DIDs
+
func FindUserMentions(source string) []string {
+
var (
+
mentions []string
+
mentionsSet = make(map[string]struct{})
+
md = NewMarkdown()
+
sourceBytes = []byte(source)
+
root = md.Parser().Parse(text.NewReader(sourceBytes))
+
)
+
ast.Walk(root, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
+
if entering && n.Kind() == KindAt {
+
handle := n.(*AtNode).handle
+
mentionsSet[handle] = struct{}{}
+
return ast.WalkSkipChildren, nil
+
}
+
return ast.WalkContinue, nil
+
})
+
for handle := range mentionsSet {
+
mentions = append(mentions, handle)
+
}
+
return mentions
+
}
ERROR
appview/pages/markup/reference_link.go

Failed to calculate interdiff for this file.

ERROR
appview/pulls/pulls.go

Failed to calculate interdiff for this file.

ERROR
appview/refresolver/resolver.go

Failed to calculate interdiff for this file.

ERROR
appview/state/router.go

Failed to calculate interdiff for this file.

ERROR
appview/state/state.go

Failed to calculate interdiff for this file.

NEW
appview/pages/markup/extension/atlink.go
···
// An AtNode struct represents an AtNode
type AtNode struct {
-
handle string
ast.BaseInline
}
···
block.Advance(m[1])
node := &AtNode{}
node.AppendChild(node, ast.NewTextSegment(atSegment))
-
node.handle = string(atSegment.Value(block.Source())[1:])
return node
}
···
func (r *atHtmlRenderer) renderAt(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
w.WriteString(`<a href="/@`)
-
w.WriteString(n.(*AtNode).handle)
w.WriteString(`" class="mention">`)
} else {
w.WriteString("</a>")
···
// An AtNode struct represents an AtNode
type AtNode struct {
+
Handle string
ast.BaseInline
}
···
block.Advance(m[1])
node := &AtNode{}
node.AppendChild(node, ast.NewTextSegment(atSegment))
+
node.Handle = string(atSegment.Value(block.Source())[1:])
return node
}
···
func (r *atHtmlRenderer) renderAt(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
w.WriteString(`<a href="/@`)
+
w.WriteString(n.(*AtNode).Handle)
w.WriteString(`" class="mention">`)
} else {
w.WriteString("</a>")
NEW
appview/pages/markup/markdown.go
···
return md
}
func (rctx *RenderContext) RenderMarkdown(source string) string {
md := NewMarkdown()
···
return md
}
+
// FindUserMentions returns Set of user handles from given markup soruce.
+
// It doesn't guarntee unique DIDs
+
func FindUserMentions(source string) []string {
+
var (
+
mentions []string
+
mentionsSet = make(map[string]struct{})
+
md = NewMarkdown()
+
sourceBytes = []byte(source)
+
root = md.Parser().Parse(text.NewReader(sourceBytes))
+
)
+
ast.Walk(root, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
+
if entering && n.Kind() == textension.KindAt {
+
handle := n.(*textension.AtNode).Handle
+
mentionsSet[handle] = struct{}{}
+
return ast.WalkSkipChildren, nil
+
}
+
return ast.WalkContinue, nil
+
})
+
for handle := range mentionsSet {
+
mentions = append(mentions, handle)
+
}
+
return mentions
+
}
+
func (rctx *RenderContext) RenderMarkdown(source string) string {
md := NewMarkdown()