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++) {
const line = lines[i].trim();
if (line.includes(metricName)) {
// Split by | and get the value column, filtering empty strings
const columns = line
.split('|')
.map((col) => col.trim())
.filter((col) => col.length > 0);
// 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;
}
}
// Match a markdown table row like: | Metric Name | value | ...
const safeName = metricName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp(`^\\|\\s*${safeName}\\s*\\|\\s*([^|]+)\\|?`);
const match = line.match(re);
if (match) {
return match[1].trim() || 'N/A';
}
}
return 'N/A';