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

text() method merges text parts #3457

Open
kvetoslavnovak opened this issue Oct 25, 2023 · 3 comments
Open

text() method merges text parts #3457

kvetoslavnovak opened this issue Oct 25, 2023 · 3 comments

Comments

@kvetoslavnovak
Copy link

kvetoslavnovak commented Oct 25, 2023

text() method in JQuery keeps whitespaces between text parts. Cheerio unfortunatelly merges text parts from tags thogether givign you quite gibberish words.

See exampel https://api.jquery.com/text/ I tested it by myself and JQuery text() method keeps whitespaces between the text parts of the tags. This is very useful.

Cheerio on the other hand messes text parts alltogther without no spaces which is a pain.

E.g. from this

TITLE>
<TI>
<P>PART ONE</P>
</TI>
<STI>
<P>
<HT TYPE="BOLD">GENERAL PROVISIONS</HT>
</P>
</STI>
</TITLE>

JQuery text() gives you expected text PART ONE GENERAL PROVISIONS but Cheerio gives you PART ONEGENERAL PROVISIONS

Also standard HTML innerText property works the same.

@kvetoslavnovak
Copy link
Author

kvetoslavnovak commented Oct 25, 2023

OK. This seemt to do the trick:

cheerio(elem).html().replaceAll(/<\/?[a-zA-Z0-9=" ]*>/g, ' ').replace(/\s\s+/g, ' ').trim())

But better would if cheerio keep the whitespace which can ve trime manually as user needs to.

@apsquared
Copy link

Facing the same issue and hitting it via langchain CheerioWebLoad so I cant use the approach above.

@Properko
Copy link

I believe this is related #2841 and really unfortunate 😬
That regex can get heavy for huge htmls

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

3 participants