Improve removing unnecessary whitespace in HTML
This commit is contained in:
parent
899bdbc705
commit
18aeb8ba9d
@ -44,7 +44,7 @@ type htmlParser struct {
|
|||||||
prefs *config.UserPreferences
|
prefs *config.UserPreferences
|
||||||
room *rooms.Room
|
room *rooms.Room
|
||||||
|
|
||||||
keepLinebreak bool
|
preserveWhitespace bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func AdjustStyleBold(style tcell.Style) tcell.Style {
|
func AdjustStyleBold(style tcell.Style) tcell.Style {
|
||||||
@ -330,11 +330,11 @@ func (parser *htmlParser) codeblockToEntity(node *html.Node) Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parser.keepLinebreak = true
|
parser.preserveWhitespace = true
|
||||||
text := (&ContainerEntity{
|
text := (&ContainerEntity{
|
||||||
Children: parser.nodeToEntities(node.FirstChild),
|
Children: parser.nodeToEntities(node.FirstChild),
|
||||||
}).PlainText()
|
}).PlainText()
|
||||||
parser.keepLinebreak = false
|
parser.preserveWhitespace = false
|
||||||
return parser.syntaxHighlight(text, lang)
|
return parser.syntaxHighlight(text, lang)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,11 +371,13 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var spaces = regexp.MustCompile("\\s+")
|
||||||
|
|
||||||
func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity {
|
func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity {
|
||||||
switch node.Type {
|
switch node.Type {
|
||||||
case html.TextNode:
|
case html.TextNode:
|
||||||
if !parser.keepLinebreak {
|
if !parser.preserveWhitespace {
|
||||||
node.Data = strings.Replace(node.Data, "\n", "", -1)
|
node.Data = spaces.ReplaceAllLiteralString(node.Data, " ")
|
||||||
}
|
}
|
||||||
if len(node.Data) == 0 {
|
if len(node.Data) == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user