fix: fallback to html if failed to convert html to markdown

This commit is contained in:
Hongbo Wu 2023-05-19 16:55:50 +08:00
parent 87f55eabf6
commit 4cc9547935
2 changed files with 22 additions and 5 deletions

View file

@ -948,9 +948,13 @@ export const searchResolver = authorized<
if (params.includeContent && r.content) {
// convert html to the requested format
const format = params.format || ArticleFormat.Html
const converter = contentConverter(format)
if (converter) {
r.content = converter(r.content, r.highlights)
try {
const converter = contentConverter(format)
if (converter) {
r.content = converter(r.content, r.highlights)
}
} catch (error) {
console.log('Error converting content', error)
}
}

View file

@ -609,11 +609,24 @@ export const htmlToHighlightedMarkdown = (
html: string,
highlights?: Highlight[]
): string => {
if (!highlights) {
if (!highlights || highlights.length == 0) {
return nhm.translate(/* html */ html)
}
let document: Document
try {
document = parseHTML(html).document
if (!document || !document.documentElement) {
// the html is invalid
throw new Error('Invalid html content')
}
} catch (err) {
console.log(err)
return nhm.translate(/* html */ html)
}
const document = parseHTML(html).document
// wrap highlights in special tags
highlights
.filter((h) => h.type == 'HIGHLIGHT' && h.patch)