@wordpress/block-library
Version:
Block library for the WordPress editor.
27 lines (24 loc) • 733 B
JavaScript
/**
* WordPress dependencies
*/
import { cleanForSlug } from '@wordpress/url';
/**
* Generates a slug from a tab's text label.
*
* @param {string} label Tab label RichText value.
* @param {number} tabIndex Tab index value.
*
* @return {string} The generated slug with HTML stripped out.
*/
export default function slugFromLabel( label, tabIndex ) {
// Get just the text content, filtering out any HTML tags from the RichText value.
const htmlDocument = new window.DOMParser().parseFromString(
label,
'text/html'
);
if ( htmlDocument.body?.textContent ) {
return cleanForSlug( htmlDocument.body.textContent );
}
// Fall back to using the tab index if the label is empty.
return `tab-panel-${ tabIndex }`;
}