You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using sanitize-html to transform links into a plaintext format that still conveys the necessary information.
Something like turning <a href="https://example.com">Example Link</a> into Example Link (https://example.com).
transformTag isn't sufficient because it gets executed during the onopentag handler, at which point the (textual) content of the tag hasn't been processed yet. With discard as the disallowedTagsMode you lose the link in href, and the only modification transformTag could make is overwriting the text element that gets processed in ontext with the link, but then you lose the original link text.
Proposed solution
A function that gets the same arguments as exclusiveFilter, so the frame with attributes, text, and expects to return an object that edits the element's contents.
For my specific usecase it's enough to overwrite the whole <a> element with a plain-text string, which would be easier to implement than a function that allows overwriting the tag name, attributes etc after they've already been written to the result string.
Using exclusiveFilter to store a link's href and text content in an external object, combined with the frame's tagPosition. Then return true to discard the anchor tag entirely, and splice in the new text content at the stored index after sanitizing has finished.
The text was updated successfully, but these errors were encountered:
oh I have used it in the past, could work for this as well I think. A feature like this did seem in line with the basic transformTag/exclusiveFilter functions, but I also understand not wanting to add more functions like that for pretty specific usecases.
On December 10, 2024 8:18:10 PM GMT+01:00, Tom Boutell ***@***.***> wrote:
This does look useful. This might be a better use case for `cheerio` though, have you checked out that module?
--
Reply to this email directly or view it on GitHub:
#685 (comment)
You are receiving this because you authored the thread.
Message ID: ***@***.***>
The problem to solve
I'm using sanitize-html to transform links into a plaintext format that still conveys the necessary information.
Something like turning
<a href="https://example.com">Example Link</a>
intoExample Link (https://example.com)
.transformTag
isn't sufficient because it gets executed during theonopentag
handler, at which point the (textual) content of the tag hasn't been processed yet. Withdiscard
as thedisallowedTagsMode
you lose the link inhref
, and the only modificationtransformTag
could make is overwriting the text element that gets processed inontext
with the link, but then you lose the original link text.Proposed solution
A function that gets the same arguments as
exclusiveFilter
, so theframe
with attributes, text, and expects to return an object that edits the element's contents.For my specific usecase it's enough to overwrite the whole
<a>
element with a plain-text string, which would be easier to implement than a function that allows overwriting the tag name, attributes etc after they've already been written to the result string.Alternatives
Using
exclusiveFilter
to store a link's href and text content in an external object, combined with the frame'stagPosition
. Then return true to discard the anchor tag entirely, and splice in the new text content at the stored index after sanitizing has finished.The text was updated successfully, but these errors were encountered: