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

feat: Support custom metadata extractors for tasks #2393

Open
wants to merge 3 commits 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
3 changes: 3 additions & 0 deletions src/api/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { Query } from "query/query";
import { DataviewCalendarRenderer } from "ui/views/calendar-view";
import { DataviewJSRenderer } from "ui/views/js-view";
import { markdownList, markdownTable, markdownTaskList } from "ui/export/markdown";
import { extensionRegistry } from "util/extensionRegistry";

/** Asynchronous API calls related to file / system IO. */
export class DataviewIOApi {
Expand Down Expand Up @@ -87,6 +88,8 @@ export class DataviewApi {
public widget = Widgets;
/** Re-exporting of luxon for people who can't easily require it. Sorry! */
public luxon = Luxon;
/** Programmatic extensions to tweak dataview behavior */
public extensionRegistry = extensionRegistry;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason to expose extensionRegistry instead of a direct API call addCustomSpecialTaskFieldExtractor is to have a central place for such overrides. I would anticipate more of them to be added later.

The long-term vision is for dataview to be one and only query engine for any other obsidian plugin. For instance, I would like to completely replace the built-in task querying functionality in obsidian-tasks with dataview.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another potential use-case for the extension registry is to allow for custom indexers - should resolve #1803 without the need to add the canvas support directly to the core.

Overall, the philosopy is to maintain a robust lean core and let third party extensions to take care of new extra features.

WDYT?


public constructor(
public app: App,
Expand Down
9 changes: 9 additions & 0 deletions src/data-import/inline-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EXPRESSION } from "expression/parse";
import { Literal } from "data-model/value";
import * as P from "parsimmon";
import emojiRegex from "emoji-regex";
import { extensionRegistry } from "util/extensionRegistry";

/** A parsed inline field. */
export interface InlineField {
Expand Down Expand Up @@ -211,6 +212,14 @@ function extractSpecialTaskFields(line: string): InlineField[] {
});
}

for (const customExtractor of extensionRegistry._customSpecialTaskFieldExtractors) {
try {
results.push(customExtractor.exec(line));
} catch (ex) {
console.error(`Custom extractor ${customExtractor.name} failed to extract metadata`, ex, line);
}
}

return results;
}

Expand Down
15 changes: 15 additions & 0 deletions src/util/extensionRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { InlineField } from "data-import/inline-field";

export interface CustomSpecialTaskFieldExtractor {
name: string;
exec: (line: string) => InlineField;
}
export class ExtensionRegistry {
_customSpecialTaskFieldExtractors: Array<CustomSpecialTaskFieldExtractor> = [];

addCustomSpecialTaskFieldExtractor(customExtractor: CustomSpecialTaskFieldExtractor) {
this._customSpecialTaskFieldExtractors.push(customExtractor);
}
}

export const extensionRegistry = new ExtensionRegistry();