diff --git a/internal/code/code.go b/internal/code/code.go index 402d84a..6c90ef4 100644 --- a/internal/code/code.go +++ b/internal/code/code.go @@ -21,7 +21,7 @@ type Result struct { } // ?: means non-capture group -var re = regexp.MustCompile("(?s)(?:```|~~~)(\\w+)\n(.*)\n(?:```|~~~)\\s?") +var re = regexp.MustCompile("(?s)(?:```|~~~)(\\w+)\n(.*?)\n(?:```|~~~)\\s?") var ( ErrParse = errors.New("Error: could not parse code block") @@ -29,19 +29,28 @@ var ( // Parse takes a block of markdown and returns an array of Block's with code // and associated languages -func Parse(markdown string) (Block, error) { - match := re.FindStringSubmatch(markdown) +func Parse(markdown string) ([]Block, error) { + matchs := re.FindAllStringSubmatch(markdown, -1) + + var rv []Block + for _, match := range matchs { + // There was either no language specified or no code block + // Either way, we cannot execute the expression + if len(match) < 3 { + continue + } + rv = append(rv, Block{ + Language: match[1], + Code: match[2], + }) - // There was either no language specified or no code block - // Either way, we cannot execute the expression - if len(match) < 3 { - return Block{}, ErrParse } - return Block{ - Language: match[1], - Code: match[2], - }, nil + if len(rv) == 0 { + return nil, ErrParse + } + + return rv, nil } const ( diff --git a/internal/model/model.go b/internal/model/model.go index 094d722..9003c27 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -113,15 +113,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } m.VirtualText = "" case "ctrl+e": - // Run code block - block, err := code.Parse(m.Slides[m.Page]) + // Run code blocks + blocks, err := code.Parse(m.Slides[m.Page]) if err != nil { // We couldn't parse the code block on the screen m.VirtualText = "\n" + err.Error() return m, nil } - res := code.Execute(block) - m.VirtualText = res.Out + var outs []string + for _, block := range blocks { + res := code.Execute(block) + outs = append(outs, res.Out) + } + m.VirtualText = strings.Join(outs, "\n") } case fileWatchMsg: