Spaces:
Running
Running
| /** | |
| * Utility functions for markdown detection and rendering | |
| */ | |
| /** | |
| * Detects if content contains markdown syntax patterns | |
| */ | |
| export function isMarkdownContent(content: string): boolean { | |
| if (!content || content.trim() === "") return false; | |
| // Common markdown patterns | |
| const markdownPatterns = [ | |
| /^#{1,6}\s+/m, // Headers (# ## ### etc) | |
| /\*\*.*?\*\*/, // Bold text | |
| /\*.*?\*/, // Italic text | |
| /`.*?`/, // Inline code | |
| /```[\s\S]*?```/, // Code blocks | |
| /^\s*[*\-+]\s+/m, // Unordered lists | |
| /^\s*\d+\.\s+/m, // Ordered lists | |
| /\[.*?\]\(.*?\)/, // Links | |
| /!\[.*?\]\(.*?\)/, // Images | |
| /^\s*>\s+/m, // Blockquotes | |
| /^\s*\|\s*.*\s*\|/m, // Tables | |
| /^---+$/m, // Horizontal rules | |
| ]; | |
| return markdownPatterns.some((pattern) => pattern.test(content)); | |
| } | |
| /** | |
| * Detects if filename has markdown extension | |
| */ | |
| export function isMarkdownFile(filename?: string): boolean { | |
| if (!filename) return false; | |
| const markdownExtensions = [".md", ".markdown", ".mdown", ".mkd"]; | |
| const lowercaseFilename = filename.toLowerCase(); | |
| return markdownExtensions.some((ext) => lowercaseFilename.endsWith(ext)); | |
| } | |
| /** | |
| * Determines if content should be rendered as markdown based on content patterns and filename | |
| */ | |
| export function shouldRenderAsMarkdown( | |
| content: string, | |
| filename?: string | |
| ): boolean { | |
| // First check filename extension | |
| if (isMarkdownFile(filename)) { | |
| return true; | |
| } | |
| // Then check content patterns | |
| return isMarkdownContent(content); | |
| } | |