mirror of
https://github.com/cheat/cheat.git
synced 2026-03-07 03:03:32 +01:00
- Add .gitattributes to force LF in mock files (Windows autocrlf) - Fix parse.go: detect line endings from content instead of runtime.GOOS - Add fail-fast: false to CI matrix; trigger on all branch pushes - Skip chmod-based tests on Windows (permissions work differently) - Use filepath.Join for expected paths in Windows path tests - Use platform-appropriate invalid paths in error tests - Add Windows absolute path test case for ValidateSheetName - Skip Unix-specific integration tests on Windows Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package sheet
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Parse parses cheatsheet frontmatter
|
|
func parse(markdown string) (frontmatter, string, error) {
|
|
|
|
// detect the line-break style used in the content
|
|
linebreak := "\n"
|
|
if strings.Contains(markdown, "\r\n") {
|
|
linebreak = "\r\n"
|
|
}
|
|
|
|
// specify the frontmatter delimiter
|
|
delim := fmt.Sprintf("---%s", linebreak)
|
|
|
|
// initialize a frontmatter struct
|
|
var fm frontmatter
|
|
|
|
// if the markdown does not contain frontmatter, pass it through unmodified
|
|
if !strings.HasPrefix(markdown, delim) {
|
|
return fm, markdown, nil
|
|
}
|
|
|
|
// otherwise, split the frontmatter and cheatsheet text
|
|
parts := strings.SplitN(markdown, delim, 3)
|
|
|
|
// return an error if the frontmatter parses into the wrong number of parts
|
|
if len(parts) != 3 {
|
|
return fm, markdown, fmt.Errorf("failed to delimit frontmatter")
|
|
}
|
|
|
|
// return an error if the YAML cannot be unmarshalled
|
|
if err := yaml.Unmarshal([]byte(parts[1]), &fm); err != nil {
|
|
return fm, markdown, fmt.Errorf("failed to unmarshal frontmatter: %v", err)
|
|
}
|
|
|
|
return fm, parts[2], nil
|
|
}
|