I want to display when text <=2 lines, then display 2 lines without “…”.
However, if the text is > 2 lines, then add “…” in the middle.
In my code, i can add “…” in the middle, but when the text is less than or equal to 2 lines it still displays “…”. I want is that when text <= 2 lines it still displays full text, and if text > 2 then displays “…” in the middle.
this is my code:
const Main=()=>{
return(
<div>
<p>{truncateMiddle(textDummy)}</p>
</div>
)
const truncateMiddle=(text)=>{
const offset = 50;
const ellipsis = '...'
const len = text.length;
const tail = text.slice(len - offset, len);
let head = ""
let end = len - offset;
let start = 50;
while (start < end - 1) {
const curr = Math.floor((end - start) / 2 + start);
head = text.slice(0, curr);
end = curr;
}
head = text.slice(0, start || 1);
return head + ellipsis + tail;
}
thanks for help