Skip to content

Commit 7e7dd8d

Browse files
authored
feat: support to read multiple md files (#12)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
1 parent 0f518de commit 7e7dd8d

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

cli/root.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path"
8+
"path/filepath"
89
"strings"
910

1011
"github.com/AlecAivazis/survey/v2"
@@ -21,7 +22,7 @@ func NewRootCommand() (cmd *cobra.Command) {
2122
cmd = &cobra.Command{
2223
Use: "mde",
2324
Example: "mde README.md",
24-
Args: cobra.ExactArgs(1),
25+
Args: cobra.MinimumNArgs(1),
2526
RunE: opt.runE,
2627
}
2728
cmd.Version = version
@@ -33,10 +34,26 @@ func NewRootCommand() (cmd *cobra.Command) {
3334
}
3435

3536
func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
36-
mdFilePath := args[0]
37+
scriptRunners := NewScriptRunners()
38+
39+
for _, mdFilePath := range args {
40+
var files []string
41+
if files, err = filepath.Glob(mdFilePath); err != nil {
42+
return
43+
}
44+
45+
for _, file := range files {
46+
var runners ScriptRunners
47+
if runners, err = o.parseMarkdownRunner(file); err != nil {
48+
break
49+
}
50+
51+
scriptRunners = append(scriptRunners, runners...)
52+
}
53+
}
3754

3855
for {
39-
err = o.runMarkdown(mdFilePath)
56+
err = o.executeScripts(scriptRunners)
4057

4158
if !o.loop {
4259
break
@@ -45,20 +62,18 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
4562
return
4663
}
4764

48-
func (o *option) runMarkdown(mdFilePath string) (err error) {
65+
func (o *option) parseMarkdownRunner(mdFilePath string) (scriptList ScriptRunners, err error) {
4966
var mdFile []byte
5067
mdFile, err = os.ReadFile(mdFilePath)
5168
if err != nil {
5269
return
5370
}
5471

55-
//Parse the markdown
72+
// Parse the markdown
73+
scriptList = ScriptRunners{}
5674
md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true))
5775
tokens := md.Parse(mdFile)
5876

59-
// cmdMap := map[string][]string{}
60-
scriptList := NewScriptRunners()
61-
6277
// Print the result
6378
var title string
6479
for _, t := range tokens {
@@ -111,7 +126,6 @@ func (o *option) runMarkdown(mdFilePath string) (err error) {
111126
})
112127
}
113128
}
114-
err = o.executeScripts(scriptList)
115129
return
116130
}
117131

cli/root_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ func TestNewRootCommand(t *testing.T) {
1515
name: "no argument",
1616
args: nil,
1717
hasErr: true,
18-
}, {
19-
name: "more than one argument",
20-
args: []string{"a", "b"},
21-
hasErr: true,
2218
}}
2319
for _, tt := range tests {
2420
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)