block-content.ts
116 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { getBlockContent } from '@wordpress/blocks'; |
| 5 | import { serialize } from '@wordpress/blocks'; |
| 6 | import { select } from '@wordpress/data'; |
| 7 | import TurndownService from 'turndown'; |
| 8 | |
| 9 | // Turndown instance |
| 10 | const turndownService = new TurndownService(); |
| 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 turndownService.turndown( 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 turndownService.turndown( serialize( blocks ) ); |
| 50 | } |
| 51 | |
| 52 | export function getTextContentFromInnerBlocks( clientId: string ) { |
| 53 | const block = select( 'core/block-editor' ).getBlock( clientId ); |
| 54 | if ( ! block?.innerBlocks?.length ) { |
| 55 | return ''; |
| 56 | } |
| 57 | |
| 58 | return block.innerBlocks |
| 59 | .filter( blq => blq != null ) // Safeguard against null or undefined blocks |
| 60 | .map( blq => getBlockTextContent( blq.clientId ) ) |
| 61 | .join( '\n\n' ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Return the block content from the given block clientId using the `getBlockContent` function. |
| 66 | * |
| 67 | * @param {string} clientId - The block clientId. |
| 68 | * @returns {string} The block content. |
| 69 | */ |
| 70 | export function getBlockTextContent( clientId: string ): string { |
| 71 | if ( ! clientId ) { |
| 72 | return ''; |
| 73 | } |
| 74 | |
| 75 | const editor = select( 'core/block-editor' ); |
| 76 | const block = editor.getBlock( clientId ); |
| 77 | |
| 78 | /* |
| 79 | * In some context, the block can be undefined, |
| 80 | * for instance, when previewing the block. |
| 81 | */ |
| 82 | if ( ! block ) { |
| 83 | return ''; |
| 84 | } |
| 85 | |
| 86 | return getBlockContent( block ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Extract raw text from HTML content |
| 91 | * |
| 92 | * @param {string} htmlString - The HTML content. |
| 93 | * @returns {string} The raw text. |
| 94 | */ |
| 95 | export function getRawTextFromHTML( htmlString: string ): string { |
| 96 | // Removes all continuous whitespace from the start to check if the string is empty |
| 97 | if ( ! htmlString?.replace( /\s+/, '' ).length ) { |
| 98 | return ''; |
| 99 | } |
| 100 | |
| 101 | const tempDomContainer = document.createElement( 'div' ); |
| 102 | tempDomContainer.innerHTML = htmlString; |
| 103 | |
| 104 | const { textContent, innerText } = tempDomContainer; |
| 105 | |
| 106 | if ( !! textContent && ! textContent.replace( /\s+/, '' ).length ) { |
| 107 | return ''; |
| 108 | } |
| 109 | |
| 110 | if ( !! innerText && ! innerText.replace( /\s+/, '' ).length ) { |
| 111 | return ''; |
| 112 | } |
| 113 | |
| 114 | return tempDomContainer.textContent || tempDomContainer.innerText || ''; |
| 115 | } |
| 116 |