Skip to content

Commit

Permalink
fix: generate unicode slug as file name
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed Jan 16, 2023
1 parent dffa4c3 commit 2e5fcd6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
loadArticles,
PageType,
parseDateTime,
unicodeSlug,
} from "./util";
import { FolderSuggest } from "./settings/file-suggest";

Expand Down Expand Up @@ -100,15 +101,15 @@ export default class OmnivorePlugin extends Plugin {
await this.loadSettings();

this.addCommand({
id: "obsidian-omnivore-sync",
id: "omnivore-sync",
name: "Sync",
callback: () => {
this.fetchOmnivore();
},
});

this.addCommand({
id: "obsidian-omnivore-resync",
id: "omnivore-resync",
name: "Resync all articles",
callback: () => {
this.settings.syncAt = "";
Expand Down Expand Up @@ -188,7 +189,11 @@ export default class OmnivorePlugin extends Plugin {
await this.app.vault.createFolder(folderName);
}

const pageName = `${folderName}/${article.slug}.md`;
// 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
22 changes: 22 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,25 @@ export const parseDateTime = (str: string): DateTime => {
export const wrapAround = (value: number, size: number): number => {
return ((value % size) + size) % size;
};

export const unicodeSlug = (str: string, savedAt: string) => {
return (
str
.normalize("NFKD") // using NFKD method returns the Unicode Normalization Form of a given string.
.replace(/[\u0300-\u036f]/g, "") // remove all previously split accents
.trim()
.toLowerCase()
.replace(
/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g,
""
) // replace all the symbols with -
.replace(/\s+/g, "-") // collapse whitespace and replace by -
.replace(/_/g, "-") // replace _ with -
.replace(/-+/g, "-") // collapse dashes
// remove trailing -
.replace(/-$/g, "")
.substring(0, 64) +
"-" +
new Date(savedAt).getTime().toString(16)
);
};

0 comments on commit 2e5fcd6

Please sign in to comment.