Skip to content

Instantly share code, notes, and snippets.

@frankmeeuwsen
Last active November 16, 2024 14:09
Show Gist options
  • Select an option

  • Save frankmeeuwsen/27377aa9df923289c153e22a69e79850 to your computer and use it in GitHub Desktop.

Select an option

Save frankmeeuwsen/27377aa9df923289c153e22a69e79850 to your computer and use it in GitHub Desktop.
Gist from Drafts
// Create button element first
const container = this.container;
const refreshButton = container.createEl('button', {text: '๐Ÿ”„ New Verse'});
refreshButton.addEventListener('click', async () => {
    // Clear the container except for the button
    while (container.lastChild !== refreshButton) {
        container.removeChild(container.lastChild);
    }
    await main(); // Re-run the main script
});

// Main script function
async function main() {
    let pages = dv.pages('"4 - Resources/Taoism/tao te ching"');

    if (pages.length > 0) {
        let randomPage = pages[Math.floor(Math.random() * pages.length)];
        let content = await dv.io.load(randomPage.file.path);
        
        if (content) {
            // Remove YAML frontmatter
            content = content.replace(/---[\s\S]*?---/g, '');
            
            // Split into paragraphs and clean them
            let paragraphs = content
                .split('\n\n')
                .map(p => p.trim())
                .filter(p => 
                    p !== '' && 
                    !p.startsWith('> **Note**') &&
                    !p.startsWith('> **note**')
                );
            
            if (paragraphs.length > 0) {
                let randomParagraph = paragraphs[Math.floor(Math.random() * paragraphs.length)];
                
                // Format the output
                //dv.header(3, randomPage.file.name);
                dv.paragraph("*" + randomParagraph + "*");
                dv.paragraph("๐Ÿ“– [[" + randomPage.file.name + "]]");
            } else {
                dv.paragraph("No valid paragraphs found in the selected file");
            }
        } else {
            dv.paragraph("Could not access file content");
        }
    } else {
        dv.paragraph("No pages found in the specified directory");
    }
}

// Run the main script initially
await main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment