Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed markdown parser to as markdown didnt match gitlab. #721

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
29 changes: 20 additions & 9 deletions daemon/exercise.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package daemon

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"

"github.com/yuin/goldmark/renderer/html"

pb "github.com/aau-network-security/haaukins/daemon/proto"
eproto "github.com/aau-network-security/haaukins/exercise/ex-proto"
"github.com/aau-network-security/haaukins/store"
storeProto "github.com/aau-network-security/haaukins/store/proto"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"github.com/microcosm-cc/bluemonday"
"github.com/rs/zerolog/log"
"github.com/yuin/goldmark"
)

func (d *daemon) ListCategories(ctx context.Context, req *pb.Empty) (*pb.ListCategoriesResponse, error) {
Expand Down Expand Up @@ -45,11 +47,16 @@ func (d *daemon) ListCategories(ctx context.Context, req *pb.Empty) (*pb.ListCat

for _, c := range cats {
// Render markdown from orgdescription to html
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.HardLineBreak
parser := parser.NewWithExtensions(extensions)

md := []byte(c.CatDescription)
unsafeHtml := markdown.ToHTML(md, parser, nil)
var buf bytes.Buffer
renderer := goldmark.New(
goldmark.WithRendererOptions(html.WithUnsafe()),
)
if err := renderer.Convert(md, &buf); err != nil {
log.Error().Msgf("Error converting to commonmark: %s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Error().Msgf("Error converting to commonmark: %s", err)
log.Error().Err(err).Msg("converting to markdown")

Use the fields of the log package, might be nice if decided to do central logging for the distributed version later on as it makes it nicer to filter logs

}
unsafeHtml := buf.Bytes()
Comment on lines +52 to +59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make this part as function and just call it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha im so stupid, i'll do that as soon i get time! :D


//Sanitizing unsafe HTML with bluemonday
html := bluemonday.UGCPolicy().SanitizeBytes(unsafeHtml)
Expand Down Expand Up @@ -121,11 +128,15 @@ func (d *daemon) ListExercises(ctx context.Context, req *pb.Empty) (*pb.ListExer
}

// Render markdown from orgdescription to html
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.HardLineBreak
parser := parser.NewWithExtensions(extensions)

md := []byte(e.OrgDescription)
unsafeHtml := markdown.ToHTML(md, parser, nil)
var buf bytes.Buffer
renderer := goldmark.New(
goldmark.WithRendererOptions(html.WithUnsafe()),
)
if err := renderer.Convert(md, &buf); err != nil {
log.Error().Msgf("Error converting to commonmark: %s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my other comments

}
unsafeHtml := buf.Bytes()

//Sanitizing unsafe HTML with bluemonday
html := bluemonday.UGCPolicy().SanitizeBytes(unsafeHtml)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/giantswarm/semver-bump v0.0.0-20181008095244-e8413386a9b8
github.com/golang-jwt/jwt/v4 v4.4.1
github.com/golang/protobuf v1.5.2
github.com/gomarkdown/markdown v0.0.0-20220419181919-412bcf14cd2e
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0
github.com/juju/errgo v0.0.0-20140925100237-08cceb5d0b53 // indirect
Expand All @@ -21,11 +20,13 @@ require (
github.com/opencontainers/runc v1.1.1 // indirect
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.26.1
github.com/russross/blackfriday/v2 v2.1.0
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cobra v1.4.0
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/xanzy/ssh-agent v0.3.1 // indirect
github.com/yuin/goldmark v1.4.0
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,6 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/gomarkdown/markdown v0.0.0-20220419181919-412bcf14cd2e h1:eF2U4VaZBPyxJZQxz8b0ulG3Dw2yQ2kGRJ9Io/cGQeE=
github.com/gomarkdown/markdown v0.0.0-20220419181919-412bcf14cd2e/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
Expand Down Expand Up @@ -784,6 +782,7 @@ github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
Expand Down Expand Up @@ -890,6 +889,7 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.0 h1:OtISOGfH6sOWa1/qXqqAiOIAO6Z5J3AEAE18WAq6BiQ=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
Expand Down
19 changes: 13 additions & 6 deletions svcs/amigo/table.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package amigo

import (
"bytes"
"encoding/json"
"sort"
"time"

"github.com/rs/zerolog/log"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/renderer/html"

"github.com/aau-network-security/haaukins/store"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"github.com/microcosm-cc/bluemonday"
)

Expand Down Expand Up @@ -184,11 +187,15 @@ func (fd *FrontendData) initChallenges(teamId string) []byte {
}

//Render markdown to HTML
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.HardLineBreak
parser := parser.NewWithExtensions(extensions)

md := []byte(r.ChalInfo.TeamDescription)
unsafeHtml := markdown.ToHTML(md, parser, nil)
var buf bytes.Buffer
renderer := goldmark.New(
goldmark.WithRendererOptions(html.WithUnsafe()),
)
if err := renderer.Convert(md, &buf); err != nil {
log.Error().Msgf("Error converting to commonmark: %s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again log and errors shouldn't start with error

}
unsafeHtml := buf.Bytes()

//Sanitizing unsafe HTML with bluemonday
html := bluemonday.UGCPolicy().SanitizeBytes(unsafeHtml)
Expand Down