diff --git a/src/main.ts b/src/main.ts index 7ad9e98..b9a2be7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -241,15 +241,31 @@ export default class ScriptoriumPlugin extends Plugin { // Continue anyway - file was created } - // For .adoc files, ensure file is visible in explorer but don't auto-open + // For .adoc files, try to auto-open after a longer delay to allow obsidian-asciidoc plugin to initialize if (file.extension === "adoc" || file.extension === "asciidoc") { log(`AsciiDoc file created: ${file.path}`); - // File should be visible in Obsidian's file explorer automatically - // since we used vault.create(). The file explorer will refresh automatically. - // We don't auto-open to prevent crashes (obsidian-asciidoc plugin handles opening) + // Wait longer for obsidian-asciidoc plugin to be ready + await new Promise(resolve => setTimeout(resolve, 500)); - new Notice(`Created ${filename} in ${folderPath}. Install obsidian-asciidoc plugin to edit in Obsidian.`); + try { + log(`Attempting to open AsciiDoc file: ${file.path}`); + const leaf = this.app.workspace.getMostRecentLeaf(); + if (leaf && leaf.view) { + await leaf.openFile(file, { active: true }); + log("AsciiDoc file opened successfully"); + new Notice(`Created and opened ${filename}`); + } else { + const newLeaf = this.app.workspace.getLeaf("tab"); + await newLeaf.openFile(file, { active: true }); + log("AsciiDoc file opened in new leaf"); + new Notice(`Created and opened ${filename}`); + } + } catch (error: any) { + logError("Error opening AsciiDoc file", error); + // Don't show error to user - file was created successfully + new Notice(`Created ${filename} in ${folderPath}. You may need to open it manually.`); + } } else { // Open the new file in Obsidian workspace (use active leaf or create new) // Use a small delay to ensure file is fully created before opening diff --git a/src/metadataManager.ts b/src/metadataManager.ts index 5b16ea1..080dbff 100644 --- a/src/metadataManager.ts +++ b/src/metadataManager.ts @@ -365,10 +365,14 @@ export async function writeMetadata( const { body } = parseMarkdownFrontmatter(currentContent); const frontmatter = formatMarkdownFrontmatter(metadata); - // If body is empty or only whitespace, add default content + // If body is empty, only whitespace, or only contains a single header line, add default content const trimmedBody = body.trim(); + // Check if body only contains a single header line (e.g., "# Title" or "## Title") + // Split by newlines and filter out empty lines to count non-empty lines + const nonEmptyLines = trimmedBody.split('\n').filter(line => line.trim().length > 0); + const isOnlyHeader = trimmedBody && nonEmptyLines.length === 1 && /^#+\s+.+$/.test(nonEmptyLines[0]); let finalBody = body; - if (!trimmedBody || trimmedBody.length === 0) { + if (!trimmedBody || trimmedBody.length === 0 || isOnlyHeader) { // For kind 1, just add placeholder text (no header) if (metadata.kind === 1) { finalBody = `place your content here\n\n---\n\n**How to use this app:**\n1. Edit your content above\n2. Click the Nostr menu button (lightning bolt icon ⚡) in the left sidebar\n3. Select "Create Nostr events" to create and sign events\n4. Select "Publish events to relays" to publish to relays`;