mirror of
https://github.com/kovetskiy/mark.git
synced 2026-05-02 05:12:35 +00:00
feat: add support for --content-appearance
This commit is contained in:
committed by
Manuel Rüger
parent
d2717f031d
commit
9516939c7d
@@ -51,7 +51,7 @@ var (
|
||||
reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`)
|
||||
)
|
||||
|
||||
func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, titleFromFilename bool, filename string, parents []string, titleAppendGeneratedHash bool) (*Meta, []byte, error) {
|
||||
func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, titleFromFilename bool, filename string, parents []string, titleAppendGeneratedHash bool, defaultContentAppearance string) (*Meta, []byte, error) {
|
||||
var (
|
||||
meta *Meta
|
||||
offset int
|
||||
@@ -81,7 +81,6 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, titleFromFi
|
||||
if meta == nil {
|
||||
meta = &Meta{}
|
||||
meta.Type = "page" // Default if not specified
|
||||
meta.ContentAppearance = FullWidthContentAppearance // Default to full-width for backwards compatibility
|
||||
}
|
||||
|
||||
header := cases.Title(language.English).String(matches[1])
|
||||
@@ -152,10 +151,6 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, titleFromFi
|
||||
meta.Type = "page"
|
||||
}
|
||||
|
||||
if meta.ContentAppearance == "" {
|
||||
meta.ContentAppearance = FullWidthContentAppearance // Default to full-width for backwards compatibility
|
||||
}
|
||||
|
||||
if titleFromH1 && meta.Title == "" {
|
||||
meta.Title = ExtractDocumentLeadingH1(data)
|
||||
}
|
||||
@@ -167,6 +162,17 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, titleFromFi
|
||||
}
|
||||
}
|
||||
|
||||
// Use the global content appearance flag if the header is not set in the document
|
||||
if meta != nil && defaultContentAppearance != "" && meta.ContentAppearance == "" {
|
||||
if strings.TrimSpace(defaultContentAppearance) == FixedContentAppearance {
|
||||
meta.ContentAppearance = FixedContentAppearance
|
||||
} else {
|
||||
meta.ContentAppearance = FullWidthContentAppearance
|
||||
}
|
||||
} else if meta != nil && meta.ContentAppearance == "" {
|
||||
meta.ContentAppearance = FullWidthContentAppearance // Default to full-width if nothing else is set for backwards compatibility
|
||||
}
|
||||
|
||||
if meta == nil {
|
||||
return nil, data, nil
|
||||
}
|
||||
|
||||
@@ -60,3 +60,32 @@ func TestSetTitleFromFilename(t *testing.T) {
|
||||
assert.Equal(t, "Already Title Cased", meta.Title)
|
||||
})
|
||||
}
|
||||
|
||||
func TestExtractMetaContentAppearance(t *testing.T) {
|
||||
t.Run("default fills missing content appearance", func(t *testing.T) {
|
||||
data := []byte("<!-- Space: DOC -->\n<!-- Title: Example -->\n\nbody\n")
|
||||
|
||||
meta, _, err := ExtractMeta(data, "", false, false, "", nil, false, FixedContentAppearance)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, meta)
|
||||
assert.Equal(t, FixedContentAppearance, meta.ContentAppearance)
|
||||
})
|
||||
|
||||
t.Run("header takes precedence over default", func(t *testing.T) {
|
||||
data := []byte("<!-- Space: DOC -->\n<!-- Title: Example -->\n<!-- Content-Appearance: full-width -->\n\nbody\n")
|
||||
|
||||
meta, _, err := ExtractMeta(data, "", false, false, "", nil, false, FixedContentAppearance)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, meta)
|
||||
assert.Equal(t, FullWidthContentAppearance, meta.ContentAppearance)
|
||||
})
|
||||
|
||||
t.Run("falls back to full-width when default isn't set", func(t *testing.T) {
|
||||
data := []byte("<!-- Space: DOC -->\n<!-- Title: Example -->\n\nbody\n")
|
||||
|
||||
meta, _, err := ExtractMeta(data, "", false, false, "", nil, false, "")
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, meta)
|
||||
assert.Equal(t, FullWidthContentAppearance, meta.ContentAppearance)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user