Skip to content

Commit

Permalink
Use markdown renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptonist committed Jan 10, 2019
1 parent 9186df7 commit 06efaf9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
30 changes: 28 additions & 2 deletions Gopkg.lock

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

16 changes: 16 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,26 @@
# unused-packages = true


[[constraint]]
name = "github.com/gdamore/tcell"
version = "1.1.1"

[[constraint]]
name = "github.com/mitchellh/go-homedir"
version = "1.0.0"

[[constraint]]
branch = "master"
name = "github.com/rivo/tview"

[[constraint]]
name = "github.com/russross/blackfriday"
version = "2.0.1"

[[constraint]]
name = "github.com/scriptonist/termd"
version = "0.0.3"

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.3"
Expand Down
26 changes: 21 additions & 5 deletions pkg/posts/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/gdamore/tcell"
"github.com/rivo/tview"
blackfriday "github.com/russross/blackfriday"
"github.com/scriptonist/termd/pkg/console"
)

// openPost opens a post in a new tview box
Expand Down Expand Up @@ -52,16 +54,21 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
ptype,
link,
"\n",
singlePost.Post.ContentMarkdown,
"\n[green]Responses[white]",
"[green]==========[white]",
renderTerminal(singlePost.Post.ContentMarkdown),
func() string {
if len(singlePost.Post.Responses) > 0 {
return fmt.Sprintf("\n%s\n%s\n", "[green]Responses[white]",
"[green]==========[white]")
}
return ""
}(),
)
for ind, response := range singlePost.Post.Responses {
writeToTextView(
textView,
fmt.Sprintf("\n%d", ind+1),
fmt.Sprintf("---"),
response.ContentMarkdown,
renderTerminal(response.ContentMarkdown),
)
if len(response.Replies) > 0 {
writeToTextView(textView,
Expand All @@ -74,7 +81,7 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
fmt.Sprintf("\n\t%d", indreply+1),
fmt.Sprintf("\t---"),
fmt.Sprintf("\tAuthor: %s", reply.Author.Name),
fmt.Sprintf("\t%s", reply.ContentMarkdown),
fmt.Sprintf("\t%s", renderTerminal(reply.ContentMarkdown)),
)

}
Expand Down Expand Up @@ -106,3 +113,12 @@ func writeToTextView(t *tview.TextView, contents ...string) {
t.Write([]byte("\n"))
}
}

func renderTerminal(content string) string {
r := console.Console{}
out := string(blackfriday.Run([]byte(content),
blackfriday.WithRenderer(r),
blackfriday.WithExtensions(blackfriday.CommonExtensions)))
return out

}

0 comments on commit 06efaf9

Please sign in to comment.