Skip to content

Commit

Permalink
feat: support to read multiple md files
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Feb 15, 2023
1 parent 8d67ac0 commit edad21c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
33 changes: 24 additions & 9 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -21,7 +22,7 @@ func NewRootCommand() (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "mde",
Example: "mde README.md",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
RunE: opt.runE,
}
cmd.Version = version
Expand All @@ -33,10 +34,27 @@ func NewRootCommand() (cmd *cobra.Command) {
}

func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
mdFilePath := args[0]
scriptRunners := NewScriptRunners()

for _, mdFilePath := range args {
var files []string
if files, err = filepath.Glob(mdFilePath); err != nil {
return
} else {
for _, file := range files {
fmt.Println(file)
var runners ScriptRunners
if runners, err = o.parseMarkdownRunner(file); err != nil {
break
}

scriptRunners = append(scriptRunners, runners...)
}
}
}

for {
err = o.runMarkdown(mdFilePath)
err = o.executeScripts(scriptRunners)

if !o.loop {
break
Expand All @@ -45,20 +63,18 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
return
}

func (o *option) runMarkdown(mdFilePath string) (err error) {
func (o *option) parseMarkdownRunner(mdFilePath string) (scriptList ScriptRunners, err error) {
var mdFile []byte
mdFile, err = os.ReadFile(mdFilePath)
if err != nil {
return
}

//Parse the markdown
// Parse the markdown
scriptList = ScriptRunners{}
md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true))
tokens := md.Parse(mdFile)

// cmdMap := map[string][]string{}
scriptList := NewScriptRunners()

// Print the result
var title string
for _, t := range tokens {
Expand Down Expand Up @@ -107,7 +123,6 @@ func (o *option) runMarkdown(mdFilePath string) (err error) {
})
}
}
err = o.executeScripts(scriptList)
return
}

Expand Down
4 changes: 0 additions & 4 deletions cli/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ func TestNewRootCommand(t *testing.T) {
name: "no argument",
args: nil,
hasErr: true,
}, {
name: "more than one argument",
args: []string{"a", "b"},
hasErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit edad21c

Please sign in to comment.