Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wikilink support for annotation target #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/annotatorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ANNOTATION_TARGET_PROPERTY, ANNOTATION_TARGET_TYPE_PROPERTY, VIEW_TYPE_
import { DarkReaderType } from 'darkreader';
import AnnotatorPlugin from 'main';
import { Annotation } from './types';
import { FileView, Menu, MenuItem, TFile, WorkspaceLeaf } from 'obsidian';
import { FileView, Menu, MenuItem, TFile, WorkspaceLeaf, getLinkPath } from 'obsidian';
import React from 'react';
import ReactDOM from 'react-dom';
import { isUrl, get_url_extension } from 'utils';
import { isUrl, get_url_extension, getWikilink } from 'utils';

export default class AnnotatorView extends FileView {
plugin: AnnotatorPlugin;
Expand Down Expand Up @@ -45,6 +45,10 @@ export default class AnnotatorView extends FileView {
}
let destFile: TFile;
try {
const wikilink = getWikilink(target);
if (wikilink) {
target = getLinkPath(target);
}
destFile = this.app.metadataCache.getFirstLinkpathDest(target, file?.path || '');
} finally {
if (destFile) {
Expand Down
15 changes: 15 additions & 0 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ export function isUrl(potentialUrl: string) {
}
}

/**
* If the given string is a wikilink, return the link content, otherwise return null
* Ex: "[[Some File]]", returns "Some File"
*/
export function getWikilink(potentialWikilink: string): string | null {
if (!potentialWikilink) {
return null;
}
const match = potentialWikilink.match(/^\[\[(.*)\]\]$/);
if (!match) {
return null;
}
return match[1];
}

export function utf8_to_b64(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
Expand Down