yogthos / markdown-clj

Markdown parser in Clojure
Eclipse Public License 1.0
538 stars 120 forks source link

Adding rel=nofollow to all links #201

Open jwr opened 3 months ago

jwr commented 3 months ago

Short of patching markdown-clj, is there a way to add rel="nofollow" to all links?

This is pretty much a requirement for any user-supplied text that will be displayed on a public webpage, otherwise the SEO spamming crowd will eat you alive.

I've been looking at transformers, but it doesn't seem like they are created for that use case.

yogthos commented 3 months ago

You could potentially wrap the built in href transformer with your own that will inject rel="nofollow". I would definitely be open to a pr to facilitate this though. This would be the namespace to do it https://github.com/yogthos/markdown-clj/blob/master/src/cljc/markdown/links.cljc

jwr commented 3 months ago

While I do understand the intention of getting more people to contribute with PRs, I am not sure if this is the best way to proceed. This is something that requires design and my ideas might be very far from acceptable to you.

I would probably add global options that would be passed alongside state to all transformers, where each transformer could get at its options and use them. The href transformer would use :attributes, where one could specify :rel "nofollow,ugc", for example. This is likely not the best, or even a good way to implement this kind of functionality…

yogthos commented 3 months ago

There's actually some precedent for doing this already with :code-style option, so we could add more options :attributes would be ambiguous though, since you might want to set attributes for different elements. Perhaps, better approach might be to key on the transformer or tag type. For example, maybe could add :href {:attributes {:rel "nofollow,ugc"}}, or even just :href {:rel "nofollow,ugc"}.

I do think that the case could be addressed by making a custom transformer vector with the way things are currently set up. You would need a custom version of this vector where you replace the transformer for the tags with your custom version

https://github.com/yogthos/markdown-clj/blob/master/src/cljc/markdown/transformers.cljc#L423

jwr commented 3 months ago

Thank you. I don't think I'm capable of designing this properly, so I've worked around the issue by copying large swaths of code from markdown.links (href, make-link and link), so that I could replace href with href-nofollow that adds the rel=nofollow attribute.

yogthos commented 3 months ago

No worries, glad to hear the immediate issue is solved. I'll leave this open as an enhancement, if I get a bit of time I'll look at implementing the feature.

coreagile commented 2 months ago

Just to chime in, I'd also like to modify links. Like, for example, adding target=_blank -- probably will fit in to your existing design?

yogthos commented 2 months ago

It sounds like the best approach here might be to allow passing around a map of options, such as attributes, that can be applied to each type of element. These could then be applied by the transformers. If anybody wants to try to adding this in, I can definitely help guide a pr, but I probably won't have time to look myself in the near future.