Do not combine certain elements in Split or SplitBy
I think Split
might be easier to use here:
Split[
list,
First[#1] == First[#2] && Last[#1] == Last[#2] && First[#1] != "TextElement" &
]
(* Out:
{
{{"Text", "hello", "a"}, {"Text", " world", "a"}},
{{"TextElement", "line", "a"}},
{{"TextElement", "line", "a"}},
{{"Text", "!", "b"}},
{{"Text", "ok", "c"}}
}
*)
Other formulations:
Split[list, #[[{1, -1}]] == #2[[{1, -1}]] && #[[1]] != "TextElement" &]
SplitBy[list, {#[[1]] /. "TextElement" :> Unique[], #[[-1]]} &]
Related: How to Gather a list with some elements considered unique