-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfrontmatter.ts
More file actions
41 lines (34 loc) · 1.21 KB
/
frontmatter.ts
File metadata and controls
41 lines (34 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { App, TFile } from 'obsidian';
import { moment } from 'obsidian';
import type { FrontmatterUpdate } from './types';
export function isHabrArticle(app: App, file: TFile) {
const cache = app.metadataCache.getFileCache(file);
const frontmatter = cache?.frontmatter as Record<string, unknown> | undefined;
const source = frontmatter?.source;
return (
typeof source === 'string' &&
(source.includes('habr.com') || source.includes('habr.ru'))
);
}
export async function updateFrontmatter(app: App, file: TFile, data: FrontmatterUpdate) {
await app.fileManager.processFrontMatter(
file,
(frontmatter: Record<string, unknown>) => {
if (data.source) {
frontmatter.source = data.source;
}
if (data.title) {
frontmatter.title = data.title;
}
if (data.published) {
const published = moment(data.published);
if (published.isValid()) {
frontmatter.published = published.toDate();
}
}
if (data.archived) {
frontmatter.archived = data.archived;
}
},
);
}