update highlight list SN

This commit is contained in:
andrew.ng 2026-05-29 09:57:53 +07:00
parent 4b7e87e768
commit 928355b9bc
1 changed files with 35 additions and 17 deletions

View File

@ -2186,28 +2186,46 @@ Ports Missing/Down: ${missing.length}\n\n`
// Helper function to highlight SNs from listInventory in outputTestLog // Helper function to highlight SNs from listInventory in outputTestLog
const highlightSnInConsoleOutput = (text: string, listInventory: any[] | undefined) => { const highlightSnInConsoleOutput = (text: string, listInventory: any[] | undefined) => {
if (!text || !listInventory || listInventory.length === 0) { if (!text || !listInventory || listInventory.length === 0) {
return escapeHtml(text || 'No test log available') return escapeHtml(text || 'No test log available');
} }
let result = escapeHtml(text) // 1. Extract, Deduplicate (Set), filter, and sort SNs
const snList = listInventory.map((item) => item.sn).filter((sn) => sn) const uniqueSns = [...new Set(listInventory.map((item) => item.sn).filter(Boolean))];
const snList = uniqueSns.sort((a, b) => b.length - a.length);
// Sort by length descending to match longest SNs first (avoid partial matches) if (snList.length === 0) {
snList.sort((a, b) => b.length - a.length) return escapeHtml(text);
}
snList.forEach((sn) => { // 2. Escape regex special chars and combine into a single Regex OR statement
if (sn) { const escapedForRegex = snList.map((sn) => sn.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
// Create a regex that matches the SN as a whole word/token const combinedRegex = new RegExp(`\\b(${escapedForRegex.join('|')})\\b`, 'g');
const regex = new RegExp(`\\b${sn.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\$&')}\\b`, 'g')
result = result.replace(
regex,
`<span id="${sn}" style="background-color:#fbbf24;color:#78350f;font-weight:600;padding:2px 6px;border-radius:3px;cursor:pointer;" title="Click Hardware Inventory link to scroll">${escapeHtml(sn)}</span>`
)
}
})
return result let result = '';
} let lastIndex = 0;
let match;
// 3. Single-pass execution over the raw text
while ((match = combinedRegex.exec(text)) !== null) {
const matchedSn = match[0];
const startIndex = match.index;
// Escape and append the text BEFORE the match
result += escapeHtml(text.substring(lastIndex, startIndex));
// Escape the SN and wrap it in the highlight span
const safeSn = escapeHtml(matchedSn);
result += `<span id="${safeSn}" style="background-color:#fbbf24;color:#78350f;font-weight:600;padding:2px 6px;border-radius:3px;cursor:pointer;" title="Click Hardware Inventory link to scroll">${safeSn}</span>`;
// Update the index to move forward
lastIndex = combinedRegex.lastIndex;
}
// Append any remaining text after the final match
result += escapeHtml(text.substring(lastIndex));
return result;
};
// ---- Body: full template mirroring index.html, table-based + inline styles ---- // ---- Body: full template mirroring index.html, table-based + inline styles ----
const body = `<!DOCTYPE html> const body = `<!DOCTYPE html>