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

CKEditor config addTargetToExternalLinks doesn't look for internal links. #98

Closed
mikesnoeren opened this issue May 5, 2023 · 9 comments
Labels

Comments

@mikesnoeren
Copy link

Description

When adding this option the expected behaviour would be that only external links get the target="blank" attribute, however, all links get it.

Config:

return {
  link: {
    addTargetToExternalLinks: true,
  },
}

Workaround is to set targets manually, using this config:

return {
  "link": {
    "decorators": {
      "openInNewTab": {
        "attributes": {
          "target": "_blank"
        },
        "label": "Open in a new tab",
        "mode": "manual"
      }
    }
  }
}
@mikesnoeren mikesnoeren added the bug label May 5, 2023
@lukas-jansen
Copy link

https://ckeditor.com/docs/ckeditor5/latest/features/link.html#adding-target-and-rel-attributes-to-external-links looks to be the expected behavior for CkEditor but it is confusing that links to entries are now also getting it.
Maybe it shouldn't be set as a default config option?

@brandonkelly
Copy link
Member

Gross, I didn’t realize how loosely it labels links as “external”. The ckeditor/convert command won’t set link.addTargetToExternalLinks = true anymore going forward.

@brandonkelly
Copy link
Member

CKEditor 3.4.0 is out with that change.

@remcoov
Copy link

remcoov commented May 31, 2023

@brandonkelly Internal links are still being treated as external links, in new CKEditor fields..

@kevinmu17
Copy link

When using the decorator within the link option and you are using the rel attribute, you need to update your htmlpurifier config with this:

CKeditor config option (within the Craft CMS cp)

{
  "link": {
    "addTargetToExternalLinks": false,
    "decorators": {
      "openInNewTab": {
        "attributes": {
          "rel": "noopener noreferrer",
          "target": "_blank"
        },
        "label": "Open in a new tab",
        "mode": "manual"
      }
    }
  }
}

htmlpurifier config (.json)

{
  "Attr.AllowedFrameTargets": [
    "_blank"
  ],
  "Attr.AllowedRel": [
    "noopener",
    "noreferrer"
  ]
}

Maybe this can be implemented as a default @brandonkelly ? since the rel option to externals is necessary for security reasons

@CreateSean
Copy link

I copied what @kevinmu17 pasted above and I still don't see an option to control the target when creating a link in Craft.

Every single link whether internal or external is now opening in a new tab. Really need a solution to this..

@brandonkelly
Copy link
Member

I copied what @kevinmu17 pasted above and I still don't see an option to control the target when creating a link in Craft.

@CreateSean If the “Open in a new tab” decorator isn’t showing up, you must have put the decorator settings in the wrong CKEditor config, or in the wrong spot. I just copied @kevinmu17’s config verbatim to my install, to make sure there’s no typos or anything.

Every single link whether internal or external is now opening in a new tab. Really need a solution to this.

That will only happen automatically if link.addTargetToExternalLinks is set to true.

@realjoshharrison
Copy link

Building on #98 (comment) from @kevinmu17, you can create a custom automatic decorator with external detection - here's my JavaScript config in the CP:

return {
  link: {
    decorators: {
      detectExternalLinks: {
        mode: 'automatic',
        callback: url => ! url.startsWith(window.location.origin),
        attributes: {
          target: '_blank',
          rel: 'noopener noreferrer',
        }
      }
    }
  }
}

Obviously this only works when the front end and control panel are both running on the same origin. There's probably a better way, but it's a start!

@jeroenlammerts
Copy link

jeroenlammerts commented Aug 12, 2024

Related issue having trouble with the switch not detecting after save.

#280

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

No branches or pull requests

8 participants