Giter Site home page Giter Site logo

Comments (19)

sibiraj-s avatar sibiraj-s commented on June 18, 2024 1

I did not originally plan to add option to implement sanitization or provide some hooks to pre/post process stuffs, but I am open to suggestions for the implementation.

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024 1

That is in fact what I am doing right now. It looks fine with the content I have tested.

It's just that the processing takes place in two separate places, you know. Once I am sanitizing else where, and then later on in the return I push the data in the Marked.

It works, but they happen separately. So yes, I was just nitpicking but it would have been sweet if all of those could happen altogether. If you get what I mean.

That is why I wrote, it's not urgent, but it definitely will make the package better overall.

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024 1

Sure, that would be good. I do not have a lot of experience with developing packages but I will try to think of something if we can somehow implement hooks (and even tokenizer but I just don't understand tokenizer myself haha)

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

I have never been in a situation to sanitize markdown files. But If you ask me, I would heavily recommend to sanitize the content before storing in DB, processing it in client side is not recommended, as it can be bypassed on way or other.

And to your question. your code looks fine, and should work fine. Can you please verify the output of the sanitize function once (though it shouldn't be an issue).

And please provide a reproducible example in stackblitz/codesandbox maybe. Thanks

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024

Well, there is sanitization happening on the server side as well. But the best practice is usually to sanitize them on both ends (I can just no implement it and tell my overlords that it been done thoughπŸ˜‚ but I digress).

Anyway, as far as the code shown above goes - it works fine for the HTML, but it doesn't work for some other type of input like bold, italics and so on. I am going to post a sandbox soon. Let me see if it can be reproduced.

Thanks for the prompt reply and hard work!

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

Ah, I get it now. I'll see why you are referring to now. I didn't understand fully earlier.

You must return a valid react node. See here . I'll also look into this once.

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024

Yea, that's the problem. Sanitizing the argument passed into renderer messes up the React Node, and hence a weird output is received. That was my diagnosis. For now, I am essentially running the sanitization in useEffect where I am getting my data from API call.

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

I have just tried it and it works fine as expect πŸ€” . Make sure the implementation is for the renderer.

This is the code i used

strong(text){
  const clean = DOMPurify.sanitize(text as string)
  return <CustomComponent>{clean}</CustomComponent>
}

You can refer to some examples here from storybook stories

To debug further. a reproducible stackblitz will help.

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024

Ok, I have got the Sandbox. Maybe I am doing something wrong.

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

I think I see whats happening whats happening. So the elements like bold, italics are parsed first and converted to react element before passing to paragraph.

This react elements are just objects, which when passed to purifier, is converted to object object.

I tried differently before, hence I couldn't see this.

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024

Yep, I figured as much. So even if I give the renderer a separate method for processing bold and italics, it still passes them into paragraph and the result is basically the same.

If I have inline prop as true, everything is sent into text method. And even in that case, the bold and italics are processed first and then sent into text method.

Anyways, I am in no hurry, you can have a little fun figuring this out in the weekend or the nextπŸ˜„

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024

I have seen the native marked package handle Sanitize at some point in the rendering cycle. Maybe that will give you the clue. I was thinking of switching to the native package but then I thought that will be hassle, let's try hitting you up instead, maybe you will figure it out.

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

Screenshot 2023-04-26 at 11 25 59 AM

marked doesn't sanitize. Ref: https://marked.js.org/#usage

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024

Yea, marked doesn't sanitize. But it has a way to handle sanitization at some point in the rendering cycle.
Ref: https://marked.js.org/using_pro#hooks

Image showing the second example in hooks section

See the second example of them using postprocess hook.

Also, since marked just outputs the basic HTML, it can be directly sent through sanitization.
But with React, that can't be the case. So having access to the hook would be good.

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

Oh didn't know that. Thanks.

Yeah it is simple in their case as it is just a simple string of html. Where are the parser here converts to React components. I don't see an option here, even a window to do something.

from marked-react.

tanmayaBiswalOdiware avatar tanmayaBiswalOdiware commented on June 18, 2024

I wonder if React elements can be sanitized. That didn't pop into my brain until now haha.

But from your point of view, there is no way to introduce hooks into this package?

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

Why not just pass the content through dom-purify first then send it to marked-react. I believe dom-purify won't remove anything that is related to markdown formatting. πŸ€”

did you try that and created any problems?

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

Screenshot 2023-04-26 at 2 07 31 PM

from marked-react.

sibiraj-s avatar sibiraj-s commented on June 18, 2024

Cool. In that case, I'll leave this open for sometime, may be some idea might popup overtime or someone could suggest a better solution to make this seamless.

from marked-react.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.