generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from danielo515/feat-dataview-source-multi-se…
…lect Feat-dataview-source-multi-select
- Loading branch information
Showing
13 changed files
with
166 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"plugins": [ | ||
"prettier-plugin-svelte" | ||
] | ||
], | ||
"arrowParens": "always", | ||
"editorconfig": true, | ||
"svelteAllowShorthand": true, | ||
"trailingComma": "all" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
"dependencies": { | ||
"fp-ts": "^2.16.1", | ||
"fuse.js": "^6.6.2", | ||
"type-fest": "^4.6.0", | ||
"valibot": "^0.19.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { E, Either, flow } from "@std"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { App } from "obsidian"; | ||
import { ModalFormError } from "src/utils/Error"; | ||
import { log_error } from "src/utils/Log"; | ||
|
||
type DataviewQuery = (dv: unknown, pages: unknown) => unknown; | ||
export type SafeDataviewQuery = (dv: unknown, pages: unknown) => Either<ModalFormError, string[]>; | ||
/** | ||
* From a string representing a dataview query, it returns the safest possible | ||
* function that can be used to evaluate the query. | ||
* The function is sandboxed and it will return an Either<ModalFormError, string[]>. | ||
* If you want a convenient way to execute the query, use executeSandboxedDvQuery. | ||
* @param query string representing a dataview query that will be evaluated in a sandboxed environment | ||
* @returns SafeDataviewQuery | ||
*/ | ||
export function sandboxedDvQuery(query: string): SafeDataviewQuery { | ||
if (!query.startsWith('return')) { | ||
query = 'return ' + query; | ||
} | ||
const run = new Function('dv', 'pages', query) as DataviewQuery; | ||
return flow( | ||
E.tryCatchK(run, () => new ModalFormError('Error evaluating the dataview query')), | ||
E.flatMap((result) => { | ||
if (!Array.isArray(result)) { | ||
return E.left(new ModalFormError('The dataview query did not return an array')); | ||
} | ||
return E.right(result); | ||
}) | ||
); | ||
} | ||
|
||
/** | ||
* Executes and unwraps the result of a SafeDataviewQuery. | ||
* Use this function if you want a convenient way to execute the query. | ||
* It will log the errors to the UI and return an empty array if the query fails. | ||
* @param query SafeDataviewQuery to execute | ||
* @param app the global obsidian app | ||
* @returns string[] if the query was executed successfully, otherwise an empty array | ||
*/ | ||
export function executeSandboxedDvQuery(query: SafeDataviewQuery, app: App): string[] { | ||
const dv = app.plugins.plugins.dataview?.api; | ||
|
||
if (!dv) { | ||
log_error(new ModalFormError("Dataview plugin is not enabled")) | ||
return [] as string[]; | ||
} | ||
const pages = dv.pages; | ||
return pipe( | ||
query(dv, pages), | ||
E.getOrElse((e) => { | ||
log_error(e); | ||
return [] as string[]; | ||
}) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.