/ src / components / lib / parseProcessorMarkdown.ts
parseProcessorMarkdown.ts
 1  // src/lib/parseMarkdown.ts
 2  export function parseProcessorMarkdown(md: string) {
 3    // First remove any empty lines and trim each line
 4    const cleanedMd = md
 5      .split('\n')
 6      .map(line => line.trim())
 7      .filter(line => line.length > 0)
 8      .join('\n');
 9  
10    const items = cleanedMd.split("---").map((section: string) => {
11      const lines = section.trim().split("\n");
12      
13      // Parse title (either ## [Name](URL) or ## Name format)
14      let name = "";
15      let url = "";
16      const titleLine = lines[0] || "";
17      
18      // Check for ## [Name](URL) format
19      const linkedTitleMatch = titleLine.match(/^## \[(.*?)\]\((.*?)\)/);
20      if (linkedTitleMatch) {
21        name = linkedTitleMatch[1];
22        url = linkedTitleMatch[2];
23      } 
24      // Check for ## Name format
25      else {
26        const plainTitleMatch = titleLine.match(/^## (.*)/);
27        if (plainTitleMatch) {
28          name = plainTitleMatch[1];
29        }
30      }
31  
32      // Parse other fields
33      const supportTypeMatch = lines[1]?.match(/- \*\*Support Type\*\*: (.*)/);
34      const descriptionMatch = lines[2]?.match(/- \*\*Description\*\*: (.*)/);
35      
36      // Get URL from URL line if not already set from title
37      const urlLineMatch = lines[3]?.match(/- \*\*URL\*\*: \[.*?\]\((.*?)\)/);
38      if (!url && urlLineMatch) {
39        url = urlLineMatch[1];
40      }
41      
42      // Parse logo/image URL
43      const logoMatch = lines[4]?.match(/<img src="(.*?)"/);
44  
45      return {
46        name: name,
47        supportType: supportTypeMatch ? supportTypeMatch[1].trim() : "",
48        description: descriptionMatch ? descriptionMatch[1].trim() : "",
49        url: url,
50        logoUrl: logoMatch ? logoMatch[1] : "",
51      };
52    });
53  
54    return items.filter((item) => item.name);
55  }