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

Don't put the ellipsis in a <details> tag #41

Open
nex3 opened this issue Oct 2, 2024 · 6 comments
Open

Don't put the ellipsis in a <details> tag #41

nex3 opened this issue Oct 2, 2024 · 6 comments

Comments

@nex3
Copy link

nex3 commented Oct 2, 2024

If the last text in the truncated HTML is a <details> tag, the ellipsis should probably go after it, since it'll be collapsed by default in the tag itself.

@oe
Copy link
Owner

oe commented Oct 2, 2024

The condition may vary depending on whether the <details> has the open attribute and whether the <details> is the last element.

If the <details> element does not have an open attribute, the additional details may not be counted visually. If it has an open attribute, the opposite is true.

If the <details> element is not the last element, but the last element has a text content, what should be done?

I don't think it's a good idea to deal with this condition in this lib.

@nex3
Copy link
Author

nex3 commented Oct 2, 2024

To my mind, the value of the lib is specifically to at least try to know about and handle all the weird cases so users don't have to. Is there even a way for a user to handle this outside the lib without essentially re-implementing the whole thing?

To my mind, <details> (or maybe <details> without open) should be treated the same as an element that can't contain text, like <img>.

@oe
Copy link
Owner

oe commented Oct 10, 2024

In my opinion, the <summary> inside <details> should be counted, and the visual effect matters. That means a tag should be treated as if it has no text content if it's invisible when rendered.

There should be a way to customize when to ignore a tag.

Please allow me some time to consider this matter thoroughly.

oe pushed a commit that referenced this issue Oct 12, 2024
… truncate strategy #41

2. Rewrite the library to a purely functional library to prevent configurations from getting polluted.
3. Update the build toolchain to Vite and Vitetest.
@oe
Copy link
Owner

oe commented Oct 12, 2024

A new option customNodeStrategy has been added. Check readme for details.

You can run yarn add truncate-html@beta (v1.2.0-beta.1) to install the beta version and have a try.

For your requirements, the following code may achieve your needs:

import truncate, { type IOptions, type ICustomNodeStrategy } from 'truncate-html'

// argument node is a cheerio instance
const customNodeStrategy: ICustomNodeStrategy = node => {
  // keep details and treat it as nothing inside
  if (node.is('details')) {
    return 'keep'
  }
}

const html = '<div><details><summary>Click me</summary><p>Some details</p></details>other things</div>'

const options: IOptions = {
  length: 3,
  customNodeStrategy
}

truncate(html, options)
// => <div><details><summary>Click me</summary><p>Some details</p></details>oth...</div>

@oe
Copy link
Owner

oe commented Nov 3, 2024

@nex3 Hi, is there any problem with this beta version? I'd like to make an official release in a few days.

@nex3
Copy link
Author

nex3 commented Nov 4, 2024

Sorry, I haven't had a chance to try it yet. I'll see if I can give it a shot tonight.

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

No branches or pull requests

2 participants