Skip to content

Commit

Permalink
0.2.0 (#12)
Browse files Browse the repository at this point in the history
- Added right-click menu item to add an alias to a redirect file.
- Added notice in case of lack of redirect file.
  • Loading branch information
jglev authored Sep 4, 2022
1 parent ba3d741 commit d0e0dca
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 17 deletions.
175 changes: 162 additions & 13 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import {
FileSystemAdapter,
FuzzyMatch,
FuzzySuggestModal,
Keymap,
KeymapEventHandler,
KeymapInfo,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
TAbstractFile,
TFile,
} from "obsidian";
import * as yaml from "js-yaml";
import yamlFront from "front-matter";

type SuggestionObject = {
alias: string;
Expand Down Expand Up @@ -72,6 +71,87 @@ const DEFAULT_SETTINGS: RedirectPluginSettings = {
apiVersion: 2,
};

export class AliasPromptModal extends Modal {
newAlias: string;
file: TFile;
enterKeyHandler: KeymapEventHandler;

constructor(app: App, file: TFile) {
super(app);
this.file = file;
this.enterKeyHandler = this.scope.register(
[],
"Enter",
(evt: KeyboardEvent) => {
this.submitAlias();
return false;
}
);
}

async submitAlias() {
const fileParsed = yamlFront(
await app.vault.adapter.read(this.file.path)
);
const attributes: Record<string, any> = fileParsed.attributes;

const frontMatterAliases = [
...(attributes?.alias ? [attributes.alias] : []),
...(attributes?.aliases
? Array.isArray(attributes.aliases)
? attributes.aliases
: [attributes.aliases]
: []),
this.newAlias,
];

const newFrontMatter: Record<string, any> = fileParsed.attributes;

if (Object.keys(newFrontMatter).includes("alias")) {
delete newFrontMatter.alias;
}
if (Object.keys(newFrontMatter).includes("aliases")) {
delete newFrontMatter.aliases;
}

const newContent = `---\n${yaml.dump({
...newFrontMatter,
aliases: frontMatterAliases,
})}---\n\n${fileParsed.body}`;

app.vault.adapter.write(this.file.path, newContent);

this.scope.unregister(this.enterKeyHandler);
this.close();
}

onOpen() {
const { contentEl } = this;

// contentEl.createEl("h1", { text: "New alias" });

new Setting(contentEl).setName("New alias").addText((text) =>
text.onChange((value) => {
this.newAlias = value;
})
);

new Setting(contentEl).addButton((btn) =>
btn
.setButtonText("Submit")
.setCta()
.onClick(async () => {
this.submitAlias();
})
);
}

onClose() {
let { contentEl } = this;
contentEl.empty();
}
}

const getRedirectFiles = (
plugin: RedirectPlugin,
files: TFile[],
Expand Down Expand Up @@ -229,11 +309,17 @@ interface FileWithPath extends File {
path: string;
}

enum HandleFilesWithModalAction {
OpenFile,
AddAliasToFile,
}

const handleFilesWithModal = (
plugin: RedirectPlugin,
app: App,
files: FileWithPath[] | TFile[],
ctrlKey: boolean
ctrlKey: boolean,
action: HandleFilesWithModalAction
) => {
const redirectFiles = getRedirectFiles(
plugin,
Expand Down Expand Up @@ -264,14 +350,31 @@ const handleFilesWithModal = (
...new Set(relevantRedirectFiles.map((f) => f.originTFile.path)),
];

if (relevantRedirectFilesChunked.length === 0) {
new Notice(`No file redirects to the selected file.`);

return;
}

if (
[...files].length === 1 &&
relevantRedirectFilesChunked.length === 1
) {
plugin.app.workspace
.getLeaf(ctrlKey)
.openFile(relevantRedirectFiles[0].originTFile);
return;
if (action === HandleFilesWithModalAction.OpenFile) {
plugin.app.workspace
.getLeaf(ctrlKey)
.openFile(relevantRedirectFiles[0].originTFile);
return;
}

if (action === HandleFilesWithModalAction.AddAliasToFile) {
const newAliasModal = new AliasPromptModal(
plugin.app,
relevantRedirectFiles[0].originTFile
);
newAliasModal.open();
return;
}
}

if (relevantRedirectFilesChunked.length >= 1) {
Expand All @@ -282,9 +385,21 @@ const handleFilesWithModal = (
file: SuggestionObject,
newPane: boolean
): void => {
plugin.app.workspace
.getLeaf(newPane)
.openFile(file.originTFile);
if (action === HandleFilesWithModalAction.OpenFile) {
plugin.app.workspace
.getLeaf(newPane)
.openFile(file.originTFile);
return;
}

if (action === HandleFilesWithModalAction.AddAliasToFile) {
const newAliasModal = new AliasPromptModal(
plugin.app,
file.originTFile
);
newAliasModal.open();
return;
}
},
limitToNonMarkdown: plugin.settings.limitToNonMarkdown,
files: relevantRedirectFiles,
Expand Down Expand Up @@ -334,7 +449,13 @@ export default class RedirectPlugin extends Plugin {
(f: FileWithPath) => f.path.startsWith(basePath)
);

handleFilesWithModal(this, app, files, evt.ctrlKey);
handleFilesWithModal(
this,
app,
files,
evt.ctrlKey,
HandleFilesWithModalAction.OpenFile
);
}
);

Expand Down Expand Up @@ -500,6 +621,19 @@ export default class RedirectPlugin extends Plugin {
(this.settings.limitToNonMarkdown &&
file.extension !== "md"))
) {
let redirectFiles = getRedirectFiles(
this,
app.vault.getFiles(),
true
).filter((a: SuggestionObject) => a.redirectTFile === file);

// Do not show the menu items below if there are
// no relevant redirect files:
if (!redirectFiles.length) {
return false;
}


menu.addItem((item) => {
item.setTitle("Open redirect origin file")
.setIcon("right-arrow-with-tail")
Expand All @@ -508,7 +642,22 @@ export default class RedirectPlugin extends Plugin {
this,
app,
[file],
e.ctrlKey
e.ctrlKey,
HandleFilesWithModalAction.OpenFile
);
});
});

menu.addItem((item) => {
item.setTitle("Add alias to redirect origin file")
.setIcon("plus-with-circle")
.onClick((e: MouseEvent) => {
handleFilesWithModal(
this,
app,
[file],
e.ctrlKey,
HandleFilesWithModalAction.AddAliasToFile
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-redirect",
"name": "Redirect",
"version": "0.1.9",
"version": "0.2.0",
"minAppVersion": "0.14.15",
"description": "An Obsidian (https://obsidian.md) plugin for redirecting links based on YAML frontmatter.",
"author": "Jacob Levernier",
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-redirect",
"version": "0.1.9",
"version": "0.2.0",
"description": "An Obsidian (https://obsidian.md) plugin for redirecting links based on YAML frontmatter.",
"main": "main.js",
"scripts": {
Expand All @@ -25,5 +25,9 @@
"tslib": "2.3.1",
"typescript": "4.4.4"
},
"dependencies": {}
"dependencies": {
"@types/js-yaml": "^4.0.5",
"front-matter": "^4.0.2",
"js-yaml": "^4.1.0"
}
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"0.1.6": "0.14.15",
"0.1.7": "0.14.15",
"0.1.8": "0.14.15",
"0.1.9": "0.14.15"
"0.1.9": "0.14.15",
"0.2.0": "0.14.15"
}
49 changes: 49 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.51.tgz#cfd70924a25a3fd32b218e5e420e6897e1ac4f40"
integrity sha512-CuPgU6f3eT/XgKKPqKd/gLZV1Xmvf1a2R5POBOGQa6uv82xpls89HU5zKeVoyR8XzHd1RGNOlQlvUe3CFkjWNQ==

"@types/js-yaml@^4.0.5":
version "4.0.5"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.5.tgz#738dd390a6ecc5442f35e7f03fa1431353f7e138"
integrity sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==

"@types/json-schema@^7.0.9":
version "7.0.11"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
Expand Down Expand Up @@ -132,6 +137,18 @@
"@typescript-eslint/types" "5.28.0"
eslint-visitor-keys "^3.3.0"

argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
integrity sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==
dependencies:
sprintf-js "~1.0.2"

argparse@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

array-union@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d"
Expand Down Expand Up @@ -296,6 +313,11 @@ eslint-visitor-keys@^3.3.0:
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-3.3.0.tgz#f6480fa6b1f30efe2d1968aa8ac745b862469826"
integrity sha512-mQ+suqKJVyeuwGYHAdjMFqjCyfl8+Ldnxuyp3ldiMBFKkvytrXUZWaiPCEav8qDHKty44bD+qV1IP4T+w+xXRA==

esprima@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==

esrecurse@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/esrecurse/-/esrecurse-4.3.0.tgz#7ad7964d679abb28bee72cec63758b1c5d2c9921"
Expand Down Expand Up @@ -338,6 +360,13 @@ fill-range@^7.0.1:
dependencies:
to-regex-range "^5.0.1"

front-matter@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/front-matter/-/front-matter-4.0.2.tgz#b14e54dc745cfd7293484f3210d15ea4edd7f4d5"
integrity sha512-I8ZuJ/qG92NWX8i5x1Y8qyj3vizhXS31OxjKDu3LKP+7/qBgfIKValiZIEwoVoJKUHlhWtYrktkxV1XsX+pPlg==
dependencies:
js-yaml "^3.13.1"

functional-red-black-tree@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
Expand Down Expand Up @@ -384,6 +413,21 @@ is-number@^7.0.0:
resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b"
integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==

js-yaml@^3.13.1:
version "3.14.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.1.tgz#dae812fdb3825fa306609a8717383c50c36a0537"
integrity sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==
dependencies:
argparse "^2.0.1"

lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
Expand Down Expand Up @@ -466,6 +510,11 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==

sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==

to-regex-range@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4"
Expand Down

0 comments on commit d0e0dca

Please sign in to comment.