UNPKG

@wordpress/block-library

Version:
27 lines (24 loc) 733 B
/** * 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 }`; }