-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 attributes to injected HTML - e.g. target=_blank
on a link?
#760
Comments
Do you mean changing the format how the official link plugin editor produces all links? Or do you mean some new feature, e.g. a checkbox in the link balloon? |
@Reinmar whatever you think makes sense? i'd be happy with a simple config change or similar that allows me to force all links to be external at the code level |
And what if you want to use I don't agree with @thedavidmeister. It should be configurable per link, or (if this is considered out of scope), it's up to the developer to force the links to use "target=_blank" (using js, or modifying the HTML). |
OK, that's why I asked :D Because there are two cases here:
Extending existing featuresAdding features like the ability to set link's
The first task is very contextual, so different approaches need to be applied to different cases. Sometimes you'll want to implement your own new UI from scratch, sometimes you'll want to add a new input to the existing UI, sometimes you don't need a UI but some integration with your backend/whatever. What helps here is that features are implemented in a pretty modular way. We call it a "feature split" and it's under refactoring right now so we're inconsistent here and the existing code is imperfect. You can read more about it in #488 and #425. The second task is supported by the conversion mechanism through its event-based nature and so-called conversion dispatchers (which let you convert each attribute and node separately). You can e.g. use the existing link plugin and add your own plugin which will support converting Unfortunately, the involved APIs are also being refactored as we speak and I'm unable to point you to the documentation. Both, API guides and proper architecture guides will be written as soon as we stabilise these concepts. Right now I can only send you to the source code but with a warning that what was released in alpha 2 looks very different than what we have right now. Changing features behaviourIf you want the link feature to produce a different HTML output, which better match your needs then:
Unfortunately, as in case of extending features, this is also all under development and will be released and documented soon. |
target=_blank
on a link?target=_blank
on a link?
ok well cool that it is under development. i feel like my original question got a bit scope creeped here to be a more general feature discussion... i had assumed (wrongly) that adding an html attribute to output would be straightforward and i was just misunderstanding something, seems like this is all still WIP. for my use case i'm happy for target |
I'll add one more option to ones presented by @Reinmar under "changing feature behavior". If you want same behavior, each time a link is added, without any additional UI, then you could also introduce a plugin that will add an additional attribute whenever link is inserted. First, you would have to specify a new attribute for Second, you would need to add a callback on Third, you would need to provide a simple converter for that attribute. That could be done through This all, however, will change pretty soon, and will be simpler, but quite different, so I'd just hold on for now. |
ok np, this one is not urgent so i'll hold out til after API changes |
I'm not sure if this falls into straightforward, but that's the code you'd need today: data.modelToView.on( 'attribute:linkHref:$text', ( evt, data, consumable, conversionApi ) => {
if ( !data.attributeNewValue ) {
return;
}
const viewRange = conversionApi.mapper.toViewRange( data.range );
const linkElement = new LinkElement( 'a', { target: '_blank' } );
linkElement.priority = 5;
viewWriter.wrap( viewRange, linkElement );
} ); This makes There are some uncool things in this code, but I'll not go into that now since it may change. So, that's for now. In the future adding this @scofalik You overcomplicated this topic :P You don't need another model attribute if all links should have this attribute. A link is now represented in the model by the |
That's true, I thought that bringing new converter will be more difficult, but the link's default converter is very simple, so yeah, this converter is actually quite simple. |
π @scofalik wrote an amazing answer about this on SO: https://stackoverflow.com/questions/51303892/how-to-add-target-attribute-to-a-tag-in-ckeditor5/51365516#51365516 |
Recently we added the link decorators feature designed to solve that (and any other link attribute), see the Custom link attributes (decorators) section in our documentation for more information. |
π Is this a bug report or feature request? (choose one)
support
π» Version of CKEditor
alpha2
π Steps to reproduce
β Expected result
links open in a new tab
β Actual result
links open in current tab
π Other details that might be useful
just not sure how to do this?
The text was updated successfully, but these errors were encountered: