block-content.ts
2 years ago
capitalize.ts
2 years ago
compare-blocks.ts
2 years ago
fix-incomplete-html.ts
3 years ago
get-feature-availability.ts
2 years ago
block-content.ts
106 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { renderMarkdownFromHTML } from '@automattic/jetpack-ai-client'; |
| 5 | import { getBlockContent } from '@wordpress/blocks'; |
| 6 | import { serialize } from '@wordpress/blocks'; |
| 7 | import { select } from '@wordpress/data'; |
| 8 | /** |
| 9 | * Internal dependencies |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Returns partial content from the beginning of the post |
| 14 | * to the current block, based on the given block clientId. |
| 15 | * |
| 16 | * @param {string} clientId - The current block clientId. |
| 17 | * @returns {string} The partial content. |
| 18 | */ |
| 19 | export function getPartialContentToBlock( clientId: string ): string { |
| 20 | if ( ! clientId ) { |
| 21 | return ''; |
| 22 | } |
| 23 | |
| 24 | const editor = select( 'core/block-editor' ); |
| 25 | const index = editor.getBlockIndex( clientId ); |
| 26 | const blocks = editor.getBlocks().slice( 0, index ) ?? []; |
| 27 | |
| 28 | if ( ! blocks?.length ) { |
| 29 | return ''; |
| 30 | } |
| 31 | |
| 32 | return renderMarkdownFromHTML( { content: serialize( blocks ) } ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns content from all blocks, |
| 37 | * by inspecting the blocks `content` attributes |
| 38 | * |
| 39 | * @returns {string} The content. |
| 40 | */ |
| 41 | export function getContentFromBlocks(): string { |
| 42 | const editor = select( 'core/block-editor' ); |
| 43 | const blocks = editor.getBlocks(); |
| 44 | |
| 45 | if ( ! blocks?.length ) { |
| 46 | return ''; |
| 47 | } |
| 48 | |
| 49 | return renderMarkdownFromHTML( { content: serialize( blocks ) } ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Given a list of blocks, it returns their content as a string. |
| 54 | * @param {Array} blocks - The list of blocks. |
| 55 | * @returns {string} The content of the blocks as a string. |
| 56 | */ |
| 57 | export function getBlocksContent( blocks ) { |
| 58 | return blocks |
| 59 | .filter( block => block != null ) // Safeguard against null or undefined blocks |
| 60 | .map( block => getBlockContent( block ) ) |
| 61 | .join( '\n\n' ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the text content of the inner blocks of a block. |
| 66 | * |
| 67 | * @param {string} clientId - The block clientId. |
| 68 | * @returns {string} The text content. |
| 69 | */ |
| 70 | export function getTextContentFromInnerBlocks( clientId: string ) { |
| 71 | const block = select( 'core/block-editor' ).getBlock( clientId ); |
| 72 | if ( ! block?.innerBlocks?.length ) { |
| 73 | return ''; |
| 74 | } |
| 75 | |
| 76 | return getBlocksContent( block.innerBlocks ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Extract raw text from HTML content |
| 81 | * |
| 82 | * @param {string} htmlString - The HTML content. |
| 83 | * @returns {string} The raw text. |
| 84 | */ |
| 85 | export function getRawTextFromHTML( htmlString: string ): string { |
| 86 | // Removes all continuous whitespace from the start to check if the string is empty |
| 87 | if ( ! htmlString?.replace( /\s+/, '' ).length ) { |
| 88 | return ''; |
| 89 | } |
| 90 | |
| 91 | const tempDomContainer = document.createElement( 'div' ); |
| 92 | tempDomContainer.innerHTML = htmlString; |
| 93 | |
| 94 | const { textContent, innerText } = tempDomContainer; |
| 95 | |
| 96 | if ( !! textContent && ! textContent.replace( /\s+/, '' ).length ) { |
| 97 | return ''; |
| 98 | } |
| 99 | |
| 100 | if ( !! innerText && ! innerText.replace( /\s+/, '' ).length ) { |
| 101 | return ''; |
| 102 | } |
| 103 | |
| 104 | return tempDomContainer.textContent || tempDomContainer.innerText || ''; |
| 105 | } |
| 106 |