From 6562556fc188b8d903d6bf7227037ed2a0c10242 Mon Sep 17 00:00:00 2001 From: kanekv Date: Fri, 31 Mar 2023 17:39:01 -0700 Subject: [PATCH 1/2] allow map-based embeds --- raw_op.go | 17 +++++++++++++++++ render.go | 9 +++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/raw_op.go b/raw_op.go index d4f58c6..1c51af0 100644 --- a/raw_op.go +++ b/raw_op.go @@ -34,6 +34,7 @@ func (ro *rawOp) makeOp(o *Op) error { for mk := range ins { o.Type = mk o.Data = extractString(ins[mk]) + o.DataMap = extractMap(ins[mk]) break } default: @@ -69,3 +70,19 @@ func extractString(v interface{}) string { } return "" } + +func extractMap(v interface{}) map[string]string { + switch val := v.(type) { + case map[string]interface{}: + newMap := make(map[string]string) + for key, value := range val { + if strValue, ok := value.(string); ok { + newMap[key] = strValue + } else { + newMap[key] = fmt.Sprint(value) + } + } + return newMap + } + return make(map[string]string) +} diff --git a/render.go b/render.go index 7ae1ce8..15e5182 100644 --- a/render.go +++ b/render.go @@ -133,9 +133,10 @@ func (o *Op) addFmTer(vars *renderVars, fmTer Formatter) { // An Op is a Delta insert operations (https://github.com/quilljs/delta#insert) that has been converted into this format for // usability with the type safety in Go. type Op struct { - Data string // the text to insert or the value of the embed object (http://quilljs.com/docs/delta/#embeds) - Type string // the type of the op (typically "text", but any other type can be registered) - Attrs map[string]string // key is attribute name; value is either the attribute value or "y" (meaning true) + Data string // the text to insert or the value of the embed object (http://quilljs.com/docs/delta/#embeds) + DataMap map[string]string + Type string // the type of the op (typically "text", but any other type can be registered) + Attrs map[string]string // key is attribute name; value is either the attribute value or "y" (meaning true) } // writeBlock writes a block element (which may be nested inside another block element if it is a FormatWrapper). @@ -393,7 +394,7 @@ type Format struct { // A blankOp can be used to signal any FormatWrapper formats to write the final closing wrap. func blankOp() *Op { - return &Op{"", "text", make(map[string]string)} + return &Op{"", make(map[string]string), "text", make(map[string]string)} } // If cl has something, then classesList returns the class attribute to add to an HTML element with a space before the From 086af9886f9ac47feb03ceb4f60e07a1a954d9ea Mon Sep 17 00:00:00 2001 From: kanekv Date: Fri, 31 Mar 2023 17:54:15 -0700 Subject: [PATCH 2/2] append trailing \n (sometimes delta doesn't have one) --- render.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/render.go b/render.go index 15e5182..d971d2f 100644 --- a/render.go +++ b/render.go @@ -36,6 +36,9 @@ func RenderExtended(ops []byte, customFormats func(string, *Op) Formatter) ([]by o: Op{Attrs: make(map[string]string, 3)}, } + raw = append(raw, rawOp{ + Insert: "\n", + }) for i := range raw { if err := raw[i].makeOp(&vars.o); err != nil {