From e9b31e375f3987c1f50b8aae60ccd8597271437e Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 27 Dec 2022 21:59:31 +0800 Subject: [PATCH] Update savePage API to accept parsed content in the param --- packages/api/src/generated/graphql.ts | 18 ++++++++++++++++++ packages/api/src/generated/schema.graphql | 16 ++++++++++++++++ packages/api/src/schema.ts | 16 ++++++++++++++++ packages/api/src/services/save_page.ts | 16 ++++++++++------ packages/api/src/utils/parser.ts | 15 ++++++++++++--- 5 files changed, 72 insertions(+), 9 deletions(-) diff --git a/packages/api/src/generated/graphql.ts b/packages/api/src/generated/graphql.ts index 07b53580a..0c26798f8 100644 --- a/packages/api/src/generated/graphql.ts +++ b/packages/api/src/generated/graphql.ts @@ -1612,6 +1612,21 @@ export enum PageType { Website = 'WEBSITE' } +export type ParseResult = { + byline: Scalars['String']; + content: Scalars['String']; + dir: Scalars['String']; + excerpt: Scalars['String']; + language?: InputMaybe; + length: Scalars['Int']; + previewImage?: InputMaybe; + publishedDate?: InputMaybe; + siteIcon?: InputMaybe; + siteName?: InputMaybe; + textContent: Scalars['String']; + title: Scalars['String']; +}; + export type PreparedDocumentInput = { document: Scalars['String']; pageInfo: PageInfoInput; @@ -2071,6 +2086,7 @@ export type SaveFilterSuccess = { export type SavePageInput = { clientRequestId: Scalars['ID']; originalContent: Scalars['String']; + parseResult?: InputMaybe; source: Scalars['String']; title?: InputMaybe; url: Scalars['String']; @@ -3291,6 +3307,7 @@ export type ResolversTypes = { PageInfo: ResolverTypeWrapper; PageInfoInput: PageInfoInput; PageType: PageType; + ParseResult: ParseResult; PreparedDocumentInput: PreparedDocumentInput; Profile: ResolverTypeWrapper; Query: ResolverTypeWrapper<{}>; @@ -3704,6 +3721,7 @@ export type ResolversParentTypes = { Page: Page; PageInfo: PageInfo; PageInfoInput: PageInfoInput; + ParseResult: ParseResult; PreparedDocumentInput: PreparedDocumentInput; Profile: Profile; Query: {}; diff --git a/packages/api/src/generated/schema.graphql b/packages/api/src/generated/schema.graphql index 6036c33fc..7d4df8a16 100644 --- a/packages/api/src/generated/schema.graphql +++ b/packages/api/src/generated/schema.graphql @@ -1169,6 +1169,21 @@ enum PageType { WEBSITE } +input ParseResult { + byline: String! + content: String! + dir: String! + excerpt: String! + language: String + length: Int! + previewImage: String + publishedDate: Date + siteIcon: String + siteName: String + textContent: String! + title: String! +} + input PreparedDocumentInput { document: String! pageInfo: PageInfoInput! @@ -1501,6 +1516,7 @@ type SaveFilterSuccess { input SavePageInput { clientRequestId: ID! originalContent: String! + parseResult: ParseResult source: String! title: String url: String! diff --git a/packages/api/src/schema.ts b/packages/api/src/schema.ts index 081d4b60f..9a17cf019 100755 --- a/packages/api/src/schema.ts +++ b/packages/api/src/schema.ts @@ -526,12 +526,28 @@ const schema = gql` uploadFileId: ID! } + input ParseResult { + title: String! + byline: String! + dir: String! + content: String! + textContent: String! + length: Int! + excerpt: String! + siteName: String + siteIcon: String + previewImage: String + publishedDate: Date + language: String + } + input SavePageInput { url: String! source: String! clientRequestId: ID! title: String originalContent: String! + parseResult: ParseResult } input SaveUrlInput { diff --git a/packages/api/src/services/save_page.ts b/packages/api/src/services/save_page.ts index a7997969a..de1d18168 100644 --- a/packages/api/src/services/save_page.ts +++ b/packages/api/src/services/save_page.ts @@ -77,13 +77,17 @@ export const savePage = async ( input: SavePageInput ): Promise => { const [slug, croppedPathname] = createSlug(input.url, input.title) - const parseResult = await parsePreparedContent(input.url, { - document: input.originalContent, - pageInfo: { - title: input.title, - canonicalUrl: input.url, + const parseResult = await parsePreparedContent( + input.url, + { + document: input.originalContent, + pageInfo: { + title: input.title, + canonicalUrl: input.url, + }, }, - }) + input.parseResult + ) const articleToSave = parsedContentToPage({ url: input.url, diff --git a/packages/api/src/utils/parser.ts b/packages/api/src/utils/parser.ts index e19681821..243513667 100644 --- a/packages/api/src/utils/parser.ts +++ b/packages/api/src/utils/parser.ts @@ -17,8 +17,8 @@ import { v4 as uuid } from 'uuid' import addressparser from 'addressparser' import { preParseContent } from '@omnivore/content-handler' import { - findEmbeddedHighlight, EmbeddedHighlightData, + findEmbeddedHighlight, } from './highlightGenerator' const logger = buildLogger('utils.parse') @@ -174,6 +174,7 @@ const getReadabilityResult = async ( export const parsePreparedContent = async ( url: string, preparedDocument: PreparedDocumentInput, + parseResult?: Readability.ParseResult | null, isNewsletter?: boolean, allowRetry = true ): Promise => { @@ -208,13 +209,21 @@ export const parsePreparedContent = async ( preParsedDom && (dom = preParsedDom) try { - article = await getReadabilityResult(url, document, dom, isNewsletter) + article = + parseResult || + (await getReadabilityResult(url, document, dom, isNewsletter)) if (!article?.textContent && allowRetry) { const newDocument = { ...preparedDocument, document: '' + preparedDocument.document + '', } - return parsePreparedContent(url, newDocument, isNewsletter, false) + return parsePreparedContent( + url, + newDocument, + parseResult, + isNewsletter, + false + ) } // Format code blocks