block-content.ts
144 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 | const HTML_JOIN_CHARACTERS = '<br />'; |
| 13 | |
| 14 | /** |
| 15 | * Returns partial content from the beginning of the post |
| 16 | * to the current block, based on the given block clientId. |
| 17 | * |
| 18 | * @param {string} clientId - The current block clientId. |
| 19 | * @returns {string} The partial content. |
| 20 | */ |
| 21 | export function getPartialContentToBlock( clientId: string ): string { |
| 22 | if ( ! clientId ) { |
| 23 | return ''; |
| 24 | } |
| 25 | |
| 26 | const editor = select( 'core/block-editor' ); |
| 27 | const index = editor.getBlockIndex( clientId ); |
| 28 | const blocks = editor.getBlocks().slice( 0, index ) ?? []; |
| 29 | |
| 30 | if ( ! blocks?.length ) { |
| 31 | return ''; |
| 32 | } |
| 33 | |
| 34 | return turndownService.turndown( serialize( blocks ) ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns content from all blocks, |
| 39 | * by inspecting the blocks `content` attributes |
| 40 | * |
| 41 | * @returns {string} The content. |
| 42 | */ |
| 43 | export function getContentFromBlocks(): string { |
| 44 | const editor = select( 'core/block-editor' ); |
| 45 | const blocks = editor.getBlocks(); |
| 46 | |
| 47 | if ( ! blocks?.length ) { |
| 48 | return ''; |
| 49 | } |
| 50 | |
| 51 | return turndownService.turndown( serialize( blocks ) ); |
| 52 | } |
| 53 | |
| 54 | type GetTextContentFromBlocksProps = { |
| 55 | count: number; |
| 56 | clientIds: string[]; |
| 57 | content: string; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Returns the text content from all selected blocks. |
| 62 | * |
| 63 | * @returns {GetTextContentFromBlocksProps} The text content. |
| 64 | */ |
| 65 | |
| 66 | export function getTextContentFromSelectedBlocks(): GetTextContentFromBlocksProps { |
| 67 | const clientIds = select( 'core/block-editor' ).getSelectedBlockClientIds(); |
| 68 | const defaultContent = { |
| 69 | count: 0, |
| 70 | clientIds: [], |
| 71 | content: '', |
| 72 | }; |
| 73 | |
| 74 | if ( ! clientIds?.length ) { |
| 75 | return defaultContent; |
| 76 | } |
| 77 | |
| 78 | const blocks = select( 'core/block-editor' ).getBlocksByClientId( clientIds ); |
| 79 | if ( ! blocks?.length ) { |
| 80 | return defaultContent; |
| 81 | } |
| 82 | |
| 83 | return { |
| 84 | count: blocks.length, |
| 85 | clientIds, |
| 86 | content: blocks |
| 87 | ? blocks |
| 88 | .filter( block => block !== null && block !== undefined ) // Safeguard against null or undefined blocks |
| 89 | .map( block => getBlockTextContent( block.clientId ) ) |
| 90 | .join( HTML_JOIN_CHARACTERS ) |
| 91 | : '', |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Return the block content from the given block clientId. |
| 97 | * |
| 98 | * It will try to get the content from the block `content` attribute. |
| 99 | * Otherwise, it will try to get the content |
| 100 | * by using the `getBlockContent` function. |
| 101 | * |
| 102 | * @param {string} clientId - The block clientId. |
| 103 | * @returns {string} The block content. |
| 104 | */ |
| 105 | export function getBlockTextContent( clientId: string ): string { |
| 106 | if ( ! clientId ) { |
| 107 | return ''; |
| 108 | } |
| 109 | |
| 110 | const editor = select( 'core/block-editor' ); |
| 111 | const block = editor.getBlock( clientId ); |
| 112 | |
| 113 | /* |
| 114 | * In some context, the block can be undefined, |
| 115 | * for instance, when previewing the block. |
| 116 | */ |
| 117 | if ( ! block ) { |
| 118 | return ''; |
| 119 | } |
| 120 | |
| 121 | // Attempt to pick the content from the block `content` attribute. |
| 122 | if ( block?.attributes?.content ) { |
| 123 | return block.attributes.content; |
| 124 | } |
| 125 | |
| 126 | return getBlockContent( block ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Extract raw text from HTML content |
| 131 | * |
| 132 | * @param {string} htmlString - The HTML content. |
| 133 | * @returns {string} The raw text. |
| 134 | */ |
| 135 | export function getRawTextFromHTML( htmlString: string ): string { |
| 136 | if ( ! htmlString?.length ) { |
| 137 | return ''; |
| 138 | } |
| 139 | |
| 140 | const tempDomContainer = document.createElement( 'div' ); |
| 141 | tempDomContainer.innerHTML = htmlString; |
| 142 | return tempDomContainer.textContent || tempDomContainer.innerText || ''; |
| 143 | } |
| 144 |