@@ -2,6 +2,7 @@ package main
22
33import (
44 "fmt"
5+ "io"
56 "os"
67 "path/filepath"
78
@@ -15,49 +16,60 @@ import (
1516
1617var (
1718 plainMode bool
18- watchMode bool
1919)
2020
2121var rootCmd = & cobra.Command {
22- Use : "md [flags] < markdown-file> " ,
22+ Use : "md [flags] [ markdown-file] " ,
2323 Short : "A markdown renderer and viewer for the terminal" ,
2424 Long : `md is a command-line tool that renders markdown files with syntax highlighting
25- and provides options for vim-style navigation.` ,
26- Args : cobra .ExactArgs (1 ),
27- RunE : func (cmd * cobra.Command , args []string ) error {
28- filename := args [0 ]
29- if ! filepath .IsAbs (filename ) {
30- var err error
31- filename , err = filepath .Abs (filename )
32- if err != nil {
33- return fmt .Errorf ("error resolving file path: %v" , err )
34- }
35- }
36-
37- if _ , err := os .Stat (filename ); os .IsNotExist (err ) {
38- return fmt .Errorf ("file not found: %s" , filename )
39- }
25+ and provides options for vim-style navigation.
4026
27+ If no file is provided, md reads from stdin.` ,
28+ Args : cobra .MaximumNArgs (1 ),
29+ RunE : func (cmd * cobra.Command , args []string ) error {
4130 themeManager := theme .New ()
4231 mdRenderer := renderer .New (themeManager )
4332 codeHighlighter := highlighter .New (themeManager )
4433 mdViewer := viewer .New ()
4534
46- renderAndDisplay := func () error {
47- content , err := mdRenderer .RenderFile (filename , codeHighlighter )
48- if err != nil {
49- return fmt .Errorf ("rendering error: %v" , err )
35+ var content string
36+ var err error
37+
38+ if len (args ) == 0 || args [0 ] == "-" {
39+ // Read from stdin
40+ stdinContent , readErr := io .ReadAll (os .Stdin )
41+ if readErr != nil {
42+ return fmt .Errorf ("error reading from stdin: %v" , readErr )
43+ }
44+ if len (stdinContent ) == 0 {
45+ return fmt .Errorf ("no input provided" )
46+ }
47+ content , err = mdRenderer .RenderContent (stdinContent , codeHighlighter )
48+ } else {
49+ filename := args [0 ]
50+ if ! filepath .IsAbs (filename ) {
51+ filename , err = filepath .Abs (filename )
52+ if err != nil {
53+ return fmt .Errorf ("error resolving file path: %v" , err )
54+ }
5055 }
5156
52- if plainMode {
53- fmt .Print (content )
54- return nil
55- } else {
56- return mdViewer .DisplayInVimMode (content )
57+ if _ , statErr := os .Stat (filename ); os .IsNotExist (statErr ) {
58+ return fmt .Errorf ("file not found: %s" , filename )
5759 }
60+
61+ content , err = mdRenderer .RenderFile (filename , codeHighlighter )
5862 }
5963
60- return renderAndDisplay ()
64+ if err != nil {
65+ return fmt .Errorf ("rendering error: %v" , err )
66+ }
67+
68+ if plainMode {
69+ fmt .Print (content )
70+ return nil
71+ }
72+ return mdViewer .DisplayInVimMode (content )
6173 },
6274}
6375
0 commit comments