Skip to content

Commit

Permalink
feat: allow links in the todo description
Browse files Browse the repository at this point in the history
Links to other pages within the Obsidian vault can be created by
enclosing in double square brackets for example: [[Inbox]]

Closes #9
  • Loading branch information
mvgrimes committed Jan 3, 2024
1 parent 0872b4f commit 8b7668f
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "todotxt",
"name": "TodoTxt",
"version": "1.4.4",
"version": "1.5.0",
"minAppVersion": "0.15.0",
"description": "Manage Todo.txt files.",
"author": "Mark Grimes",
Expand Down
5 changes: 3 additions & 2 deletions dist/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"1.3.3": "0.15.0",
"1.4.0": "0.15.0",
"1.4.3": "0.15.0",
"1.4.4": "0.15.0"
}
"1.4.4": "0.15.0",
"1.5.0": "0.15.0"
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "todotxt",
"name": "TodoTxt",
"version": "1.4.4",
"version": "1.5.0",
"minAppVersion": "0.15.0",
"description": "Manage Todo.txt files.",
"author": "Mark Grimes",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-sample-plugin",
"version": "1.4.4",
"version": "1.5.0",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "dist/main.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/ui/todoslist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type TodosListProps = {
onCompleteToggle: (t: Todo) => void;
onDeleteClicked: (t: Todo) => void;
onEditClicked: (t: Todo) => void;
onNavigate: (url: string, newTab: boolean) => void;
};

export const TodosList = (props: TodosListProps) => {
Expand All @@ -34,6 +35,7 @@ export const TodosList = (props: TodosListProps) => {
onDeleteClicked={props.onDeleteClicked}
onEditClicked={props.onEditClicked}
onKeyPressed={handleKeyPress}
onNavigate={props.onNavigate}
key={todo.id}
/>
))}
Expand Down
2 changes: 2 additions & 0 deletions src/ui/todosview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type TodosViewProps = {
defaultOrganizeBy: 'project' | 'context';
preservePriority: boolean;
recurringTasks: boolean;
onNavigate: (url: string, newTab: boolean) => void;
};
type OrganizeBy = 'project' | 'context';

Expand Down Expand Up @@ -176,6 +177,7 @@ export const TodosView = (props: TodosViewProps) => {
onCompleteToggle={handleCompleteToggle}
onDeleteClicked={handleShowDelete}
onEditClicked={handleShowEdit}
onNavigate={props.onNavigate}
/>
</section>
))}
Expand Down
39 changes: 38 additions & 1 deletion src/ui/todoview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type TodoViewProps = {
onDeleteClicked: (t: Todo) => void;
onEditClicked: (t: Todo) => void;
onKeyPressed: (e: React.KeyboardEvent<HTMLInputElement>, t: Todo) => void;
onNavigate: (url: string, newTab: boolean) => void;
};
export const TodoView = (props: TodoViewProps) => {
const { todo } = props;
Expand Down Expand Up @@ -43,7 +44,10 @@ export const TodoView = (props: TodoViewProps) => {
todo.preThreshold() ? 'todo-prethreshold' : '',
)}
>
{todo.description}
<TodoDescription
description={todo.description}
onNavigate={props.onNavigate}
/>
</span>
{todo.tags.map((tag, i) => (
<TodoTagView tag={tag} key={i} />
Expand Down Expand Up @@ -106,3 +110,36 @@ const TodoTagView = ({ tag }: { tag: TodoTag }) => {
</span>
);
};

const DESC_RE = /(\[\[[^\]]+\]\])/g;

const TodoDescription = ({
description,
onNavigate,
}: {
description: string;
onNavigate: (url: string, newTab: boolean) => void;
}) => {
const parts = description.split(DESC_RE);
console.log(description, parts);

return (
<span>
{parts.map((part) => {
const url = part.match(/^\[\[(.*)\]\]$/);
if (url)
return (
<a
onClick={(e) => {
e.preventDefault();
onNavigate(url[1], true);
}}
>
{url[1]}
</a>
);
return <>{part}</>;
})}
</span>
);
};
6 changes: 6 additions & 0 deletions src/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export class TodotxtView extends TextFileView {
this.requestSave();
}

handleNavigate(url: string, newTab: boolean) {
const callingFilePath = ''; // TODO: this must be available in `this`
this.plugin.app.workspace.openLinkText(url, callingFilePath, newTab);
}

refresh() {
const settings = this.plugin?.settings;

Expand All @@ -70,6 +75,7 @@ export class TodotxtView extends TextFileView {
defaultOrganizeBy={settings?.defaultOrganizeBy || 'project'}
preservePriority={settings?.preservePriority ?? true}
recurringTasks={settings?.recurringTasks ?? false}
onNavigate={this.handleNavigate.bind(this)}
/>,
);
}
Expand Down
5 changes: 3 additions & 2 deletions versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"1.3.3": "0.15.0",
"1.4.0": "0.15.0",
"1.4.3": "0.15.0",
"1.4.4": "0.15.0"
}
"1.4.4": "0.15.0",
"1.5.0": "0.15.0"
}

0 comments on commit 8b7668f

Please sign in to comment.