mirror of
https://github.com/cheat/cheat.git
synced 2024-11-25 07:21:36 +01:00
fix(frontmatter): do not trim whitespace (#663)
Do not strip leading or trailing newlines. Doing so had interferred with users' intended cheatsheet layouts.
This commit is contained in:
parent
f1540290a7
commit
f0bfeda47a
@ -2,6 +2,7 @@ package frontmatter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/yaml.v1"
|
"gopkg.in/yaml.v1"
|
||||||
@ -16,15 +17,21 @@ type Frontmatter struct {
|
|||||||
// Parse parses cheatsheet frontmatter
|
// Parse parses cheatsheet frontmatter
|
||||||
func Parse(markdown string) (string, Frontmatter, error) {
|
func Parse(markdown string) (string, Frontmatter, error) {
|
||||||
|
|
||||||
|
// determine the appropriate line-break for the platform
|
||||||
|
linebreak := "\n"
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
linebreak = "\r\n"
|
||||||
|
}
|
||||||
|
|
||||||
// specify the frontmatter delimiter
|
// specify the frontmatter delimiter
|
||||||
delim := "---"
|
delim := fmt.Sprintf("---%s", linebreak)
|
||||||
|
|
||||||
// initialize a frontmatter struct
|
// initialize a frontmatter struct
|
||||||
var fm Frontmatter
|
var fm Frontmatter
|
||||||
|
|
||||||
// if the markdown does not contain frontmatter, pass it through unmodified
|
// if the markdown does not contain frontmatter, pass it through unmodified
|
||||||
if !strings.HasPrefix(markdown, delim) {
|
if !strings.HasPrefix(markdown, delim) {
|
||||||
return strings.TrimSpace(markdown), fm, nil
|
return markdown, fm, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, split the frontmatter and cheatsheet text
|
// otherwise, split the frontmatter and cheatsheet text
|
||||||
@ -40,5 +47,5 @@ func Parse(markdown string) (string, Frontmatter, error) {
|
|||||||
return markdown, fm, fmt.Errorf("failed to unmarshal frontmatter: %v", err)
|
return markdown, fm, fmt.Errorf("failed to unmarshal frontmatter: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.TrimSpace(parts[2]), fm, nil
|
return parts[2], fm, nil
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ func New(
|
|||||||
Title: title,
|
Title: title,
|
||||||
CheatPath: cheatpath,
|
CheatPath: cheatpath,
|
||||||
Path: path,
|
Path: path,
|
||||||
Text: text + "\n",
|
Text: text,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
Syntax: fm.Syntax,
|
Syntax: fm.Syntax,
|
||||||
ReadOnly: readOnly,
|
ReadOnly: readOnly,
|
||||||
|
Loading…
Reference in New Issue
Block a user