Skip to content

Commit

Permalink
fix: use omnivore title as the filename and add omnivore id to the fr…
Browse files Browse the repository at this point in the history
…ontmatter which is used to deduplicate files with the same name
  • Loading branch information
sywhb committed Feb 22, 2023
1 parent de94fba commit 8fbf85d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 78 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Plugin,
PluginSettingTab,
Setting,
stringifyYaml,
TFile,
TFolder,
} from "obsidian";
Expand All @@ -19,7 +20,7 @@ import {
loadArticles,
PageType,
parseDateTime,
unicodeSlug,
replaceIllegalChars,
} from "./util";
import { FolderSuggest } from "./settings/file-suggest";

Expand Down Expand Up @@ -57,6 +58,7 @@ const DEFAULT_SETTINGS: Settings = {
syncAt: "",
customQuery: "",
template: `---
id: {{{id}}}
title: {{{title}}}
{{#author}}
author: {{{author}}}
Expand Down Expand Up @@ -179,7 +181,7 @@ export default class OmnivorePlugin extends Plugin {
await this.saveSettings();

try {
console.log(`obsidian-omnivore starting sync since: '${syncAt}`);
console.log(`obsidian-omnivore starting sync since: '${syncAt}'`);

new Notice("🚀 Fetching articles ...");

Expand Down Expand Up @@ -215,11 +217,6 @@ export default class OmnivorePlugin extends Plugin {
await this.app.vault.createFolder(folderName);
}

// use unicode slug to show characters from other languages in the file name
const pageName = `${folderName}/${unicodeSlug(
article.title,
article.savedAt
)}.md`;
const siteName =
article.siteName ||
this.siteNameFromUrl(article.originalArticleUrl);
Expand Down Expand Up @@ -255,6 +252,7 @@ export default class OmnivorePlugin extends Plugin {

// Build content string based on template
const content = Mustache.render(template, {
id: article.id,
title: article.title,
omnivoreUrl: `https://omnivore.app/me/${article.slug}`,
siteName,
Expand All @@ -270,15 +268,82 @@ export default class OmnivorePlugin extends Plugin {
content: article.content,
});

// 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,
};
// remove null and empty values from frontmatter
const filteredFrontmatter = Object.fromEntries(
Object.entries(frontmatter).filter(
([_, value]) => value != null && value !== ""
)
);
const frontmatterYaml = stringifyYaml(filteredFrontmatter);
const frontmatterString = `---\n${frontmatterYaml}---`;
// Modify the contents of the note with the updated front matter
const updatedContent = content.replace(
/^(---[\s\S]*?---)/gm,
frontmatterString
);
// use the title as the filename
const pageName = `${folderName}/${replaceIllegalChars(
article.title
)}.md`;
const normalizedPath = normalizePath(pageName);
const omnivoreFile = app.vault.getAbstractFileByPath(normalizedPath);
if (omnivoreFile instanceof TFile) {
const existingContent = await this.app.vault.read(omnivoreFile);
if (existingContent !== content) {
await this.app.vault.modify(omnivoreFile, content);
try {
if (omnivoreFile instanceof TFile) {
await app.fileManager.processFrontMatter(
omnivoreFile,
async (frontMatter) => {
const id = frontMatter.id;
if (id && id !== article.id) {
// this article has the same name but different id
const newPageName = `${folderName}/${replaceIllegalChars(
article.title
)}-${article.id}.md`;
const newNormalizedPath = normalizePath(newPageName);
const newOmnivoreFile =
app.vault.getAbstractFileByPath(newNormalizedPath);
if (newOmnivoreFile instanceof TFile) {
// a file with the same name and id already exists, so we need to update it
const existingContent = await this.app.vault.read(
newOmnivoreFile
);
if (existingContent !== updatedContent) {
await this.app.vault.modify(
newOmnivoreFile,
updatedContent
);
}
return;
}
// a file with the same name but different id already exists, so we need to create it
await this.app.vault.create(
newNormalizedPath,
updatedContent
);
return;
}
// a file with the same id already exists, so we might need to update it
const existingContent = await this.app.vault.read(
omnivoreFile
);
if (existingContent !== updatedContent) {
await this.app.vault.modify(omnivoreFile, updatedContent);
}
}
);
} else if (!omnivoreFile) {
// file doesn't exist, so we need to create it
await this.app.vault.create(normalizedPath, updatedContent);
}
} else if (!omnivoreFile) {
await this.app.vault.create(normalizedPath, content);
} catch (e) {
console.error(e);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export enum PageType {
}

export interface Article {
id: string;
title: string;
siteName: string;
originalArticleUrl: string;
Expand Down Expand Up @@ -119,7 +120,7 @@ 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 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":"${
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}"}}`,
method: "POST",
Expand Down Expand Up @@ -227,3 +228,7 @@ export const unicodeSlug = (str: string, savedAt: string) => {
new Date(savedAt).getTime().toString(16)
);
};

export const replaceIllegalChars = (str: string): string => {
return str.replace(/[/\\?%*:|"<>]/g, "-");
};

0 comments on commit 8fbf85d

Please sign in to comment.