Commit 22fd8ac3 authored by Eric Paris's avatar Eric Paris

Speed up mungedoc TOC by pre-compiling a regex

Brings the time to run the TOC over the docs directory from .7 seconds to .1 seconds
parent 853ea5ba
...@@ -57,12 +57,13 @@ func buildTOC(markdown []byte) ([]byte, error) { ...@@ -57,12 +57,13 @@ func buildTOC(markdown []byte) ([]byte, error) {
buffer.WriteString("\n") buffer.WriteString("\n")
scanner := bufio.NewScanner(bytes.NewReader(markdown)) scanner := bufio.NewScanner(bytes.NewReader(markdown))
inBlockQuotes := false inBlockQuotes := false
blockQuoteRegex, err := regexp.Compile("^```")
if err != nil {
return nil, err
}
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
match, err := regexp.Match("^```", []byte(line)) match := blockQuoteRegex.Match([]byte(line))
if err != nil {
return nil, err
}
if match { if match {
inBlockQuotes = !inBlockQuotes inBlockQuotes = !inBlockQuotes
continue continue
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment