be able to run multiple code blocks in a single slide

This commit is contained in:
林玮 (Jade Lin)
2021-09-01 11:16:15 +08:00
committed by Maas Lalani
parent 4e3be668eb
commit 68adc0a622
2 changed files with 28 additions and 15 deletions

View File

@@ -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 (