Last active
January 4, 2025 16:16
-
-
Save frankmeeuwsen/2a8d46e35316668e16281f25ae17f845 to your computer and use it in GitHub Desktop.
Javascript for random text in Obsidian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * RandomVerse Script voor Obsidian | |
| * Auteur: Frank Meeuwsen | |
| * Versie: 1.0.0 | |
| * | |
| * Dit script genereert willekeurige verzen uit een opgegeven map. | |
| * Gebruik: dv.view("randomtext", { opties }) | |
| * Beschrijving: | |
| * Sla dit script ergens in je Obsidian vault op met de naam randomtext. | |
| * Het kan in een folder /scripts, de folder /templates, het maakt niet uit, als het maar in je vault is. | |
| * Roep het script op de pagina aan met | |
| ```dataviewjs | |
| await dv.view("randomtext"); | |
| ``` | |
| * Je kunt met argumenten de configuratie per pagina aanpassen. Zo kun je bv maken | |
| ```dataviewjs | |
| await dv.view("randomtext", { | |
| buttonText: "πͺ· Random Tao Verse", | |
| }); | |
| ``` | |
| * Complete custom settings | |
| ```dataviewjs | |
| await dv.view("randomtext", { | |
| sourceFolder: '"4 - Resources/Poetry"', | |
| buttonText: "π Random Poem", | |
| linkEmoji: "π", | |
| maxParagraphs: 2, | |
| addDividers: true, | |
| showTitle: true, | |
| buttonStyle: ` | |
| background: rgba(255, 255, 255, 0.2); | |
| backdrop-filter: blur(5px); | |
| border: 1px solid rgba(255, 255, 255, 0.3); | |
| border-radius: 10px; | |
| padding: 8px 16px; | |
| color: var(--text-normal); | |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
| margin-bottom: 1em; | |
| transition: all 0.2s ease; | |
| &:hover { | |
| transform: translateY(-2px); | |
| box-shadow: 0 6px 8px rgba(0, 0, 0, 0.2); | |
| } | |
| ` | |
| }); | |
| ``` | |
| */ | |
| // Configuration | |
| const defaultConfig = { | |
| // Folder and query settings | |
| sourceFolder: '"4 - Resources/Taoism/tao te ching"', | |
| // Button settings | |
| buttonText: 'π New verse', | |
| // Optional styling | |
| buttonStyle: ` | |
| background-color: var(--interactive-accent); | |
| color: var(--text-on-accent); | |
| padding: 8px 16px; | |
| border: none; | |
| border-radius: var(--radius-l); | |
| margin-bottom: 1em; | |
| cursor: pointer; | |
| font-size: var(--font-ui-small); | |
| `, | |
| // Display settings | |
| linkEmoji: 'π', // Emoji shown before the source link | |
| // Optional: Additional display settings you might want to use | |
| showTitle: false, // Whether to show the file name as a header | |
| maxParagraphs: 1, // How many paragraphs to show (1 for single, more for multiple) | |
| addDividers: false, // Whether to add horizontal lines between paragraphs | |
| // Text cleaning settings | |
| excludePatterns: [ | |
| '> **Note**', | |
| '> **note**' | |
| // Add more patterns to exclude here | |
| ] | |
| }; | |
| // Merge default config with input arguments | |
| const CONFIG = {...defaultConfig, ...input}; | |
| // Create button element and container for output | |
| const container = dv.container; | |
| const refreshButton = document.createElement('button'); | |
| refreshButton.textContent = CONFIG.buttonText; | |
| refreshButton.style = CONFIG.buttonStyle; | |
| container.appendChild(refreshButton); | |
| // Create a div to hold the verse content | |
| const verseContainer = document.createElement('div'); | |
| container.appendChild(verseContainer); | |
| // Main script function | |
| async function main() { | |
| // Clear previous content | |
| verseContainer.innerHTML = ''; | |
| let pages = dv.pages(CONFIG.sourceFolder); | |
| 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 !== '' && | |
| !CONFIG.excludePatterns.some(pattern => p.startsWith(pattern)) | |
| ); | |
| if (paragraphs.length > 0) { | |
| // Select random paragraphs based on maxParagraphs setting | |
| let selectedParagraphs = []; | |
| for (let i = 0; i < Math.min(CONFIG.maxParagraphs, paragraphs.length); i++) { | |
| let randomIndex = Math.floor(Math.random() * paragraphs.length); | |
| selectedParagraphs.push(paragraphs[randomIndex]); | |
| paragraphs.splice(randomIndex, 1); // Remove selected paragraph to avoid duplicates | |
| } | |
| // Show title if configured | |
| if (CONFIG.showTitle) { | |
| const titleElement = dv.header(3, randomPage.file.name); | |
| verseContainer.appendChild(titleElement); | |
| } | |
| // Display selected paragraphs | |
| selectedParagraphs.forEach((paragraph, index) => { | |
| const verseText = dv.paragraph("*" + paragraph + "*"); | |
| verseContainer.appendChild(verseText); | |
| // Add divider if configured and not the last paragraph | |
| if (CONFIG.addDividers && index < selectedParagraphs.length - 1) { | |
| const divider = dv.paragraph("---"); | |
| verseContainer.appendChild(divider); | |
| } | |
| }); | |
| // Add source link with configured emoji | |
| const verseSource = dv.paragraph(`${CONFIG.linkEmoji} [[${randomPage.file.name}]]`); | |
| verseContainer.appendChild(verseSource); | |
| } else { | |
| const errorMsg = dv.paragraph("No valid paragraphs found in the selected file"); | |
| verseContainer.appendChild(errorMsg); | |
| } | |
| } else { | |
| const errorMsg = dv.paragraph("Could not access file content"); | |
| verseContainer.appendChild(errorMsg); | |
| } | |
| } else { | |
| const errorMsg = dv.paragraph("No pages found in the specified directory"); | |
| verseContainer.appendChild(errorMsg); | |
| } | |
| } | |
| // Add click event listener to the refresh button | |
| refreshButton.addEventListener('click', main); | |
| // 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