generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessBibTeX.ts
72 lines (60 loc) · 2.88 KB
/
processBibTeX.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
import { normalizePath, TFile, Notice } from 'obsidian';
import { createAuthorPage, updateAuthorPageContent} from './authors';
import { buildFrontmatter } from './frontmatter';
import { parseBibTeX } from './parseBibTeX';
import { ensureFoldersExist } from './ensureFoldersExist';
export async function processBibTeX(bibtexData: string) {
console.log('Processing BibTeX data:', bibtexData); // Check the BibTeX data
// Parse BibTeX input
const parsedData = await parseBibTeX(bibtexData);
console.log('Parsed BibTeX data:', parsedData); // Check the parsed data
if (!parsedData) {
new Notice('Failed to parse BibTeX data.');
return;
}
// Generate folder hierarchy if necessary
console.log('Ensuring folders exist...'); // Check if ensuring folders exist
await ensureFoldersExist();
const vault = this.app.vault;
// Process references
console.log('Processing references...'); // Check if processing references
for (const reference of parsedData.references) {
const { title } = reference;
// Check if reference page already exists
const referencePagePath = `Sources/References/${normalizePath(title)}.md`;
const referencePageExists = await vault.adapter.exists(referencePagePath);
// If reference page already exists, skip creation
if (referencePageExists) {
console.log(`Reference page already exists: ${referencePagePath}`);
continue;
}
// Create reference page
try {
console.log(`Creating reference page: ${referencePagePath}`); // Check if creating reference page
let referenceContent = `# ${title}`;
const frontmatter = await buildFrontmatter(reference);
if(reference.abstract != ""){
referenceContent = referenceContent + `\n## Abstract\n${reference.abstract}`;
}
const fullContent = `${frontmatter}\n${referenceContent}`;
await vault.create(referencePagePath, fullContent);
console.log(`Created reference page: ${referencePagePath}`);
} catch (error) {
console.error('Error creating reference page:', error);
}
}
// Process authors
console.log('Processing authors...'); // Check if processing authors
for (const author of parsedData.authors) {
const authorPagePath = `Sources/Authors/${normalizePath(author.name)}.md`;
const authorPage = vault.getAbstractFileByPath(authorPagePath) as TFile; // Cast to TFile
if (authorPage) {
await updateAuthorPageContent(authorPage, author.name, parsedData.references, vault);
} else {
// If author page doesn't exist, create it
await createAuthorPage(authorPagePath, author.name, parsedData.references, vault);
}
}
// Display success message
new Notice('BibTeX processing complete!');
}