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