/ clis / google / utils.js
utils.js
 1  /**
 2   * Google adapter utilities.
 3   * Shared RSS parser for news and trends commands.
 4   */
 5  /**
 6   * Parse RSS XML by splitting into <item> blocks, then extracting fields per block.
 7   * Handles both plain text and CDATA-wrapped content.
 8   */
 9  export function parseRssItems(xml, fields) {
10      const items = xml.match(/<item>([\s\S]*?)<\/item>/g) || [];
11      return items.map(block => {
12          const record = {};
13          for (const field of fields) {
14              // Escape regex special characters in field name (e.g. ht:approx_traffic is safe, but defensive)
15              const escaped = field.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
16              // Handle tags with attributes (e.g. <source url="...">text</source>) and CDATA wrapping
17              // (?:\s[^>]*)? ensures we don't match prefix tags (e.g. <sourceUrl> when looking for <source>)
18              const match = block.match(new RegExp(`<${escaped}(?:\\s[^>]*)?>(?:<!\\[CDATA\\[)?([\\s\\S]*?)(?:\\]\\]>)?</${escaped}>`));
19              record[field] = match ? match[1].trim() : '';
20          }
21          return record;
22      });
23  }