mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-21 09:11:05 +00:00
Fix some corner cases in revised summary handling
And clean up the test. See #2309
This commit is contained in:
parent
d18a8cbe15
commit
0a7d1d0ddc
2 changed files with 17 additions and 32 deletions
|
@ -246,6 +246,7 @@ type summaryContent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitUserDefinedSummaryAndContent(markup string, c []byte) *summaryContent {
|
func splitUserDefinedSummaryAndContent(markup string, c []byte) *summaryContent {
|
||||||
|
c = bytes.TrimSpace(c)
|
||||||
startDivider := bytes.Index(c, internalSummaryDivider)
|
startDivider := bytes.Index(c, internalSummaryDivider)
|
||||||
|
|
||||||
if startDivider == -1 {
|
if startDivider == -1 {
|
||||||
|
@ -276,20 +277,27 @@ func splitUserDefinedSummaryAndContent(markup string, c []byte) *summaryContent
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the closest end/start markup string to the divider
|
// Find the closest end/start markup string to the divider
|
||||||
//firstStart := bytes.Index(c[:startDivider], startMarkup)
|
|
||||||
fromIdx := bytes.LastIndex(c[:startDivider], startMarkup)
|
fromIdx := bytes.LastIndex(c[:startDivider], startMarkup)
|
||||||
fromStart := startDivider - fromIdx - len(startMarkup)
|
fromStart := startDivider - fromIdx - len(startMarkup)
|
||||||
fromEnd := bytes.Index(c[endDivider:], endMarkup)
|
fromEnd := bytes.Index(c[endDivider:], endMarkup)
|
||||||
|
|
||||||
if fromEnd != -1 && fromEnd <= fromStart {
|
if fromEnd != -1 && fromEnd <= fromStart {
|
||||||
endSummary = startDivider + fromEnd + len(endMarkup)
|
endSummary = startDivider + fromEnd + len(endMarkup)
|
||||||
} else if fromStart != -1 {
|
} else if fromStart != -1 && fromEnd != -1 {
|
||||||
endSummary = startDivider - fromStart - len(startMarkup)
|
endSummary = startDivider - fromStart - len(startMarkup)
|
||||||
}
|
}
|
||||||
|
|
||||||
withoutDivider := bytes.TrimSpace(append(c[:startDivider], c[endDivider:]...))
|
withoutDivider := bytes.TrimSpace(append(c[:startDivider], c[endDivider:]...))
|
||||||
contentWithoutSummary := bytes.TrimSpace(withoutDivider[endSummary:])
|
|
||||||
summary := bytes.TrimSpace(withoutDivider[:endSummary])
|
var (
|
||||||
|
contentWithoutSummary []byte
|
||||||
|
summary []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(withoutDivider) > 0 {
|
||||||
|
contentWithoutSummary = bytes.TrimSpace(withoutDivider[endSummary:])
|
||||||
|
summary = bytes.TrimSpace(withoutDivider[:endSummary])
|
||||||
|
}
|
||||||
|
|
||||||
if addDiv {
|
if addDiv {
|
||||||
// For the rst
|
// For the rst
|
||||||
|
|
|
@ -655,6 +655,11 @@ func TestSplitSummaryAndContent(t *testing.T) {
|
||||||
{"markdown", "<p>a</p><p>b</p><p>cHUGOMORE42</p>", "<p>a</p><p>b</p><p>c</p>", "<p>a</p><p>b</p><p>c</p>", ""},
|
{"markdown", "<p>a</p><p>b</p><p>cHUGOMORE42</p>", "<p>a</p><p>b</p><p>c</p>", "<p>a</p><p>b</p><p>c</p>", ""},
|
||||||
{"markdown", "<p>a</p><p>bHUGOMORE42</p><p>c</p>", "<p>a</p><p>b</p>", "<p>a</p><p>b</p><p>c</p>", "<p>c</p>"},
|
{"markdown", "<p>a</p><p>bHUGOMORE42</p><p>c</p>", "<p>a</p><p>b</p>", "<p>a</p><p>b</p><p>c</p>", "<p>c</p>"},
|
||||||
{"markdown", "<p>aHUGOMORE42</p><p>b</p><p>c</p>", "<p>a</p>", "<p>a</p><p>b</p><p>c</p>", "<p>b</p><p>c</p>"},
|
{"markdown", "<p>aHUGOMORE42</p><p>b</p><p>c</p>", "<p>a</p>", "<p>a</p><p>b</p><p>c</p>", "<p>b</p><p>c</p>"},
|
||||||
|
{"markdown", " HUGOMORE42 ", "", "", ""},
|
||||||
|
{"markdown", "HUGOMORE42", "", "", ""},
|
||||||
|
{"markdown", "<p>HUGOMORE42", "<p>", "<p>", ""},
|
||||||
|
{"markdown", "HUGOMORE42<p>", "", "<p>", "<p>"},
|
||||||
|
{"markdown", "\n\n<p>HUGOMORE42</p>\n", "<p></p>", "<p></p>", ""},
|
||||||
} {
|
} {
|
||||||
|
|
||||||
sc := splitUserDefinedSummaryAndContent(this.markup, []byte(this.content))
|
sc := splitUserDefinedSummaryAndContent(this.markup, []byte(this.content))
|
||||||
|
@ -664,34 +669,6 @@ func TestSplitSummaryAndContent(t *testing.T) {
|
||||||
require.Equal(t, this.expectedContent, string(sc.content), fmt.Sprintf("[%d] Content markup %s", i, this.markup))
|
require.Equal(t, this.expectedContent, string(sc.content), fmt.Sprintf("[%d] Content markup %s", i, this.markup))
|
||||||
require.Equal(t, this.expectedContentWithoutSummary, string(sc.contentWithoutSummary), fmt.Sprintf("[%d] Content without summary, markup %s", i, this.markup))
|
require.Equal(t, this.expectedContentWithoutSummary, string(sc.contentWithoutSummary), fmt.Sprintf("[%d] Content without summary, markup %s", i, this.markup))
|
||||||
}
|
}
|
||||||
|
|
||||||
if true {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ad := `<div class="paragraph"><p>sn</p></div>
|
|
||||||
<div class="paragraph">
|
|
||||||
<p>HUGOMORE42
|
|
||||||
Some more text</p>
|
|
||||||
</div>
|
|
||||||
`
|
|
||||||
|
|
||||||
md := `<p>Summary Same LineHUGOMORE42</p>
|
|
||||||
|
|
||||||
<p>Some more text</p>`
|
|
||||||
|
|
||||||
sc := splitUserDefinedSummaryAndContent("markdown", []byte(md))
|
|
||||||
|
|
||||||
require.Equal(t, "adf", string(sc.summary))
|
|
||||||
require.Equal(t, "asdf", string(sc.content))
|
|
||||||
|
|
||||||
if true {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sc = splitUserDefinedSummaryAndContent("asciidoc", []byte(ad))
|
|
||||||
require.Equal(t, "<div class=\"paragraph\"><p>sn</p></div>", string(sc.summary))
|
|
||||||
require.Equal(t, "\n<div class=\"paragraph\">\n<p> \nSome more text</p>\n</div>\n", string(sc.summary))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPageWithDelimiter(t *testing.T) {
|
func TestPageWithDelimiter(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue