-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix markdown meta parsing #12817
Fix markdown meta parsing #12817
Conversation
Signed-off-by: jolheiser <john.olheiser@gmail.com>
Signed-off-by: jolheiser <john.olheiser@gmail.com>
Signed-off-by: jolheiser <john.olheiser@gmail.com>
Codecov Report
@@ Coverage Diff @@
## master #12817 +/- ##
==========================================
+ Coverage 43.13% 43.16% +0.02%
==========================================
Files 654 654
Lines 72205 72205
==========================================
+ Hits 31146 31165 +19
+ Misses 36013 35990 -23
- Partials 5046 5050 +4
Continue to review full report at Codecov.
|
Signed-off-by: jolheiser <john.olheiser@gmail.com>
@@ -25,20 +25,22 @@ func isYAMLSeparator(line string) bool { | |||
// and returns the frontmatter metadata separated from the markdown content | |||
func ExtractMetadata(contents string, out interface{}) (string, error) { | |||
var front, body []string | |||
var seps int | |||
lines := strings.Split(contents, "\n") | |||
for idx, line := range lines { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to use Scanner
.
scanner := bufio.NewScanner(strings.NewReader(contents))
var idx = 0
for scanner.Scan() {
line := scanner.Text()
...
idx++
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the advantage in this case?
We still need to scan the whole file so we can return body, and we've already checked file size prior to calling this func.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Less CPU and memory usage. But since it should be a small file, it's not necessary.
Fixes #12815
This code should now exclude the separator lines themselves, so as long as there are two or more hyphens it will work as a separator and also YAML won't choke.
Included a minimal test case.