From 2dc2fcc8361f3fec81093ac2febc7fce047515f5 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 22 Jun 2023 10:51:29 +0800 Subject: [PATCH] fix: failed to fetch highlight when quote is null --- src/api.ts | 6 +++--- src/settings/template.ts | 4 ++-- src/util.ts | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/api.ts b/src/api.ts index 947400f..0067f2a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -58,9 +58,9 @@ export enum HighlightType { export interface Highlight { id: string; - quote: string; - annotation: string; - patch: string; + quote: string | null; + annotation: string | null; + patch: string | null; updatedAt: string; labels?: Label[]; type: HighlightType; diff --git a/src/settings/template.ts b/src/settings/template.ts index bde61f4..7124a0f 100644 --- a/src/settings/template.ts +++ b/src/settings/template.ts @@ -193,7 +193,7 @@ export const renderArticleContnet = async ( highlightUrl: `https://omnivore.app/me/${article.slug}#${highlight.id}`, highlightID: highlight.id.slice(0, 8), dateHighlighted: formatDate(highlight.updatedAt, dateHighlightedFormat), - note: highlight.annotation, + note: highlight.annotation ?? undefined, labels: renderLabels(highlight.labels), }; }); @@ -228,7 +228,7 @@ export const renderArticleContnet = async ( datePublished, fileAttachment, description: article.description, - note: articleNote?.annotation, + note: articleNote?.annotation ?? undefined, type: article.pageType, dateRead, wordsCount, diff --git a/src/util.ts b/src/util.ts index a5a2ff2..6ce8e2b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -18,13 +18,19 @@ export interface HighlightPoint { top: number; } -export const getHighlightLocation = (patch: string): number => { +export const getHighlightLocation = (patch: string | null): number => { + if (!patch) { + return 0; + } const dmp = new diff_match_patch(); const patches = dmp.patch_fromText(patch); return patches[0].start1 || 0; }; -export const getHighlightPoint = (patch: string): HighlightPoint => { +export const getHighlightPoint = (patch: string | null): HighlightPoint => { + if (!patch) { + return { left: 0, top: 0 }; + } const { bbox } = JSON.parse(patch) as { bbox: number[] }; if (!bbox || bbox.length !== 4) { return { left: 0, top: 0 }; @@ -129,9 +135,12 @@ export const siteNameFromUrl = (originalArticleUrl: string): string => { }; export const formatHighlightQuote = ( - quote: string, + quote: string | null, template: string ): string => { + if (!quote) { + return ""; + } // if the template has highlights, we need to preserve paragraphs const regex = /{{#highlights}}(\n)*>/gm; if (regex.test(template)) {