Skip to content

Commit

Permalink
AI Proofread: Put on initial stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoagds committed Jul 16, 2024
1 parent fa266e8 commit 8a9aebb
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions projects/js-packages/ai-client/src/libs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
HTMLToMarkdown,
renderHTMLFromMarkdown,
renderMarkdownFromHTML,
fixes,
} from './markdown/index.js';

export type { RenderHTMLRules } from './markdown/index.js';
4 changes: 2 additions & 2 deletions projects/js-packages/ai-client/src/libs/markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import HTMLToMarkdown from './html-to-markdown.js';
import MarkdownToHTML from './markdown-to-html.js';
import MarkdownToHTML, { fixes } from './markdown-to-html.js';
/**
* Types
*/
Expand All @@ -29,4 +29,4 @@ const renderMarkdownFromHTML = ( { content }: { content: string } ) => {
return defaultHTMLConverter.render( { content } );
};

export { MarkdownToHTML, HTMLToMarkdown, renderHTMLFromMarkdown, renderMarkdownFromHTML };
export { MarkdownToHTML, HTMLToMarkdown, renderHTMLFromMarkdown, renderMarkdownFromHTML, fixes };
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MarkdownIt from 'markdown-it';
*/
import type { Options } from 'markdown-it';

export type Fix = 'list' | 'paragraph' | 'listItem';
export type Fix = 'list' | 'paragraph' | 'listItem' | 'table';

const addListComments = ( content: string ) => {
return (
Expand All @@ -32,7 +32,7 @@ const addListComments = ( content: string ) => {
type Fixes = {
[ key in Fix ]: ( content: string, extension?: boolean ) => string;
};
const fixes: Fixes = {
export const fixes: Fixes = {
list: ( content: string, extension = false ) => {
// Fix list indentation
const fixedIndentation = content
Expand Down Expand Up @@ -61,6 +61,15 @@ const fixes: Fixes = {
// Fix encoding of <br /> tags
return content.replaceAll( /\s*&lt;br \/&gt;\s*/g, '<br />' );
},
table: ( content: string, extension = false ) => {
if ( ! extension ) {
return content;
}

return content
.replace( /^<figure.*?>/g, '' ) // Remove figure start for table block
.replace( /<\/figure>$/g, '' ); // Remove figure end for table block
},
};

const defaultMarkdownItOptions: Options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { renderHTMLFromMarkdown } from '@automattic/jetpack-ai-client';
import { fixes } from '@automattic/jetpack-ai-client';
import { rawHandler } from '@wordpress/blocks';
import { Button, Popover, Spinner } from '@wordpress/components';
import { select as globalSelect, useDispatch, useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -82,10 +82,12 @@ export default function Highlight() {
};

const handleSuggestions = () => {
const sentence = ( anchor as HTMLElement )?.innerText;
const target = ( anchor as HTMLElement )?.innerText;
const sentence = ( anchor as HTMLElement )?.parentElement?.innerText;

setSuggestions( {
id,
target,
feature,
sentence,
blockId: block,
Expand All @@ -94,11 +96,8 @@ export default function Highlight() {

const handleApplySuggestion = () => {
// Apply known fixes
const render = renderHTMLFromMarkdown( {
content: suggestions?.revisedText,
rules: [ 'listItem', 'list', 'paragraph' ],
extension: true,
} );
const render = fixes.listItem( suggestions?.html, true ); // Replace li for WP tags

const [ newBlock ] = rawHandler( { HTML: render } );
updateBlockAttributes( block, newBlock.attributes );
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ export function toggleFeature( feature: string, force?: boolean ) {
export function setSuggestions( {
id,
feature,
target,
sentence,
blockId,
}: {
id: string;
feature: string;
target: string;
sentence: string;
blockId: string;
} ) {
Expand All @@ -70,6 +72,7 @@ export function setSuggestions( {
askQuestionSync(
getRequestMessages( {
feature,
target,
sentence,
blockId,
} ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type BreveState = {
[ key: string ]: {
loading: boolean;
suggestions: {
revisedText: string;
html: string;
suggestion: string;
};
};
Expand All @@ -45,7 +45,7 @@ export type BreveSelect = {
feature: string,
id: string
) => {
revisedText: string;
html: string;
suggestion: string;
};
};
Expand All @@ -61,6 +61,7 @@ export type BreveDispatch = {
setSuggestions: ( suggestions: {
id: string;
feature: string;
target: string;
sentence: string;
blockId: string;
} ) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Internal dependencies
*/
import { getBlockContent } from '@wordpress/blocks';
import { select } from '@wordpress/data';
import features from '../features/index.js';

Expand All @@ -14,20 +15,21 @@ const requestTypeMap = {
// adjective: 'breve-adjective',
};

export const getRequestMessages = ( { feature, sentence, blockId } ) => {
export const getRequestMessages = ( { feature, target, sentence, blockId } ) => {
const block = select( 'core/block-editor' ).getBlock( blockId );
const html = block?.originalContent;
const html = getBlockContent( block );
const dictionary = features?.find?.( ftr => ftr.config.name === feature )?.dictionary || {};
const replacement = dictionary[ sentence.toLowerCase() ] || null;
const replacement = dictionary[ target.toLowerCase() ] || null;

return [
{
role: 'jetpack-ai' as const,
context: {
type: requestTypeMap[ feature ],
target,
sentence,
replacement,
html,
replacement,
},
},
];
Expand Down

0 comments on commit 8a9aebb

Please sign in to comment.