41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
import { Plugin } from "obsidian";
|
|
|
|
export default class PAUL extends Plugin {
|
|
async onload() {
|
|
// Command 추가
|
|
this.addCommand({
|
|
id: 'modified-time-update',
|
|
name: 'Modified time update',
|
|
callback: () => {
|
|
this.readCurrentNote();
|
|
},
|
|
});
|
|
}
|
|
|
|
// Actual functions
|
|
readCurrentNote() {
|
|
const activeFile = this.app.workspace.getActiveFile();
|
|
// activeFile이 존재하는지 확인 && 확장자가 md인지 확인
|
|
if (activeFile && activeFile.extension === 'md') {
|
|
this.app.vault.read(activeFile).then(content => {
|
|
const pattern = /Modified: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/;
|
|
let digit = (inp)=>{ return String(inp).padStart(2, '0') }
|
|
const t = new Date()
|
|
const modified = `Modified: ${t.getFullYear()}-${digit(t.getMonth()+1)}-${digit(t.getDate())}T${digit(t.getHours())}:${digit(t.getMinutes())}:${digit(t.getSeconds())}Z`;
|
|
|
|
// Modified 라는 property의 존재여부 확인
|
|
if (content.search(pattern) != -1) {
|
|
this.app.vault.modify(activeFile, content.replace(pattern, modified));
|
|
} else {
|
|
// console.log(content);
|
|
// console.log('No modified yet : ', content.match(/---\n(.+:.+\n)+---/gs));
|
|
const analysis = content.match(/^---\n((?!-{3})(?:(?:.+:.*)|(?:\s{2}-\s.+))\n)+---/);
|
|
// const analysis = content.match(/---\n(.*:.*\n)*---/s);
|
|
// console.log(analysis);
|
|
const pnt = analysis.index + analysis[0].length - 3;
|
|
this.app.vault.modify(activeFile, content.slice(0, pnt) + modified + '\n' + content.slice(pnt));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
} |