diff --git a/src/main.ts b/src/main.ts index 7357a90..1f01bbe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,6 +16,7 @@ import { Article, compareHighlightsInFile, DATE_FORMAT, + formatDate, getHighlightLocation, loadArticles, PageType, @@ -185,12 +186,8 @@ export default class OmnivorePlugin extends Plugin { ); for (const article of articles) { - const dateSaved = DateTime.fromISO(article.savedAt).toFormat( - this.settings.dateSavedFormat - ); - const subFolderName = DateTime.fromISO(article.savedAt).toFormat( - this.settings.folderFormat - ); + const dateFormat = this.settings.dateSavedFormat; + const subFolderName = formatDate(article.savedAt, this.settings.folderFormat); const folderName = `${folder}/${subFolderName}`; const omnivoreFolder = app.vault.getAbstractFileByPath( normalizePath(folderName) @@ -226,6 +223,7 @@ export default class OmnivorePlugin extends Plugin { note: highlight.annotation, }; }); + const dateSaved = formatDate(article.savedAt, dateFormat); const siteName = article.siteName || this.siteNameFromUrl(article.originalArticleUrl); @@ -246,6 +244,10 @@ export default class OmnivorePlugin extends Plugin { highlights, content: article.content, }); + const publishedAt = article.publishedAt; + const datePublished = publishedAt + ? formatDate(publishedAt, dateFormat) + : null; // add frontmatter to the content const frontmatter = { id: article.id, @@ -253,6 +255,7 @@ export default class OmnivorePlugin extends Plugin { author: article.author, tags: article.labels?.map((l) => l.name), date_saved: dateSaved, + date_published: datePublished, }; // remove null and empty values from frontmatter const filteredFrontmatter = Object.fromEntries( diff --git a/src/util.ts b/src/util.ts index 55cb307..24d7991 100644 --- a/src/util.ts +++ b/src/util.ts @@ -66,6 +66,7 @@ export interface Article { savedAt: string; pageType: PageType; content?: string; + publishedAt: string; } export interface Label { @@ -120,9 +121,60 @@ export const loadArticles = async ( const res = await requestUrl({ url: endpoint, headers: requestHeaders(apiKey), - body: `{"query":"\\n query Search($after: String, $first: Int, $query: String, $includeContent: Boolean, $format: String) {\\n search(first: $first, after: $after, query: $query, includeContent: $includeContent, format: $format) {\\n ... on SearchSuccess {\\n edges {\\n node {\\n title\\n slug\\n siteName\\n originalArticleUrl\\n url\\n author\\n updatedAt\\n description\\n savedAt\\n pageType\\n content\\n id\\n highlights {\\n id\\n quote\\n annotation\\n patch\\n updatedAt\\n }\\n labels {\\n name\\n }\\n }\\n }\\n pageInfo {\\n hasNextPage\\n }\\n }\\n ... on SearchError {\\n errorCodes\\n }\\n }\\n }\\n ","variables":{"after":"${after}","first":${first}, "query":"${ - updatedAt ? "updated:" + updatedAt : "" - } sort:saved-asc ${query}", "includeContent": ${includeContent}, "format": "${format}"}}`, + body: JSON.stringify({ + query: ` + query Search($after: String, $first: Int, $query: String, $includeContent: Boolean, $format: String) { + search(first: $first, after: $after, query: $query, includeContent: $includeContent, format: $format) { + ... on SearchSuccess { + edges { + node { + id + title + slug + siteName + originalArticleUrl + url + author + updatedAt + description + savedAt + pageType + content + publishedAt + highlights { + id + quote + annotation + patch + updatedAt + labels { + name + } + } + labels { + name + } + } + } + pageInfo { + hasNextPage + } + } + ... on SearchError { + errorCodes + } + } + }`, + variables: { + after: `${after}`, + first, + query: `${ + updatedAt ? "updated:" + updatedAt : "" + } sort:saved-asc ${query}`, + includeContent, + format, + }, + }), method: "POST", }); @@ -232,3 +284,7 @@ export const unicodeSlug = (str: string, savedAt: string) => { export const replaceIllegalChars = (str: string): string => { return str.replace(/[/\\?%*:|"<>]/g, "-"); }; + +export const formatDate = (date: string, format: string): string => { + return DateTime.fromISO(date).toFormat(format); +};