Add an option for the original html to be parsed by distiller

This commit is contained in:
Hongbo Wu 2023-02-09 17:44:02 +08:00
parent 5903160eb7
commit a91cf9ef63
2 changed files with 40 additions and 2 deletions

View file

@ -68,6 +68,7 @@ import {
validatedDate,
} from '../../utils/helpers'
import {
getDistillerResult,
htmlToMarkdown,
ParsedContentPuppeteer,
parsePreparedContent,
@ -106,6 +107,12 @@ import { saveSearchHistory } from '../../services/search_history'
import { parsedContentToPage } from '../../services/save_page'
import * as httpContext from 'express-http-context'
enum ArticleFormat {
Markdown = 'markdown',
Html = 'html',
Distiller = 'distiller',
}
export type PartialArticle = Omit<
Article,
| 'updatedAt'
@ -413,7 +420,9 @@ export const getArticleResolver: ResolverFn<
return { errorCodes: [ArticleErrorCode.Unauthorized] }
}
const includeOriginalHtml = !!graphqlFields(info).article.originalHtml
const includeOriginalHtml =
format === ArticleFormat.Distiller ||
!!graphqlFields(info).article.originalHtml
analytics.track({
userId: claims?.uid,
@ -449,8 +458,17 @@ export const getArticleResolver: ResolverFn<
page.content = UNPARSEABLE_CONTENT
}
if (format === 'markdown') {
if (format === ArticleFormat.Markdown) {
page.content = htmlToMarkdown(page.content)
} else if (format === ArticleFormat.Distiller) {
if (!page.originalHtml) {
return { errorCodes: [ArticleErrorCode.BadData] }
}
const distillerResult = await getDistillerResult(page.originalHtml)
if (!distillerResult) {
return { errorCodes: [ArticleErrorCode.BadData] }
}
page.content = distillerResult
}
return {

View file

@ -485,3 +485,23 @@ const nhm = new NodeHtmlMarkdown(
export const htmlToMarkdown = (html: string) => {
return nhm.translate(/* html */ html)
}
export const getDistillerResult = async (
html: string
): Promise<string | undefined> => {
try {
const url = process.env.DISTILLER_URL
if (!url) {
console.log('No distiller url')
return undefined
}
const response = await axios.post<string>(url, html, {
timeout: 5000,
})
return response.data
} catch (e) {
console.log('Error parsing by distiller', e)
return undefined
}
}