-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from JohannesKaufmann/improve-hard-line-break-2
improve-hard-line-break-2
- Loading branch information
Showing
17 changed files
with
208 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,56 @@ | ||
package textutils | ||
|
||
// EscapeMultiLine deals with multiline content inside a link or a heading. | ||
func EscapeMultiLine(content []byte) []byte { | ||
content = TrimConsecutiveNewlines(content) | ||
import ( | ||
"bytes" | ||
"unicode" | ||
) | ||
|
||
var ( | ||
doubleSpace = []byte{' ', ' '} | ||
|
||
newContent := make([]byte, 0, len(content)) | ||
newlineBreak = []byte{'\n'} | ||
hardLineBreak = []byte{' ', ' ', '\n'} | ||
escapedNoContentLineBreak = []byte{'\\', '\n'} | ||
) | ||
|
||
startNormal := 0 | ||
lineHasContent := false | ||
for index, char := range content { | ||
isNewline := char == '\n' | ||
isSpace := char == ' ' || char == ' ' | ||
// EscapeMultiLine deals with multiline content inside a link or a heading. | ||
func EscapeMultiLine(content []byte) []byte { | ||
parts := bytes.Split(content, newlineBreak) | ||
if len(parts) == 1 { | ||
return content | ||
} | ||
|
||
isFirstNewline := isNewline && lineHasContent | ||
isLastNewline := isNewline && !lineHasContent | ||
output := make([]byte, 0, len(content)) | ||
for i := range parts { | ||
trimmedLeft := bytes.TrimLeftFunc(parts[i], unicode.IsSpace) | ||
|
||
if isFirstNewline { | ||
newContent = append(newContent, content[startNormal:index]...) | ||
newContent = append(newContent, '\n') | ||
if len(trimmedLeft) == 0 { | ||
// A blank line would interrupt the link. | ||
// So we need to escape the line | ||
output = append(output, escapedNoContentLineBreak...) | ||
continue | ||
} | ||
|
||
startNormal = index + 1 | ||
lineHasContent = false | ||
isLast := i == len(parts)-1 | ||
if isLast { | ||
// For the last line we don't need to add any "\n" anymore | ||
output = append(output, trimmedLeft...) | ||
continue | ||
} | ||
|
||
// Now decide what ending we want: | ||
if bytes.HasSuffix(trimmedLeft, doubleSpace) { | ||
// We already have " " so adding a "\n" is enough | ||
output = append(output, trimmedLeft...) | ||
output = append(output, newlineBreak...) | ||
continue | ||
} else { | ||
// We *prefer* having a hard-line-break " \n" | ||
output = append(output, trimmedLeft...) | ||
output = append(output, hardLineBreak...) | ||
continue | ||
} else if isLastNewline { | ||
newContent = append(newContent, '\\') | ||
newContent = append(newContent, '\n') | ||
|
||
startNormal = index + 1 | ||
lineHasContent = false | ||
} else if !isSpace { | ||
lineHasContent = true | ||
} else if isSpace && !lineHasContent { | ||
startNormal = index + 1 | ||
} | ||
} | ||
|
||
newContent = append(newContent, content[startNormal:]...) | ||
|
||
return newContent | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.