File size: 1,509 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * 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);
}