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

How to "replaceWith" on plain text, which is not a DOM Elements? #3411

Open
opendeluxe opened this issue Sep 30, 2023 · 5 comments
Open

How to "replaceWith" on plain text, which is not a DOM Elements? #3411

opendeluxe opened this issue Sep 30, 2023 · 5 comments

Comments

@opendeluxe
Copy link

I have the following DIV that needs some replaces:

const html = `Lorem Ipsum dolores`;
const $ = cheerio.load(html, null, false);

This loads the string without wrapping the text into <html><body>, which must be avoided in my case.

I want to do a replacement on "Ipsum" to replace it with a DOM element, like an <a>-Element.
After that, I want to replace other words as well with related DOM-elements, but never ever touch already replaced words again.

Which filter gives me this feature to work with replaceWith?

@SagarKapoorin
Copy link

hi there,
I'm relatively new to this project, so please correct me if I'm mistaken.
So the issue you facing is:-
You want to replace specific words in an HTML string with DOM elements (e.g., replacing "Ipsum" with an element), but you also want to ensure that once a word has been replaced, it won't be replaced again

@SagarKapoorin
Copy link

Can you tell me in which file this issue occurs.

@opendeluxe
Copy link
Author

opendeluxe commented Oct 1, 2023

Yes, lets say I get an article from a CMS, it can contain HTML like a-links or divs or span-elements or nothing at all.
I want to replace specific words or word groups to with specific <a>-elements to deep link into other articles. No matter, if this word is part of a span-element or a div or a p-element or plain text which is not at all encapsulated within an HTML tag. But i this word is part of an a-element, it should not be replaced again as it is already a link.

@SagarKapoorin
Copy link

Ok i got it ,I am Looking for existing file where this existing code is written as i am unable to find code where i have to make changes

@luv2027
Copy link

luv2027 commented Oct 5, 2023

I have the following DIV that needs some replaces:

const html = `Lorem Ipsum dolores`;
const $ = cheerio.load(html, null, false);

This loads the string without wrapping the text into <html><body>, which must be avoided in my case.

I want to do a replacement on "Ipsum" to replace it with a DOM element, like an <a>-Element. After that, I want to replace other words as well with related DOM-elements, but never ever touch already replaced words again.

Which filter gives me this feature to work with replaceWith?

Can you tell me in which file the changes needs to be made ?

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