@automattic/jetpack-ai-client 0.27.0 → 0.27.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.27.1] - 2025-03-10
9
+ ### Added
10
+ - AI Client: Add optional preprocess function to getPostContent. [#42269]
11
+
12
+ ### Changed
13
+ - Update dependencies. [#42222]
14
+
8
15
  ## [0.27.0] - 2025-03-03
9
16
  ### Added
10
17
  - AI Client: Move openBlockSidebar utility function. [#42016]
@@ -32,7 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
32
39
 
33
40
  ## [0.26.0] - 2025-02-10
34
41
  ### Added
35
- - Add shared components from ai-assistant-plugin [#41078]
42
+ - Add shared components from ai-assistant-plugin. [#41078]
36
43
 
37
44
  ### Changed
38
45
  - Updated package dependencies. [#41491] [#41577]
@@ -542,6 +549,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
542
549
  - AI Client: stop using smart document visibility handling on the fetchEventSource library, so it does not restart the completion when changing tabs. [#32004]
543
550
  - Updated package dependencies. [#31468] [#31659] [#31785]
544
551
 
552
+ [0.27.1]: https://github.com/Automattic/jetpack-ai-client/compare/v0.27.0...v0.27.1
545
553
  [0.27.0]: https://github.com/Automattic/jetpack-ai-client/compare/v0.26.3...v0.27.0
546
554
  [0.26.3]: https://github.com/Automattic/jetpack-ai-client/compare/v0.26.2...v0.26.3
547
555
  [0.26.2]: https://github.com/Automattic/jetpack-ai-client/compare/v0.26.1...v0.26.2
@@ -1,5 +1,6 @@
1
1
  declare const usePostContent: () => {
2
- getPostContent: () => string;
2
+ getPostContent: (preprocess?: (serialized: string) => string) => string;
3
3
  isEditedPostEmpty: () => boolean;
4
+ getSerializedPostContent: () => any;
4
5
  };
5
6
  export default usePostContent;
@@ -21,10 +21,23 @@ const usePostContent = () => {
21
21
  isEditedPostEmpty: coreEditorSelect.isEditedPostEmpty,
22
22
  };
23
23
  }, []);
24
- const getPostContent = useCallback(() => {
24
+ const getSerializedPostContent = useCallback(() => {
25
25
  const blocks = getBlocks();
26
- return blocks?.length ? renderMarkdownFromHTML({ content: serialize(blocks) }) : '';
26
+ if (blocks.length === 0) {
27
+ return '';
28
+ }
29
+ return serialize(blocks);
27
30
  }, [getBlocks]);
28
- return { getPostContent, isEditedPostEmpty };
31
+ const getPostContent = useCallback((preprocess) => {
32
+ let serialized = getSerializedPostContent();
33
+ if (!serialized) {
34
+ return '';
35
+ }
36
+ if (preprocess && typeof preprocess === 'function') {
37
+ serialized = preprocess(serialized);
38
+ }
39
+ return serialized ? renderMarkdownFromHTML({ content: serialized }) : '';
40
+ }, [getBlocks]);
41
+ return { getPostContent, isEditedPostEmpty, getSerializedPostContent };
29
42
  };
30
43
  export default usePostContent;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { Block } from '../types.js';
5
+ /**
6
+ * Recursively get all blocks from the post, including nested innerBlocks
7
+ * @return {Array} Array of all blocks in the post
8
+ */
9
+ export declare const getAllBlocks: () => Array<Block>;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { select } from '@wordpress/data';
5
+ /**
6
+ * Recursively get all blocks from the post, including nested innerBlocks
7
+ * @return {Array} Array of all blocks in the post
8
+ */
9
+ export const getAllBlocks = () => {
10
+ const topLevelBlocks = select('core/block-editor').getBlocks();
11
+ const allBlocks = [];
12
+ const processBlock = (block) => {
13
+ allBlocks.push(block);
14
+ if (block.innerBlocks?.length) {
15
+ block.innerBlocks.forEach(processBlock);
16
+ }
17
+ };
18
+ topLevelBlocks.forEach(processBlock);
19
+ return allBlocks;
20
+ };
@@ -3,3 +3,4 @@ export type { RenderHTMLRules } from './markdown/index.js';
3
3
  export { mapActionToHumanText } from './map-action-to-human-text.js';
4
4
  export { openBlockSidebar } from './open-block-sidebar.js';
5
5
  export { showAiAssistantSection } from './show-ai-assistant-section.js';
6
+ export { getAllBlocks } from './get-all-blocks.js';
@@ -2,3 +2,4 @@ export { MarkdownToHTML, HTMLToMarkdown, renderHTMLFromMarkdown, renderMarkdownF
2
2
  export { mapActionToHumanText } from './map-action-to-human-text.js';
3
3
  export { openBlockSidebar } from './open-block-sidebar.js';
4
4
  export { showAiAssistantSection } from './show-ai-assistant-section.js';
5
+ export { getAllBlocks } from './get-all-blocks.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@automattic/jetpack-ai-client",
4
- "version": "0.27.0",
4
+ "version": "0.27.1",
5
5
  "description": "A JS client for consuming Jetpack AI services",
6
6
  "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/ai-client/#readme",
7
7
  "bugs": {
@@ -45,9 +45,9 @@
45
45
  "types": "./build/index.d.ts",
46
46
  "dependencies": {
47
47
  "@automattic/jetpack-base-styles": "^0.6.44",
48
- "@automattic/jetpack-components": "^0.68.0",
49
- "@automattic/jetpack-connection": "^0.38.0",
50
- "@automattic/jetpack-shared-extension-utils": "^0.17.4",
48
+ "@automattic/jetpack-components": "^0.68.1",
49
+ "@automattic/jetpack-connection": "^0.39.0",
50
+ "@automattic/jetpack-shared-extension-utils": "^0.17.5",
51
51
  "@microsoft/fetch-event-source": "2.0.1",
52
52
  "@types/jest": "29.5.14",
53
53
  "@types/react": "18.3.18",
@@ -28,13 +28,34 @@ const usePostContent = () => {
28
28
  };
29
29
  }, [] );
30
30
 
31
- const getPostContent = useCallback( () => {
31
+ const getSerializedPostContent = useCallback( () => {
32
32
  const blocks = getBlocks();
33
33
 
34
- return blocks?.length ? renderMarkdownFromHTML( { content: serialize( blocks ) } ) : '';
34
+ if ( blocks.length === 0 ) {
35
+ return '';
36
+ }
37
+
38
+ return serialize( blocks );
35
39
  }, [ getBlocks ] );
36
40
 
37
- return { getPostContent, isEditedPostEmpty };
41
+ const getPostContent = useCallback(
42
+ ( preprocess?: ( serialized: string ) => string ) => {
43
+ let serialized = getSerializedPostContent();
44
+
45
+ if ( ! serialized ) {
46
+ return '';
47
+ }
48
+
49
+ if ( preprocess && typeof preprocess === 'function' ) {
50
+ serialized = preprocess( serialized );
51
+ }
52
+
53
+ return serialized ? renderMarkdownFromHTML( { content: serialized } ) : '';
54
+ },
55
+ [ getBlocks ]
56
+ );
57
+
58
+ return { getPostContent, isEditedPostEmpty, getSerializedPostContent };
38
59
  };
39
60
 
40
61
  export default usePostContent;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { select } from '@wordpress/data';
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+ import type { Block } from '../types.js';
9
+
10
+ /**
11
+ * Recursively get all blocks from the post, including nested innerBlocks
12
+ * @return {Array} Array of all blocks in the post
13
+ */
14
+ export const getAllBlocks = (): Array< Block > => {
15
+ const topLevelBlocks = select( 'core/block-editor' ).getBlocks();
16
+ const allBlocks: Array< Block > = [];
17
+
18
+ const processBlock = ( block: Block ) => {
19
+ allBlocks.push( block );
20
+
21
+ if ( block.innerBlocks?.length ) {
22
+ block.innerBlocks.forEach( processBlock );
23
+ }
24
+ };
25
+
26
+ topLevelBlocks.forEach( processBlock );
27
+
28
+ return allBlocks;
29
+ };
package/src/libs/index.ts CHANGED
@@ -13,3 +13,5 @@ export { mapActionToHumanText } from './map-action-to-human-text.js';
13
13
  export { openBlockSidebar } from './open-block-sidebar.js';
14
14
 
15
15
  export { showAiAssistantSection } from './show-ai-assistant-section.js';
16
+
17
+ export { getAllBlocks } from './get-all-blocks.js';