Repository navigation
Stop adding nofollow and target to every WYSIWYG link - #130
Closed
Contributolo wants to merge 1 commit into
Closed
Contributolo wants to merge 1 commit into
Contributolo wants to merge 1 commit into
Conversation
Tiptap's Link extension defaults HTMLAttributes to target="_blank" and
rel="noopener noreferrer nofollow", and configure() merges the field's
toolbar.formats.link.attributes into these defaults key by key. The link
modal then called setLink({ href, target }) with target "_self" when
"Open in new tab" was off and never passed rel. As a result every link
inserted through the toolbar was stored as
<a target="_self" rel="noopener noreferrer nofollow" href="...">
internal links included, and with "Open in new tab" on as
target="_blank" with the same rel. Stored links without target or rel
(written in code mode or pasted) picked up target="_blank" and
rel="noopener noreferrer nofollow" on the next edit of the field,
because Tiptap falls back to the attribute defaults when parsing.
Reset target and rel to null in HTMLAttributes, as the Tiptap Link docs
describe, and take target and rel from the link modal, which already
sets target="_blank" and rel="noreferrer noopener" for a new tab only.
Attributes configured in toolbar.formats.link.attributes still apply to
every link; a configured rel is kept and the new tab tokens are added
to it.
Contributor
|
Included in Blockstudio 7.7.4. The WYSIWYG link-attribute fix has shipped: unwanted target/nofollow attributes are no longer added, and explicit configuration is preserved. Your changes were incorporated into the consolidated release branch with integration tests and release adjustments, so I am closing this PR as shipped rather than merging the original branch again. Full CI (14/14) and Distribution (5/5) passed on release commit e1d38df. Thank you for the contribution. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Every link inserted through the WYSIWYG toolbar is stored with Tiptap's default link attributes, internal links included. Measured in the block editor with the 7.7.2 code, for an internal URL and for
https://wordpress.org/:"link": { "opensInNewTab": true }<a target="_self" rel="noopener noreferrer nofollow" href="...">"link": { "opensInNewTab": true }<a target="_blank" rel="noopener noreferrer nofollow" href="...">"attributes": { "rel": "nofollow" }(test blocktype-wysiwyg)<a target="_self" rel="nofollow" href="...">"attributes": { "rel": "nofollow" }<a target="_blank" rel="nofollow" href="...">A stored link without
targetorrel(written in code mode, pasted, or saved through the API) is rewritten to<a target="_blank" rel="noopener noreferrer nofollow" href="...">as soon as any text in that field is edited.nofollowon internal links tells search engines not to follow the site's own links,target="_self"is noise, and the last case silently turns plain links into new tab links.Cause
@tiptap/extension-linkdefaultsHTMLAttributesto{ target: '_blank', rel: 'noopener noreferrer nofollow', class: null }(addOptionsinsrc/link.ts), and thetarget,relandclassmark attributes default to these values (addAttributes).configure()merges options withmergeDeep(Extendable.configurein@tiptap/core), sotoolbar.formats.link.attributesonly replaces the keys it sets.setLink({ href, target: opensInNewTab ? '_blank' : '_self' })withoutrel, sorelkeeps its default.injectExtensionAttributesToParseRule), which is why plain stored links pick uptargetandrelon the next edit.The Tiptap docs describe resetting these with
rel: nullandtarget: nullinHTMLAttributes("Removing and overriding existing html attributes", https://tiptap.dev/docs/editor/extensions/marks/link).Fix
getWysiwygLinkHTMLAttributes()setstarget: nullandrel: nullbefore the configuredtoolbar.formats.link.attributes, so configured attributes still win and still apply to every link.getWysiwygLinkAttributes()builds thesetLink()attributes from whatLinkModalalready returns:normalizeLinkChange()setstarget="_blank"andrel="noreferrer noopener"for a new tab only, as for the link field. A configuredrelis kept and the new tab tokens are added to it. Turning "Open in new tab" off removestargetandrelagain.Result in the block editor:
attributes<a href="...">attributes<a target="_blank" rel="noreferrer noopener" href="...">"attributes": { "rel": "nofollow" }<a rel="nofollow" href="...">"attributes": { "rel": "nofollow" }<a target="_blank" rel="nofollow noreferrer noopener" href="...">attributes, stored<a href="...">, one character typed<a href="...">Existing content is not migrated: links already stored with
target="_self"orrel="noopener noreferrer nofollow"keep these attributes until the link is changed in the link modal.Tests
tests/client/wysiwyg-link.test.ts: a headless Tiptap editor without a DOM and the same Link configuration, links inserted the way the modal does, rendered attributes checked. All 5 tests fail with the 7.7.2 behavior and pass with the fix.npm run checkpasses.attributes. The existing E2E filestypes/wysiwyg/default.ts,types/wysiwyg/switch.tsandtypes/repeater/wysiwyg.tspass before and after (55 of 55).Relation to #125
#125 moves the extension list into
src/blocks/utils/wysiwyg-extensions.ts. Both PRs are based onmainand touch the sameLink.configure()call, so whichever lands second needs a small rebase: inwysiwyg-extensions.tsthe line becomesHTMLAttributes: getWysiwygLinkHTMLAttributes(item), andwysiwyg.tsxonly importsgetWysiwygLinkAttributes. I checked that combination (lint, types, client tests) and can rebase either branch.Notes
noreferrerfor new tab links (Stop adding “noreferrer” to external links. WordPress/gutenberg#26968,rel="noopener"only). This PR keeps the valueLinkModalalready uses (NEW_TAB_RELinfields/components/link.tsx), so the link field and the WYSIWYG field stay consistent. Changing it there would be a separate change.toolbar.formats.link(opensInNewTab,attributes) is not described inschemas/or the field docs yet, so there was no documentation to update.