Skip to content

Commit ad4f464

Browse files
committed
feat: add stdin support for piped input
- Allow reading markdown from stdin when no file argument provided - Support explicit '-' argument to read from stdin - Update README with stdin usage examples - Update command usage to reflect optional file argument
1 parent 26aface commit ad4f464

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
md

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,28 @@ go build -o md .
2828

2929
```
3030
Usage:
31-
md [flags] <markdown-file>
31+
md [flags] [markdown-file]
3232
3333
Flags:
34-
-h, --help help for md
35-
-v, --vim Enable vim-style navigation
34+
-h, --help help for md
35+
-p, --plain Render entire markdown to stdout (no pager)
3636
```
3737

38+
By default, `md` displays content in a vim-style pager. Use `--plain` to output directly to stdout.
39+
40+
### Reading from stdin
41+
42+
You can pipe markdown content directly to `md`:
43+
44+
```bash
45+
cat README.md | md
46+
echo "# Hello World" | md
47+
curl -s https://example.com/doc.md | md
48+
```
3849

3950
### Vim Navigation Keys
4051

41-
When using `--vim` mode, you can navigate using:
52+
In the default pager mode, you can navigate using:
4253

4354
- `j` / `k` - Move down/up
4455
- `gg` - Go to top

main.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"path/filepath"
78

@@ -15,49 +16,60 @@ import (
1516

1617
var (
1718
plainMode bool
18-
watchMode bool
1919
)
2020

2121
var 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

Comments
 (0)