Skip to content

Commit

Permalink
Add cwd detection for Templater user scripts
Browse files Browse the repository at this point in the history
Signed-off-by: William So <polyipseity@gmail.com>
  • Loading branch information
polyipseity committed Sep 12, 2023
1 parent a211b5c commit ef032bf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-news-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"obsidian-modules": minor
---

Add cwd detection for Templater user scripts.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Contributions are welcome!
The todos here, ordered alphabetically, are things planned for the plugin. There are no guarantees that they will be completed. However, we are likely to accept contributions for them.

- Add startup modules.
- Add context detection for Templater user scripts (not templates), `dv.view`, and canvases.
- Add context detection for `dv.view` and canvases.
- User-defined module aliases.
- Add bare module transformation support for more CDNs such as <https://cdn.jsdelivr.net>.
- Faster import analysis and transformation.
Expand Down
21 changes: 21 additions & 0 deletions sources/@types/templater-obsidian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ declare module "templater-obsidian" {
// eslint-disable-next-line @typescript-eslint/naming-convention
readonly template_file: TFile | undefined
}
// https://github.com/SilentVoid13/Templater/blob/487805b5ad1fd7fbc145040ed82b4c41fc2c48e2/src/core/functions/FunctionsGenerator.ts#L13
interface FunctionsGenerator {
// eslint-disable-next-line @typescript-eslint/naming-convention
readonly user_functions: UserFunctions
}
// https://silentvoid13.github.io/Templater/internal-functions/overview.html
interface FunctionsObject extends Record<string, unknown> {
readonly config: ConfigModule
Expand All @@ -18,12 +23,28 @@ declare module "templater-obsidian" {
}
// https://github.com/SilentVoid13/Templater/blob/487805b5ad1fd7fbc145040ed82b4c41fc2c48e2/src/core/Templater.ts#L39
interface Templater {
// eslint-disable-next-line @typescript-eslint/naming-convention
readonly functions_generator: FunctionsGenerator
readonly parser: Parser
}
// https://github.com/SilentVoid13/Templater/blob/487805b5ad1fd7fbc145040ed82b4c41fc2c48e2/src/main.ts#L15
interface TemplaterPlugin extends Plugin {
readonly templater: Templater
}
// https://github.com/SilentVoid13/Templater/blob/487805b5ad1fd7fbc145040ed82b4c41fc2c48e2/src/core/functions/user_functions/UserFunctions.ts#L7
interface UserFunctions {
// eslint-disable-next-line @typescript-eslint/naming-convention
readonly user_script_functions: UserScriptFunctions
}
// https://github.com/SilentVoid13/Templater/blob/487805b5ad1fd7fbc145040ed82b4c41fc2c48e2/src/core/functions/user_functions/UserScriptFunctions.ts#L8
interface UserScriptFunctions {
// https://github.com/SilentVoid13/Templater/blob/487805b5ad1fd7fbc145040ed82b4c41fc2c48e2/src/core/functions/user_functions/UserScriptFunctions.ts#L37
// eslint-disable-next-line @typescript-eslint/naming-convention
readonly load_user_script_function: (
file: TFile,
user_script_functions: Map<string, () => unknown>,
) => PromiseLike<void>
}
}
import type { } from "templater-obsidian"
import type { Plugin, TFile } from "obsidian"
26 changes: 25 additions & 1 deletion sources/require/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export function patchContextForEditor(context: ModulesPlugin): void {

export function patchContextForTemplater(context: ModulesPlugin): void {
function patch(plugin: TemplaterPlugin): void {
const { templater: { parser } } = plugin
const { templater: {
// eslint-disable-next-line @typescript-eslint/naming-convention, camelcase
functions_generator: { user_functions: { user_script_functions } },
parser,
} } = plugin
plugin.register(around(parser, {
// eslint-disable-next-line @typescript-eslint/naming-convention, camelcase
parse_commands(next) {
Expand All @@ -81,6 +85,26 @@ export function patchContextForTemplater(context: ModulesPlugin): void {
}
},
}))
plugin.register(around(user_script_functions, {
// eslint-disable-next-line @typescript-eslint/naming-convention, camelcase
load_user_script_function(next) {
return async function fn(
// eslint-disable-next-line camelcase
this: typeof user_script_functions,
...args: Parameters<typeof next>
): Promise<Awaited<ReturnType<typeof next>>> {
const { api: { requires } } = context,
req = requires.get(self),
[file] = args
req?.context.cwds.push(file.parent?.path ?? null)
try {
await next.apply(this, args)
} finally {
req?.context.cwds.pop()
}
}
},
}))
}
revealPrivate(context, [context.app], app2 => {
const { plugins } = app2
Expand Down

0 comments on commit ef032bf

Please sign in to comment.