<%* // Record start time for entire template runtime // Fixes for 1.4 // - Content-Verification Add a function to verify the content matches the digest when sealing throw a notice/log. // - Metadata - Include more metadata in the footnote, such as: Seal date and hash (after sealing), File path or name, Author information (if available in frontmatter “authors”) // - [-] PUNT Version-Control - Add functionality to interact with version control systems (e.g., Git) to track changes to sealed content. // - BUG Make Seal Command idempotent, it should not create a new seal each time // - BUG throw error, notice, and log if no text is selected const startTime = performance.now();
// Import necessary modules const crypto = require(‘crypto’);
// Function to compute a 12-character SHA-256 hash function computeHash(text) { const fullHash = crypto.createHash(‘sha256’).update(text).digest(‘hex’); return fullHash.slice(0, 12); }
// Get the currently selected text in Obsidian
console.log([${new Date().toISOString()}] Getting selected text);
let selection = app.workspace.activeEditor.getSelection();
// Check if a Digest Link exists
const digestLinkMatch = selection.match(/[^ds/([a-f0-9]{12})/(\d{4}-\d{2}-\d{2})^]/);
if (!digestLinkMatch) {
const errorMessage = “No valid Digest Link found. Please run the Digest script first.”;
new Notice(errorMessage);
console.error([${new Date().toISOString()}] ${errorMessage});
throw new Error(errorMessage);
}
const [fullDigestLink, digest, digestDate] = digestLinkMatch;
console.log([${new Date().toISOString()}] Digest Link found: ${fullDigestLink});
// Check if a Seal Link already exists
const sealLinkMatch = selection.match(/\s(^ds-\d{4}-\d{2}-\d{2}-[a-f0-9]{12})/);
let existingSealLink = '';
if (sealLinkMatch) {
existingSealLink = sealLinkMatch[1];
selection = selection.replace(existingSealLink, '').trim();
console.log(`[{new Date().toISOString()}] Existing Seal Link found: ${existingSealLink}); new Notice(Existing Seal Link found. It will be updated.`);
}
// Get the current date for sealing const sealDate = new Date().toISOString().split(‘T’)[0];
// Compute the seal hash
const sealText = ${selection};
const sealHash = computeHash(sealText);
console.log([${new Date().toISOString()}] Seal Hash computed: ${sealHash});
// Create the new Seal Link
const newSealLink = ^ds-${sealDate}-${sealHash};
// Calculate hashtime const endTime = performance.now(); const hashtime = ((endTime - startTime) / 1000).toFixed(6);
// Create the notice and log message
const message = Date Seal: Digest: ${digest} Digest Date: ${digestDate} Seal Hash: ${sealHash} Seal Date: ${sealDate} Hashtime Seconds: ${hashtime} ${existingSealLink ? 'Previous Seal Link: ' + existingSealLink : ''} New Seal Link: ${newSealLink};
new Notice(message);
console.log([${new Date().toISOString()}] ${message});
// Update the selection with the new seal information
const updatedSelection = ${selection} ${newSealLink};
app.workspace.activeEditor.editor.replaceSelection(updatedSelection);
%>