-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Autogenerate heading anchors #30825
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
Merged
Merged
Autogenerate heading anchors #30825
Changes from 33 commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
5cb5c9a
auto-generate anchors for heading blocks
aristath ca2f664
Don't use "we" & "us" in comments
aristath 556f82b
account for non-latin anchors
aristath 4a6acd4
remove lodash dependency
aristath 042a1aa
Revert "remove lodash dependency"
aristath 8c4be85
add dom-ready dependency
aristath 766ef32
refactor everything
aristath 3296651
use useSelect
aristath 3e6928c
This was not removed on purpose
aristath 1e5d3d7
Move dummyElement inside getTextWithoutMarkup
aristath 2d20135
Update e2e tests
aristath 69efbab
Don't use a plain "wp-" as anchor
aristath e5af9c8
typo
aristath 15e04db
Improve empty string handling
aristath 635eca9
Improve inline comments
aristath 4c03e1b
Bugfix anchor generation for empty headings
aristath abeaaaf
Update packages/block-library/src/heading/edit.js
aristath 9f728c2
Use blockEditorStore
aristath 4579a72
cs
aristath 93b0d4d
refactor hook
aristath a9f6381
Update packages/block-library/src/heading/edit.js
aristath 762af2c
no need for lodash here, these are pretty simple
aristath c3ccf1c
refactor - no prefix
aristath 47014dd
update e2e
aristath cc34994
bugfix
aristath 4b9e037
simplify
aristath a1263d9
fix for block transforms
aristath 370409e
Improve generated slugs
aristath 951480c
bring back the prefix
aristath 9953f70
Revert "update e2e"
aristath 694180e
remove wp- prefix if not autogenerated anchor
aristath 34d1721
fix for empty/null anchors
aristath 50141d7
use useEffect properly
aristath 7e0f1d9
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath f1ad7c2
Refactor to not use a prefix.
aristath c61d9a7
Maybe not use setAttributes? :thinking:
aristath 7d8c3ea
no need for this condition
aristath 00006fe
This was too ugly to stay for more than 10 minutes
aristath 7b8b783
remove prefix from tests
aristath 516c400
alternative to useEffect
aristath 842449d
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 3ec7e1e
Revert "alternative to useEffect"
aristath 171f228
only watch the content
aristath 577feb6
mark anchor changes as not persistent
aristath 5706771
yet another refactor
aristath 60114f1
fix for legacy headers & conversions
aristath 0dbefeb
Typo was causing a crash
aristath 2a021a6
use useEffect for side effects
ntsekouras 716b5a9
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 4f5c06e
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 88cf66c
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 8c0e353
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 234abfc
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 6535526
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 8f39085
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 43cc150
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath db12f78
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 5bb9302
Simplify & improve performance
aristath bbabee8
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 98d29dc
Use an object instead of Set
aristath b5cb1f9
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 2b1f215
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath 79f49ff
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath b5b34a8
Merge branch 'trunk' into add/autogenerate-heading-anchors
aristath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
packages/block-library/src/heading/autogenerate-anchors.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { deburr, trim } from 'lodash'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
|
||
| /** | ||
| * Runs a callback over all blocks, including nested blocks. | ||
| * | ||
| * @param {Object[]} blocks The blocks. | ||
| * @param {Function} callback The callback. | ||
| * | ||
| * @return {void} | ||
| */ | ||
| const recurseOverBlocks = ( blocks, callback ) => { | ||
| for ( const block of blocks ) { | ||
| // eslint-disable-next-line callback-return | ||
| callback( block ); | ||
| if ( block.innerBlocks ) { | ||
| recurseOverBlocks( block.innerBlocks, callback ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Returns the text without markup. | ||
| * | ||
| * @param {string} text The text. | ||
| * | ||
| * @return {string} The text without markup. | ||
| */ | ||
| const getTextWithoutMarkup = ( text ) => { | ||
| const dummyElement = document.createElement( 'div' ); | ||
| dummyElement.innerHTML = text; | ||
| return dummyElement.innerText; | ||
| }; | ||
|
|
||
| /** | ||
| * Get all heading anchors. | ||
| * | ||
| * @param {Object} blockList An object containing all blocks. | ||
| * @param {string} excludeId A block ID we want to exclude. | ||
| * | ||
| * @return {string[]} Return an array of anchors. | ||
| */ | ||
| const getAllHeadingAnchors = ( blockList, excludeId ) => { | ||
| const anchors = []; | ||
|
|
||
| recurseOverBlocks( blockList, ( block ) => { | ||
| if ( | ||
| block.name === 'core/heading' && | ||
| ( ! excludeId || block.clientId !== excludeId ) && | ||
| block.attributes.anchor | ||
| ) { | ||
| anchors.push( block.attributes.anchor ); | ||
| } | ||
| } ); | ||
|
|
||
| return anchors; | ||
| }; | ||
|
|
||
| /** | ||
| * Get the slug from the content. | ||
| * | ||
| * @param {string} content The block content. | ||
| * | ||
| * @return {string} Returns the slug. | ||
| */ | ||
| const getSlug = ( content ) => { | ||
| // Get the slug. | ||
| return trim( | ||
| deburr( getTextWithoutMarkup( content ) ) | ||
| .replace( /[^\p{L}\p{N}]+/gu, '-' ) | ||
| .toLowerCase(), | ||
| '-' | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Generate the anchor for a heading. | ||
| * | ||
| * @param {string} anchor The heading anchor. | ||
| * @param {string} content The block content. | ||
| * @param {string[]} allHeadingAnchors An array containing all headings anchors. | ||
| * | ||
| * @return {string|null} Return the heading anchor. | ||
| */ | ||
| const generateAnchor = ( anchor, content, allHeadingAnchors ) => { | ||
aristath marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const slug = getSlug( content ); | ||
| // If slug is empty, then return null. | ||
| // Returning null instead of an empty string allows us to check again when the content changes. | ||
| if ( '' === slug ) { | ||
| return null; | ||
| } | ||
|
|
||
| const baseAnchor = `wp-${ slug }`; | ||
| anchor = baseAnchor; | ||
| let i = 0; | ||
|
|
||
| // If the anchor already exists in another heading, append -i. | ||
| while ( allHeadingAnchors.includes( anchor ) ) { | ||
| i += 1; | ||
| anchor = baseAnchor + '-' + i; | ||
| } | ||
|
|
||
| return anchor; | ||
| }; | ||
|
|
||
| /** | ||
| * Updates the anchor if required. | ||
| * | ||
| * @param {string} clientId The block's client-ID. | ||
| * @param {string} anchor The heading anchor. | ||
| * @param {string} content The block content. | ||
| * | ||
| * @return {string} The anchor. | ||
| */ | ||
| export default function useGeneratedAnchor( clientId, anchor, content ) { | ||
| const allHeadingAnchors = useSelect( | ||
| ( select ) => { | ||
| const allBlocks = select( blockEditorStore ).getBlocks(); | ||
| return getAllHeadingAnchors( allBlocks, clientId ); | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
| return ! anchor || anchor.startsWith( 'wp-' ) | ||
| ? generateAnchor( anchor, content, allHeadingAnchors ) | ||
| : anchor; | ||
| } | ||
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
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
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.