Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option strip empty tag when using with maxLines #23

Open
ng-ha opened this issue Oct 21, 2023 · 2 comments
Open

option strip empty tag when using with maxLines #23

ng-ha opened this issue Oct 21, 2023 · 2 comments

Comments

@ng-ha
Copy link

ng-ha commented Oct 21, 2023

Hey, thanks for greet package, @arendjr !

It would be helpful if text-clipper has option to strip empty tag.

For example:

clip( '<p><img/></p><p>abc</p>',  3,  {
    html: true,
    stripTags: ['img'],
    maxLines: 1,
  })

returns

'<p></p>'

which isn't what i wanted.

I want this:

'<p>abc</p>'

Can you provide me a workaround, please! Thank you!

@ng-ha
Copy link
Author

ng-ha commented Oct 21, 2023

i found a workaround:

  • first, remove tags: const strippedHtml = clip(html, Infinity, {html: true, stripTags: ['img', 'br']});
  • then remove empty tags: const sanitizedHtml = strippedHtml.replace(/<[^>]>[\s\r\n]</[^>]*>/g, '')
  • finally, clip it: clip(sanitizedHtml, 40, {html: true, maxLines: 1});

@arendjr
Copy link
Owner

arendjr commented Oct 23, 2023

Thanks for the feedback!

I think your suggestion makes a lot of sense. In fact, I think a variation of this makes sense for the plain-text clipper too:

clip( '\nabc',  3,  {
    maxLines: 1,
})

I didn’t actually check what it returns, but I’m afraid you’ll only get ellipsis, while ”abc” would be a more desirable output in many cases.

I’m thinking it might make sense to introduce an omitEmptyLines option which in the HTML would omit empty paragraphs too, including when such a paragraph is created by stripping other tags.

I’d be happy to accept a PR for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants