Skip to content

Commit

Permalink
fix: add date_published in the frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed Feb 22, 2023
1 parent 7b4f7e4 commit cf61925
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Article,
compareHighlightsInFile,
DATE_FORMAT,
formatDate,
getHighlightLocation,
loadArticles,
PageType,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -246,13 +244,18 @@ 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,
title: article.title,
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(
Expand Down
62 changes: 59 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface Article {
savedAt: string;
pageType: PageType;
content?: string;
publishedAt: string;
}

export interface Label {
Expand Down Expand Up @@ -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",
});

Expand Down Expand Up @@ -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);
};

0 comments on commit cf61925

Please sign in to comment.