generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
196 additions
and
5 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
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
52 changes: 52 additions & 0 deletions
52
docs/src/internal-functions/internal-modules/hooks-module.md
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,52 @@ | ||
# Hooks Module | ||
|
||
{{ tp.hooks.description }} | ||
|
||
<!-- toc --> | ||
|
||
## Documentation | ||
|
||
Function documentation is using a specific syntax. More information [here](../../syntax.md#function-documentation-syntax) | ||
|
||
|
||
{%- for key, fn in tp.hooks.functions %} | ||
### `{{ fn.definition }}` | ||
|
||
{{ fn.description }} | ||
|
||
{% if fn.args %} | ||
##### Arguments | ||
|
||
{% for arg in fn.args %} | ||
- `{{ arg.name }}`: {{ arg.description }} | ||
{% endfor %} | ||
{% endif %} | ||
|
||
{% if fn.example %} | ||
##### Example | ||
|
||
``` | ||
{{ fn.example }} | ||
``` | ||
{% endif %} | ||
{%- endfor %} | ||
|
||
## Examples | ||
|
||
```javascript | ||
// Update frontmatter after template finishes executing | ||
<%* | ||
tp.hooks.on_all_templates_executed(async () => { | ||
const file = tp.file.find_tfile(tp.file.path(true)); | ||
await app.fileManager.processFrontMatter(file, (frontmatter) => { | ||
frontmatter["key"] = "value"; | ||
}); | ||
}); | ||
%> | ||
// Run a command from another plugin that modifies the current file, after Templater has updated the file | ||
<%* | ||
tp.hooks.on_all_templates_executed(() => { | ||
app.commands.executeCommandById("obsidian-linter:lint-file"); | ||
}); | ||
-%> | ||
``` |
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
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
38 changes: 38 additions & 0 deletions
38
src/core/functions/internal_functions/hooks/InternalModuleHooks.ts
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,38 @@ | ||
import { EventRef } from "obsidian"; | ||
import { ModuleName } from "editor/TpDocumentation"; | ||
import { InternalModule } from "../InternalModule"; | ||
|
||
export class InternalModuleHooks extends InternalModule { | ||
public name: ModuleName = "hooks"; | ||
private event_refs: EventRef[] = []; | ||
|
||
async create_static_templates(): Promise<void> { | ||
this.static_functions.set( | ||
"on_all_templates_executed", | ||
this.generate_on_all_templates_executed() | ||
); | ||
} | ||
|
||
async create_dynamic_templates(): Promise<void> {} | ||
|
||
async teardown(): Promise<void> { | ||
this.event_refs.forEach((eventRef) => { | ||
eventRef.e.offref(eventRef); | ||
}); | ||
this.event_refs = []; | ||
} | ||
|
||
generate_on_all_templates_executed(): ( | ||
callback_function: () => unknown | ||
) => void { | ||
return (callback_function) => { | ||
const event_ref = app.workspace.on( | ||
"templater:all-templates-executed", | ||
() => callback_function() | ||
); | ||
if (event_ref) { | ||
this.event_refs.push(event_ref); | ||
} | ||
}; | ||
} | ||
} |
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.