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 add attribute (rel="nofollow") to anchor <a> for html nodes? #277

Closed
Elmervc opened this issue Nov 15, 2018 · 1 comment
Closed

Comments

@Elmervc
Copy link

Elmervc commented Nov 15, 2018

I'd like to add the rel="nofollow" attribute to (most of the) rendered links from markdown texts.
I came around #103 and created an extension that handles Link and AutoLink nodes. This works great.
Now, I'd also like to add the rel attribute to anchor elements that are entered as HTML in the markdown input, but I cannot find out which node I need to match or how to match this.
Do you have any pointers? Thank you :)

@Override
public void setAttributes(final Node node, final AttributablePart part, final Attributes attributes) {
  if (   (node instanceof Link || node instanceof AutoLink /*|| node instanceof ??SomeHTMLNode?? */)
      && (part == AttributablePart.LINK) ) {

    attributes.replaceValue("rel", "nofollow");

  }
}
@vsch
Copy link
Owner

vsch commented Nov 15, 2018

@Elmervc, markdown does not parse HTML so you need to parse the node text for your tags and render the node in your extension with your changes. In this case the node is an HtmlInline, which will contain a contiguous run of HTML from the markdown.

If you don't have any mods for a particular node you can just let the previous renderer handle it by calling context.delegateRender()

The example from CoreNodeRenderer:

render(HtmlInline node, NodeRendererContext context, HtmlWriter html)

and the function which implements the meat of handling all the suppression and escaping options:

CoreNodeRenderer.java: Lines 742-750

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants