Skip to content

Commit

Permalink
Merge pull request #97 from dvcrn/55-templater
Browse files Browse the repository at this point in the history
Add experimental compatibility with Templates
  • Loading branch information
dvcrn authored Jan 24, 2025
2 parents e4b47f3 + c376dd9 commit c6db233
Show file tree
Hide file tree
Showing 7 changed files with 4,572 additions and 2,328 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ jobs:
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: yarn install
run: yarn install --frozen-lockfile
${{ runner.os }}-npm-
- name: npm install
run: npm install
- name: Run test
run: yarn test
run: npm test
- name: Run Build
run: yarn run build
run: npm run build
- name: Checking format
run: yarn run format && git diff --exit-code
run: npm run format && git diff --exit-code
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

# npm
node_modules
package-lock.json

# build
main.js
Expand Down
72 changes: 66 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,80 @@ export default class FilenameHeadingSyncPlugin extends Plugin {
isRenameInProgress: boolean = false;
settings: FilenameHeadingSyncPluginSettings;

async waitForTemplater() {
const templaterEnabled = (this.app as any).plugins.enabledPlugins.has(
'templater-obsidian',
);

if (!templaterEnabled) {
return;
}

const templaterEvents = [
'templater:new-note-from-template',
'templater:template-appended',
'templater:overwrite-file',
];

return new Promise((resolve) => {
// Create one-time event handlers that clean themselves up
const handlers = templaterEvents.map((event) => {
const handler = () => {
// Remove all handlers when any event fires
handlers.forEach((h) => this.app.workspace.off(h.event, h.fn));
resolve(true);
};

return { event, fn: handler };
});

// Register all handlers
handlers.forEach((h) =>
this.app.workspace.on(h.event as any, () => {
console.log(
`[filename-heading-sync] templater event ${h.event} fired, cleaning up`,
);
h.fn();
}),
);

// Timeout fallback that also cleans up
setTimeout(() => {
handlers.forEach((h) => this.app.workspace.off(h.event, h.fn));
resolve(true);
}, 100);
});
}

async onload() {
await this.loadSettings();

this.registerEvent(
this.app.vault.on('rename', (file, oldPath) => {
if (this.settings.useFileSaveHook) {
return this.handleSyncFilenameToHeading(file, oldPath);
this.waitForTemplater().then(() => {
return this.handleSyncFilenameToHeading(file, oldPath);
});
}
}),
);

this.registerEvent(
this.app.vault.on('modify', (file) => {
if (this.settings.useFileSaveHook) {
return this.handleSyncHeadingToFile(file);
this.waitForTemplater().then(() => {
return this.handleSyncHeadingToFile(file);
});
}
}),
);

this.registerEvent(
this.app.workspace.on('file-open', (file) => {
if (this.settings.useFileOpenHook && file !== null) {
return this.handleSyncFilenameToHeading(file, file.path);
this.waitForTemplater().then(() => {
return this.handleSyncFilenameToHeading(file, file.path);
});
}
}),
);
Expand Down Expand Up @@ -166,7 +218,11 @@ export default class FilenameHeadingSyncPlugin extends Plugin {
this.forceSyncHeadingToFilename(file);
}

forceSyncHeadingToFilename(file: TFile) {
forceSyncHeadingToFilename(file: TFile | null) {
if (file === null) {
return;
}

this.app.vault.read(file).then(async (data) => {
const lines = data.split('\n');
const start = this.findNoteStart(lines);
Expand All @@ -179,7 +235,7 @@ export default class FilenameHeadingSyncPlugin extends Plugin {
sanitizedHeading.length > 0 &&
this.sanitizeHeading(file.basename) !== sanitizedHeading
) {
const newPath = `${file.parent.path}/${sanitizedHeading}.md`;
const newPath = `${file.parent?.path}/${sanitizedHeading}.md`;
this.isRenameInProgress = true;
await this.app.fileManager.renameFile(file, newPath);
this.isRenameInProgress = false;
Expand Down Expand Up @@ -228,7 +284,11 @@ export default class FilenameHeadingSyncPlugin extends Plugin {
this.forceSyncFilenameToHeading(file);
}

forceSyncFilenameToHeading(file: TFile) {
forceSyncFilenameToHeading(file: TFile | null) {
if (file === null) {
return;
}

const sanitizedHeading = this.sanitizeHeading(file.basename);
this.app.vault.read(file).then((data) => {
const lines = data.split('\n');
Expand Down
Loading

0 comments on commit c6db233

Please sign in to comment.