Digested-Tile 2024-09-13

Authors:: Unknown License:: Unspecified Digest Root:: 0daeda8530f1

MarkdownTile

getPreviousBlockchainEntryDigest.js

async function getPreviousBlockchainEntryDigest(app, blockchainDirectoryPath, computeSHA256) {
    // Get the current date
    const currentDate = new Date();
    const currentYear = currentDate.getFullYear();
    const currentMonth = currentDate.getMonth() + 1; // JavaScript months are 0-based
  
    // Format the current year and month as YY-MM
    const currentYYMM = `${String(currentYear).slice(-2)}-${String(currentMonth).padStart(2, '0')}`;
  
    let noticeMessage = `Searching for PreviousBlockchainEntryDigest:\n`;
    noticeMessage += `Blockchain Directory: ${blockchainDirectoryPath}\n\n`;
  
    // Function to get the last block from a file
    async function getLastBlockFromFile(filePath) {
        try {
            const fileContent = await app.vault.adapter.read(filePath);
            const blockMatches = [...fileContent.matchAll(/#ds\/block\/([a-f0-9]+)/g)];
            if (blockMatches.length > 0) {
                const lastMatch = blockMatches[blockMatches.length - 1];
                return lastMatch[1];
            }
            return null;
        } catch (error) {
            console.error(`Error reading file ${filePath}:`, error);
            return null;
        }
    }
  
    try {
        // Get all files in the directory
        const { files } = await app.vault.adapter.list(blockchainDirectoryPath);
        
        // Log all files in the directory
        console.log(`Contents of ${blockchainDirectoryPath}:`);
        console.log('Files:', files);
        console.log(`Total files found: ${files.length}`);
  
        noticeMessage += `Total files found in directory: ${files.length}\n`;
        noticeMessage += `Files found:\n${files.map(file => `- ${file}`).join('\n')}\n\n`;
  
        // Sort files in reverse chronological order
        const sortedFiles = files.sort((a, b) => b.localeCompare(a));
  
        console.log(`Sorted files:`, sortedFiles);
        noticeMessage += `Searching files in reverse chronological order:\n`;
  
        let blocksFound = false;
  
        // Iterate through sorted files to find the latest block
        for (const file of sortedFiles) {
            console.log(`Checking file: ${file}`);
            noticeMessage += `- Checking file: ${file}\n`;
            
            const lastBlock = await getLastBlockFromFile(file);
            if (lastBlock) {
                blocksFound = true;
                console.log(`Found last block in file ${file}: ${lastBlock}`);
                noticeMessage += `  Found last block: ${lastBlock}\n\n`;
                noticeMessage += `PreviousBlockchainEntryDigest chosen: ${lastBlock}\n`;
                noticeMessage += `Reason: This is the most recent block found in the latest file (${file}).`;
                new Notice(noticeMessage, 20000);  // 20 seconds duration
                return lastBlock;
            }
            console.log(`No blocks found in file ${file}.`);
            noticeMessage += `  No blocks found in this file.\n`;
        }
  
        if (!blocksFound) {
            console.log('No blocks were found in any file.');
            noticeMessage += '\nNo blocks were found in any file.\n';
        }
  
        // If no blocks found in any file, use the hash of the current YY-MM
        console.log(`Using hash of current YY-MM (${currentYYMM}) as previousBlockchainEntryDigest`);
        const fallbackHash = computeSHA256(currentYYMM);
        noticeMessage += `\nPreviousBlockchainEntryDigest chosen: ${fallbackHash}\n`;
        noticeMessage += `Reason: No existing blocks found. Using hash of current YY-MM (${currentYYMM}) as fallback.`;
        new Notice(noticeMessage, 20000);  // 20 seconds duration
        return fallbackHash;
  
    } catch (error) {
        console.error(`Error accessing blockchain directory ${blockchainDirectoryPath}:`, error);
        noticeMessage += `\nError accessing blockchain directory: ${error.message}\n`;
        noticeMessage += 'Using fallback method to generate PreviousBlockchainEntryDigest.';
        
        const fallbackHash = computeSHA256(currentYYMM);
        noticeMessage += `\nPreviousBlockchainEntryDigest (fallback): ${fallbackHash}`;
        new Notice(noticeMessage, 20000);  // 20 seconds duration
        return fallbackHash;
    }
}
 
module.exports = getPreviousBlockchainEntryDigest;
DeformattedTile

getPreviousBlockchainEntryDigest.js

async function getPreviousBlockchainEntryDigest(app, blockchainDirectoryPath, computeSHA256) {
    // Get the current date
    const currentDate = new Date();
    const currentYear = currentDate.getFullYear();
    const currentMonth = currentDate.getMonth() + 1; // JavaScript months are 0-based
  
    // Format the current year and month as YY-MM
    const currentYYMM = `${String(currentYear).slice(-2)}-${String(currentMonth).padStart(2, '0')}`;
  
    let noticeMessage = `Searching for PreviousBlockchainEntryDigest:\n`;
    noticeMessage += `Blockchain Directory: ${blockchainDirectoryPath}\n\n`;
  
    // Function to get the last block from a file
    async function getLastBlockFromFile(filePath) {
        try {
            const fileContent = await app.vault.adapter.read(filePath);
            const blockMatches = [...fileContent.matchAll(/#ds\/block\/([a-f0-9]+)/g)];
            if (blockMatches.length > 0) {
                const lastMatch = blockMatches[blockMatches.length - 1];
                return lastMatch[1];
            }
            return null;
        } catch (error) {
            console.error(`Error reading file ${filePath}:`, error);
            return null;
        }
    }
  
    try {
        // Get all files in the directory
        const { files } = await app.vault.adapter.list(blockchainDirectoryPath);
        
        // Log all files in the directory
        console.log(`Contents of ${blockchainDirectoryPath}:`);
        console.log('Files:', files);
        console.log(`Total files found: ${files.length}`);
  
        noticeMessage += `Total files found in directory: ${files.length}\n`;
        noticeMessage += `Files found:\n${files.map(file => `- ${file}`).join('\n')}\n\n`;
  
        // Sort files in reverse chronological order
        const sortedFiles = files.sort((a, b) => b.localeCompare(a));
  
        console.log(`Sorted files:`, sortedFiles);
        noticeMessage += `Searching files in reverse chronological order:\n`;
  
        let blocksFound = false;
  
        // Iterate through sorted files to find the latest block
        for (const file of sortedFiles) {
            console.log(`Checking file: ${file}`);
            noticeMessage += `- Checking file: ${file}\n`;
            
            const lastBlock = await getLastBlockFromFile(file);
            if (lastBlock) {
                blocksFound = true;
                console.log(`Found last block in file ${file}: ${lastBlock}`);
                noticeMessage += `  Found last block: ${lastBlock}\n\n`;
                noticeMessage += `PreviousBlockchainEntryDigest chosen: ${lastBlock}\n`;
                noticeMessage += `Reason: This is the most recent block found in the latest file (${file}).`;
                new Notice(noticeMessage, 20000);  // 20 seconds duration
                return lastBlock;
            }
            console.log(`No blocks found in file ${file}.`);
            noticeMessage += `  No blocks found in this file.\n`;
        }
  
        if (!blocksFound) {
            console.log('No blocks were found in any file.');
            noticeMessage += '\nNo blocks were found in any file.\n';
        }
  
        // If no blocks found in any file, use the hash of the current YY-MM
        console.log(`Using hash of current YY-MM (${currentYYMM}) as previousBlockchainEntryDigest`);
        const fallbackHash = computeSHA256(currentYYMM);
        noticeMessage += `\nPreviousBlockchainEntryDigest chosen: ${fallbackHash}\n`;
        noticeMessage += `Reason: No existing blocks found. Using hash of current YY-MM (${currentYYMM}) as fallback.`;
        new Notice(noticeMessage, 20000);  // 20 seconds duration
        return fallbackHash;
  
    } catch (error) {
        console.error(`Error accessing blockchain directory ${blockchainDirectoryPath}:`, error);
        noticeMessage += `\nError accessing blockchain directory: ${error.message}\n`;
        noticeMessage += 'Using fallback method to generate PreviousBlockchainEntryDigest.';
        
        const fallbackHash = computeSHA256(currentYYMM);
        noticeMessage += `\nPreviousBlockchainEntryDigest (fallback): ${fallbackHash}`;
        new Notice(noticeMessage, 20000);  // 20 seconds duration
        return fallbackHash;
    }
}
module.exports = getPreviousBlockchainEntryDigest;
EOT