Skip to content

Commit

Permalink
Refactored markdown rendering, fixed #7
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Aug 3, 2022
1 parent bbb6a35 commit 71c16a2
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,11 @@ func getReaderFromStdin() (io.ReadCloser, error) {
return io.NopCloser(bufio.NewReader(os.Stdin)), nil
}

func RenderImg(title, md *string) (string, error) {
func RenderImg(md string) (string, []InlineImage, error) {
var images []InlineImage

width, _, err := terminal.GetSize(0)
if err != nil {
width = 80
}

markdown := mdImgRegex.
ReplaceAllStringFunc(*md, func(md string) (string) {
ReplaceAllStringFunc(md, func(md string) (string) {
imgs := mdImgRegex.FindAllStringSubmatch(md, -1)
if len(imgs) < 1 {
return md
Expand All @@ -150,15 +145,23 @@ func RenderImg(title, md *string) (string, error) {
return fmt.Sprintf("$$$%d$", inlineImageIndex)
})

return markdown, images, nil
}

func RenderMarkdown(title, markdown string, images []InlineImage) (string, error) {
width, _, err := terminal.GetSize(0)
if err != nil {
width = 80
}

renderer, _ := glamour.NewTermRenderer(
glamour.WithEnvironmentConfig(),
glamour.WithWordWrap(width),
)


output, err :=
renderer.Render(
fmt.Sprintf("# %s\n\n%s", *title, markdown),
fmt.Sprintf("# %s\n\n%s", title, markdown),
)
if err != nil {
output = fmt.Sprintf("%v", err)
Expand Down Expand Up @@ -222,14 +225,17 @@ var rootCmd = &cobra.Command{

if noPretty == true {
fmt.Print(markdown)
fmt.Println("")
os.Exit(0)
}

output := markdown
var images []InlineImage
if noImages == false {
output, err = RenderImg(&title, &markdown)
output, images, err = RenderImg(markdown)
}

output, err = RenderMarkdown(title, output, images)
fmt.Print(output)
},
}
Expand Down

0 comments on commit 71c16a2

Please sign in to comment.