diff --git a/public/swagger/swagger-who.yaml b/public/swagger/swagger-who.yaml index 8c6a44e..89d70cd 100644 --- a/public/swagger/swagger-who.yaml +++ b/public/swagger/swagger-who.yaml @@ -7,6 +7,7 @@ paths: - $ref: "#/components/parameters/id" - $ref: "#/components/parameters/namespace" - $ref: "#/components/parameters/light" + - $ref: "#/components/parameters/md" - $ref: "#/components/parameters/sort" - $ref: "#/components/parameters/order" responses: @@ -115,6 +116,13 @@ components: schema: type: string + md: + name: md + in: query + description: "Include a html rendered markdown notes section as .md" + schema: + type: string + order: name: order in: query diff --git a/view/who.go b/view/who.go index fd22c92..1e98330 100644 --- a/view/who.go +++ b/view/who.go @@ -16,6 +16,9 @@ import ( "strings" "github.com/buger/jsonparser" + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" "github.com/iancoleman/orderedmap" "go.mongodb.org/mongo-driver/bson" ) @@ -57,6 +60,7 @@ func whoGet(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, return whoTrace(namespace, g, w, r) } _, light := r.URL.Query()["light"] + _, md := r.URL.Query()["md"] ones, err := g.ListCaseInsensitive(r.Context(), namespace, id) if err != nil { @@ -103,6 +107,21 @@ func whoGet(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, return err } } + if md { + m := bson.M{} + if err := json.Unmarshal(b, &m); err != nil { + return err + } + + renderer := html.NewRenderer(html.RendererOptions{Flags: html.CommonFlags | html.TOC}) + parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock) + m["md"] = string(markdown.ToHTML([]byte(one.Text), parser, renderer)) + + b, err = json.MarshalIndent(m, "", " ") + if err != nil { + return err + } + } _, err = io.Copy(w, bytes.NewReader(b)) return err diff --git a/view/who_test.go b/view/who_test.go index 830c9a4..7d8707f 100644 --- a/view/who_test.go +++ b/view/who_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "encoding/xml" "fmt" "io/ioutil" "local/dndex/config" @@ -19,6 +20,7 @@ import ( "testing" "time" + "github.com/buger/jsonparser" "github.com/google/uuid" ) @@ -103,6 +105,43 @@ func TestWho(t *testing.T) { t.Logf("POST GET:\n%s", b) }) + t.Run("get real md", func(t *testing.T) { + reset() + iwant := want + r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&md&id="+iwant.Name, nil) + w := httptest.NewRecorder() + handler.ServeHTTP(w, r) + if w.Code != http.StatusOK { + t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) + } + o := entity.One{} + if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { + t.Fatal(err) + } + if fmt.Sprint(o) == fmt.Sprint(iwant) { + t.Fatal(o, iwant) + } + if len(o.Connections) != len(iwant.Connections) { + t.Fatal(len(o.Connections), len(iwant.Connections)) + } + iwant.Connections = o.Connections + iwant.Modified = 0 + o.Modified = 0 + if fmt.Sprint(o) != fmt.Sprint(iwant) { + t.Fatalf("after resolving connections and modified, iwant and got differ: \nwant %+v\n got %+v", iwant, o) + } + var paragraphContent string + if v, err := jsonparser.GetString(w.Body.Bytes(), "md"); err != nil { + t.Fatal(err) + } else if err := xml.Unmarshal([]byte(v), ¶graphContent); err != nil { + t.Fatal(err) + } else if paragraphContent != iwant.Text { + t.Fatal(iwant.Text, paragraphContent, v) + } + b, _ := json.MarshalIndent(o, "", " ") + t.Logf("POST GET:\n%s", b) + }) + t.Run("get real light", func(t *testing.T) { reset() iwant := want