Digested-Tile 2024-09-13
Authors:: Unknown License:: Unspecified Digest Root:: 60c33964c7eb
MarkdownTile
upsertToFile.js
async function ensureDirectoryExists(path) {
// Extract the directory path by removing the last part of the path (assumed to be a file)
const dirs = path.split('/').slice(0, -1).join('/');
// Check if the directory exists
if (!(await app.vault.adapter.exists(dirs))) {
// If the directory does not exist, create it
await app.vault.createFolder(dirs);
// Log a message to the console indicating that the directory was created
console.log(`Directory created: ${dirs}`);
}
}
async function upsertToFile(filePath, header, content) {
try {
// Ensure the directory for the file path exists
await ensureDirectoryExists(filePath);
// Attempt to retrieve the file from the vault
let file = app.vault.getAbstractFileByPath(filePath);
let fileContent = '';
// If the file exists, read its content
if (file) {
fileContent = await app.vault.read(file);
}
// Escape special characters in the header for use in a regular expression
const escapedHeader = header.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Create a regex to find the section of the file starting with the header
const regex = new RegExp(`${escapedHeader}[\\s\\S]*?(?=######|$)`, 'g');
// Check if the content already exists in the file under the specified header
if (regex.test(fileContent)) {
// If the content is already present, log and return unchanged status
if (fileContent.includes(content.trim())) {
console.log(`File content already up to date: ${filePath}`);
return { status: 'unchanged', file: file };
}
// Special handling for blockchain entries
if (header.startsWith('### [[') && header.includes('🔒 #ds/seal/')) {
if (fileContent.includes(header)) {
console.log(`Blockchain entry already exists: ${filePath}`);
new Notice(`Blockchain entry already exists: ${filePath}`, 5000);
return { status: 'unchanged', file: file };
}
}
// Append new content if the header exists but content is missing
fileContent += `\n\n${content}\n`;
} else {
// Append content if the header does not exist
fileContent += `${content}\n\n`;
}
// Update the existing file or create a new one with the updated content
if (file) {
await app.vault.modify(file, fileContent);
} else {
file = await app.vault.create(filePath, fileContent);
}
console.log(`File upserted successfully: ${filePath}`);
return { status: 'updated', file: file };
} catch (error) {
// Log and throw any errors encountered during the process
console.error(`Error upserting file ${filePath}:`, error);
throw error;
}
}
module.exports = upsertToFile;DeformattedTile
upsertToFile.js
async function ensureDirectoryExists(path) {
// Extract the directory path by removing the last part of the path (assumed to be a file)
const dirs = path.split('/').slice(0, -1).join('/');
// Check if the directory exists
if (!(await app.vault.adapter.exists(dirs))) {
// If the directory does not exist, create it
await app.vault.createFolder(dirs);
// Log a message to the console indicating that the directory was created
console.log(`Directory created: ${dirs}`);
}
}
async function upsertToFile(filePath, header, content) {
try {
// Ensure the directory for the file path exists
await ensureDirectoryExists(filePath);
// Attempt to retrieve the file from the vault
let file = app.vault.getAbstractFileByPath(filePath);
let fileContent = '';
// If the file exists, read its content
if (file) {
fileContent = await app.vault.read(file);
}
// Escape special characters in the header for use in a regular expression
const escapedHeader = header.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Create a regex to find the section of the file starting with the header
const regex = new RegExp(`${escapedHeader}[\\s\\S]*?(?=######|$)`, 'g');
// Check if the content already exists in the file under the specified header
if (regex.test(fileContent)) {
// If the content is already present, log and return unchanged status
if (fileContent.includes(content.trim())) {
console.log(`File content already up to date: ${filePath}`);
return { status: 'unchanged', file: file };
}
// Special handling for blockchain entries
if (header.startsWith('### [[') && header.includes('🔒 #ds/seal/')) {
if (fileContent.includes(header)) {
console.log(`Blockchain entry already exists: ${filePath}`);
new Notice(`Blockchain entry already exists: ${filePath}`, 5000);
return { status: 'unchanged', file: file };
}
}
// Append new content if the header exists but content is missing
fileContent += `\n\n${content}\n`;
} else {
// Append content if the header does not exist
fileContent += `${content}\n\n`;
}
// Update the existing file or create a new one with the updated content
if (file) {
await app.vault.modify(file, fileContent);
} else {
file = await app.vault.create(filePath, fileContent);
}
console.log(`File upserted successfully: ${filePath}`);
return { status: 'updated', file: file };
} catch (error) {
// Log and throw any errors encountered during the process
console.error(`Error upserting file ${filePath}:`, error);
throw error;
}
}
module.exports = upsertToFile;