generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
122 lines (103 loc) · 3.4 KB
/
utils.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
App, Notice, TFile, Vault, WorkspaceLeaf, ViewState, normalizePath,
} from 'obsidian';
import { getNotePath } from 'vault';
export const DEBUG = !(process.env.BUILD_ENV === 'production')
if (DEBUG) console.log('DEBUG is enabled')
export const logger = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_args(...args: any[]) {
return [`${(new Date()).toISOString().slice(11, 23)} [people-link]`, ...args]
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
debug(...args: any[]) {
if (DEBUG) {
console.log(...this._args(...args))
}
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
log(...args: any[]) {
console.log(...this._args(...args))
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
warn(...args: any[]) {
console.warn(...this._args(...args))
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
error(...args: any[]) {
console.error(...this._args(...args))
},
}
// from: https://github.com/reorx/obsidian-paste-image-rename/blob/c187bf6ffc897da793eba930f9578710795460ec/src/utils.ts#L113-L124
interface VaultConfig {
useMarkdownLinks?: boolean
}
interface VaultWithConfig extends Vault {
config?: VaultConfig,
}
export function getVaultConfig(app: App): VaultConfig|undefined {
const vault = app.vault as VaultWithConfig
return vault.config
}
// This function is copied from https://github.com/argenos/nldates-obsidian/blob/e621a9609211174b964230f5bb890faa574fb7f1/src/utils.ts#L104
// Copyright (c) 2021 Argentina Ortega Sáinz
// Licensed under MIT
// TODO find a better way to do this
export function generateLinkFromName(app: App, filename: string, alias?: string) {
const useMarkdownLinks = (app.vault as VaultWithConfig).config?.useMarkdownLinks
const path = normalizePath(filename);
if (useMarkdownLinks) {
if (alias) {
return `[${alias}](${path.replace(/ /g, "%20")})`;
} else {
return `[${filename}](${path})`;
}
} else {
if (alias) {
return `[[${path}|${alias}]]`;
} else {
return `[[${path}]]`;
}
}
}
export async function createFile(folder: string, filename: string): Promise<TFile|undefined> {
const app = window.app as App;
const { vault } = app;
const normalizedPath = await getNotePath(folder, filename);
try {
const createdFile = await vault.create(normalizedPath, '');
return createdFile;
} catch (err) {
console.error(`Failed to create file: '${normalizedPath}'`, err);
new Notice(`Unable to create new file ${normalizedPath}: ${err}`);
}
}
export enum FileViewMode {
source = 'source', preview = 'preview', default = 'default'
}
export enum NewPaneDirection {
vertical = 'vertical', horizontal = 'horizontal'
}
export async function openFile(app: App, file: TFile, optional?: {openInNewPane?: boolean, direction?: NewPaneDirection, mode?: FileViewMode, focus?: boolean}): Promise<WorkspaceLeaf> {
let leaf: WorkspaceLeaf
if (optional?.openInNewPane && optional?.direction) {
leaf = app.workspace.getLeaf('split', optional.direction)
} else {
leaf = app.workspace.getLeaf('tab')
}
await leaf.openFile(file)
if (optional?.mode || optional?.focus) {
const viewState = leaf.getViewState()
await leaf.setViewState({
...viewState,
state: optional.mode && optional.mode !== 'default'
? {
...viewState.state,
mode: optional.mode,
}
: viewState.state,
popstate: true,
} as ViewState, { focus: optional?.focus })
}
return leaf
}