harden: use regex for robust markdown table parsing

- Replace brittle column index parsing with regex matching
- Escape special characters in metric names for safe regex
- More reliable parsing against column drift and table format variations
- Prevents false matches from substring matching
This commit is contained in:
Ralph Khreish
2025-09-22 15:30:00 +02:00
parent aec5a80cfb
commit 1f9dfdd934

View File

@@ -7,19 +7,12 @@ function parseMetricsTable(content, metricName) {
for (let i = 0; i < lines.length; i++) { for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim(); const line = lines[i].trim();
if (line.includes(metricName)) { // Match a markdown table row like: | Metric Name | value | ...
// Split by | and get the value column, filtering empty strings const safeName = metricName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const columns = line const re = new RegExp(`^\\|\\s*${safeName}\\s*\\|\\s*([^|]+)\\|?`);
.split('|') const match = line.match(re);
.map((col) => col.trim()) if (match) {
.filter((col) => col.length > 0); return match[1].trim() || 'N/A';
// Look for the value in columns 1-3 (accounting for different table formats)
for (let j = 1; j < Math.min(columns.length, 4); j++) {
const value = columns[j];
if (value && value !== '---' && value !== metricName) {
return value;
}
}
} }
} }
return 'N/A'; return 'N/A';