Skip to content

Commit

Permalink
fix: newlines in bold/italic
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Jan 23, 2022
1 parent dab5f48 commit cad6dec
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
16 changes: 12 additions & 4 deletions commonmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,13 @@ var commonmark = []Rule{
if trimmed == "" {
return &trimmed
}
trimmed = opt.StrongDelimiter + trimmed + opt.StrongDelimiter

// always have a space to the side to recognize the delimiter
// If there is a newline character between the start and end delimiter
// the delimiters won't be recognized. Either we remove all newline characters
// OR on _every_ line we put start & end delimiters.
trimmed = delimiterForEveryLine(trimmed, opt.StrongDelimiter)

// Always have a space to the side to recognize the delimiter
trimmed = AddSpaceIfNessesary(selec, trimmed)

return &trimmed
Expand All @@ -193,9 +197,13 @@ var commonmark = []Rule{
if trimmed == "" {
return &trimmed
}
trimmed = opt.EmDelimiter + trimmed + opt.EmDelimiter

// always have a space to the side to recognize the delimiter
// If there is a newline character between the start and end delimiter
// the delimiters won't be recognized. Either we remove all newline characters
// OR on _every_ line we put start & end delimiters.
trimmed = delimiterForEveryLine(trimmed, opt.EmDelimiter)

// Always have a space to the side to recognize the delimiter
trimmed = AddSpaceIfNessesary(selec, trimmed)

return &trimmed
Expand Down
6 changes: 6 additions & 0 deletions testdata/TestCommonmark/italic/goldmark.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
<p>Some <em>19,80€</em> Text</p>
<p><em>Content</em> and no space afterward.</p>
<p>_Not Italic_</p>
<p><em>First</em></p>
<p><em>Second</em></p>
<p><em>Text und Fotos: Max Mustermann</em></p>
<p><em>Original veröffentlicht am 01.01.2021 auf</em> <a href="http://example.com/posts/100979">Zeitung</a></p>
<p><em>Übersetzung: Max Mustermann</em></p>
<p><em>veröffentlicht am 02.02.2021</em></p>
12 changes: 11 additions & 1 deletion testdata/TestCommonmark/italic/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@


<!--with markdown characters (escaping)-->
<p>_Not Italic_</p>
<p>_Not Italic_</p>


<!--with br inside it-->
<p><em>First <br /> Second</em></p>

<p>
<em>Text und Fotos: Max Mustermann<br>Original veröffentlicht am 01.01.2021 auf </em>
<a href="/posts/100979">Zeitung</a><br>
<em>Übersetzung: Max Mustermann<br>veröffentlicht am 02.02.2021</em>
</p>
14 changes: 13 additions & 1 deletion testdata/TestCommonmark/italic/output.asterisks.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ Some *19,80€* Text

*Content* and no space afterward.

\_Not Italic\_
\_Not Italic\_

*First*

*Second*

*Text und Fotos: Max Mustermann*

*Original veröffentlicht am 01.01.2021 auf* [Zeitung](http://example.com/posts/100979)

*Übersetzung: Max Mustermann*

*veröffentlicht am 02.02.2021*
14 changes: 13 additions & 1 deletion testdata/TestCommonmark/italic/output.underscores.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ Some _19,80€_ Text

_Content_ and no space afterward.

\_Not Italic\_
\_Not Italic\_

_First_

_Second_

_Text und Fotos: Max Mustermann_

_Original veröffentlicht am 01.01.2021 auf_ [Zeitung](http://example.com/posts/100979)

_Übersetzung: Max Mustermann_

_veröffentlicht am 02.02.2021_
19 changes: 19 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,25 @@ func getCodeContent(selec *goquery.Selection) string {
return content
}

// delimiterForEveryLine puts the delimiter not just at the start and end of the string
// but if the text is divided on multiple lines, puts the delimiters on every line with content.
//
// Otherwise the bold/italic delimiters won't be recognized if it contains new line characters.
func delimiterForEveryLine(text string, delimiter string) string {
lines := strings.Split(text, "\n")

for i, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
// Skip empty lines
continue
}

lines[i] = delimiter + line + delimiter
}
return strings.Join(lines, "\n")
}

// isWrapperListItem returns wether the list item has own
// content or is just a wrapper for another list.
// e.g. "<li><ul>..."
Expand Down

0 comments on commit cad6dec

Please sign in to comment.