Skip to content

Commit

Permalink
Merge pull request #88 from omnivore-app/feature/filename-variable
Browse files Browse the repository at this point in the history
feature/filename variable
  • Loading branch information
sywhb authored Jun 19, 2023
2 parents 8f46778 + 3f8fb56 commit c4ae042
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 79 deletions.
1 change: 0 additions & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ esbuild.build({
'@lezer/common',
'@lezer/highlight',
'@lezer/lr',
'out-of-character',
...builtins],
format: 'cjs',
watch: !prod,
Expand Down
43 changes: 0 additions & 43 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"luxon": "^3.1.1",
"markdown-escape": "^2.0.0",
"mustache": "^4.2.0",
"out-of-character": "^1.2.1",
"process": "^0.11.10"
},
"volta": {
Expand Down
45 changes: 31 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ import { FolderSuggest } from "./settings/file-suggest";
import {
preParseTemplate,
renderArticleContnet,
renderAttachmentFolder,
renderFilename,
renderFolderName,
} from "./settings/template";
import {
DATE_FORMAT,
findFrontMatterIndex,
formatDate,
getQueryFromFilter,
parseDateTime,
parseFrontMatterFromContent,
Expand Down Expand Up @@ -139,7 +137,7 @@ export default class OmnivorePlugin extends Plugin {
contentType: "application/pdf",
});
const folderName = normalizePath(
renderAttachmentFolder(
renderFolderName(
article,
this.settings.attachmentFolder,
this.settings.folderDateFormat
Expand Down Expand Up @@ -172,7 +170,6 @@ export default class OmnivorePlugin extends Plugin {
template,
folder,
filename,
folderDateFormat,
isSingleFile,
frontMatterVariables,
frontMatterTemplate,
Expand Down Expand Up @@ -225,12 +222,8 @@ export default class OmnivorePlugin extends Plugin {
);

for (const article of articles) {
const folderDate = formatDate(
article.savedAt,
this.settings.folderDateFormat
);
const folderName = normalizePath(
renderFolderName(folder, folderDate)
renderFolderName(article, folder, this.settings.folderDateFormat)
);
const omnivoreFolder =
this.app.vault.getAbstractFileByPath(folderName);
Expand All @@ -254,7 +247,7 @@ export default class OmnivorePlugin extends Plugin {
);
// use the custom filename
const customFilename = replaceIllegalChars(
renderFilename(article, filename, folderDateFormat)
renderFilename(article, filename, this.settings.filenameDateFormat)
);
const pageName = `${folderName}/${customFilename}.md`;
const normalizedPath = normalizePath(pageName);
Expand Down Expand Up @@ -597,7 +590,7 @@ class OmnivoreSettingTab extends PluginSettingTab {
new Setting(containerEl)
.setName("Folder")
.setDesc(
"Enter the folder where the data will be stored. {{{date}}} could be used in the folder name"
"Enter the folder where the data will be stored. {{{title}}}, {{{dateSaved}}} and {{{datePublished}}} could be used in the folder name"
)
.addSearch((search) => {
new FolderSuggest(this.app, search.inputEl);
Expand All @@ -612,7 +605,7 @@ class OmnivoreSettingTab extends PluginSettingTab {
new Setting(containerEl)
.setName("Attachment Folder")
.setDesc(
"Enter the folder where the attachment will be downloaded to. {{{date}}} could be used in the folder name"
"Enter the folder where the attachment will be downloaded to. {{{title}}}, {{{dateSaved}}} and {{{datePublished}}} could be used in the folder name"
)
.addSearch((search) => {
new FolderSuggest(this.app, search.inputEl);
Expand Down Expand Up @@ -642,7 +635,7 @@ class OmnivoreSettingTab extends PluginSettingTab {
new Setting(containerEl)
.setName("Filename")
.setDesc(
"Enter the filename where the data will be stored. {{{title}}} and {{{date}}} could be used in the filename"
"Enter the filename where the data will be stored. {{{title}}}, {{{dateSaved}}} and {{{datePublished}}} could be used in the filename"
)
.addText((text) =>
text
Expand All @@ -653,12 +646,36 @@ class OmnivoreSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Filename Date Format")
.setDesc(
createFragment((fragment) => {
fragment.append(
"Enter the format date for use in rendered filename. Format ",
fragment.createEl("a", {
text: "reference",
href: "https://moment.github.io/luxon/#/formatting?id=table-of-tokens",
})
);
})
)
.addText((text) =>
text
.setPlaceholder("yyyy-MM-dd")
.setValue(this.plugin.settings.filenameDateFormat)
.onChange(async (value) => {
this.plugin.settings.filenameDateFormat = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Folder Date Format")
.setDesc(
createFragment((fragment) => {
fragment.append(
"Enter the format date for use in rendered template. Format ",
"Enter the format date for use in rendered folder name. Format ",
fragment.createEl("a", {
text: "reference",
href: "https://moment.github.io/luxon/#/formatting?id=table-of-tokens",
Expand Down
2 changes: 2 additions & 0 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const DEFAULT_SETTINGS: OmnivoreSettings = {
folderDateFormat: "yyyy-MM-dd",
endpoint: "https://api-prod.omnivore.app/api/graphql",
filename: "{{{title}}}",
filenameDateFormat: "yyyy-MM-dd",
attachmentFolder: "Omnivore/attachments",
version: "0.0.0",
isSingleFile: false,
Expand Down Expand Up @@ -74,4 +75,5 @@ export interface OmnivoreSettings {
intervalId: number;
frontMatterVariables: string[];
frontMatterTemplate: string;
filenameDateFormat: string;
}
37 changes: 20 additions & 17 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,17 @@ const functionMap: FunctionMap = {
export const renderFilename = (
article: Article,
filename: string,
folderDateFormat: string
dateFormat: string
) => {
const date = formatDate(article.savedAt, folderDateFormat);
const date = formatDate(article.savedAt, dateFormat);
const datePublished = article.publishedAt
? formatDate(article.publishedAt, dateFormat).trim()
: undefined;
const renderedFilename = Mustache.render(filename, {
...article,
date,
dateSaved: date,
datePublished,
});

// truncate the filename to 100 characters
Expand All @@ -138,18 +143,6 @@ export const renderFilename = (
});
};

export const renderAttachmentFolder = (
article: Article,
attachmentFolder: string,
folderDateFormat: string
) => {
const date = formatDate(article.savedAt, folderDateFormat);
return Mustache.render(attachmentFolder, {
...article,
date,
});
};

export const renderLabels = (labels?: LabelView[]) => {
return labels?.map((l) => ({
// replace spaces with underscores because Obsidian doesn't allow spaces in tags
Expand Down Expand Up @@ -321,9 +314,19 @@ export const renderArticleContnet = async (
return `${frontMatterStr}\n\n${contentWithoutFrontMatter}`;
};

export const renderFolderName = (folder: string, folderDate: string) => {
return Mustache.render(folder, {
date: folderDate,
export const renderFolderName = (
article: Article,
template: string,
dateFormat: string
) => {
const date = formatDate(article.savedAt, dateFormat);
const datePublished = article.publishedAt
? formatDate(article.publishedAt, dateFormat).trim()
: undefined;
return Mustache.render(template, {
date,
dateSaved: date,
datePublished,
});
};

Expand Down
11 changes: 8 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { diff_match_patch } from "diff-match-patch";
import { DateTime } from "luxon";
import escape from "markdown-escape";
import { parseYaml } from "obsidian";
import { replace } from "out-of-character";
import { Highlight } from "./api";

export const DATE_FORMAT_W_OUT_SECONDS = "yyyy-MM-dd'T'HH:mm";
Expand Down Expand Up @@ -92,8 +91,9 @@ export const unicodeSlug = (str: string, savedAt: string) => {
};

export const replaceIllegalChars = (str: string): string => {
// remove invisible characters
return replace(str.replace(ILLEGAL_CHAR_REGEX, REPLACEMENT_CHAR));
return removeInvisibleChars(
str.replace(ILLEGAL_CHAR_REGEX, REPLACEMENT_CHAR)
);
};

export function formatDate(date: string, format: string): string {
Expand Down Expand Up @@ -167,3 +167,8 @@ export const removeFrontMatterFromContent = (content: string): string => {

export const snakeToCamelCase = (str: string) =>
str.replace(/(_[a-z])/g, (group) => group.toUpperCase().replace("_", ""));

const removeInvisibleChars = (str: string): string => {
// eslint-disable-next-line no-control-regex
return str.replace(/[^\u0000-\u007E]/g, "");
};

0 comments on commit c4ae042

Please sign in to comment.