[fix](trx-frontend-http): tokenize ft8 grids reliably

Co-authored-by: Codex <codex@openai.com>
Signed-off-by: Stanislaw Grams <stanislawgrams@gmail.com>
This commit is contained in:
2026-02-09 22:15:24 +01:00
parent a8f7e9c8de
commit 1bfc23fec7
@@ -29,32 +29,42 @@ function addFt8Message(msg) {
} }
function renderFt8Message(message) { function renderFt8Message(message) {
const gridRegex = /[A-R]{2}\\d{2}(?:[A-X]{2})?/gi;
let out = ""; let out = "";
let lastIdx = 0; let i = 0;
for (const match of message.matchAll(gridRegex)) { while (i < message.length) {
const idx = match.index ?? 0; const ch = message[i];
const grid = match[0].toUpperCase(); if (isAlphaNum(ch)) {
const prev = idx > 0 ? message[idx - 1] : ""; let j = i + 1;
const next = idx + grid.length < message.length ? message[idx + grid.length] : ""; while (j < message.length && isAlphaNum(message[j])) j++;
if (isAlphaNum(prev) || isAlphaNum(next)) continue; const token = message.slice(i, j);
out += escapeHtml(message.slice(lastIdx, idx)); const grid = token.toUpperCase();
out += `<span class="ft8-locator">[${grid}]</span>`; if (/^[A-R]{2}\\d{2}(?:[A-X]{2})?$/.test(grid)) {
lastIdx = idx + grid.length; out += `<span class="ft8-locator">[${grid}]</span>`;
} else {
out += escapeHtml(token);
}
i = j;
} else {
out += escapeHtml(ch);
i += 1;
}
} }
out += escapeHtml(message.slice(lastIdx));
return out; return out;
} }
function extractFirstGrid(message) { function extractFirstGrid(message) {
const gridRegex = /[A-R]{2}\\d{2}(?:[A-X]{2})?/gi; let i = 0;
for (const match of message.matchAll(gridRegex)) { while (i < message.length) {
const idx = match.index ?? 0; if (isAlphaNum(message[i])) {
const grid = match[0].toUpperCase(); let j = i + 1;
const prev = idx > 0 ? message[idx - 1] : ""; while (j < message.length && isAlphaNum(message[j])) j++;
const next = idx + grid.length < message.length ? message[idx + grid.length] : ""; const token = message.slice(i, j);
if (isAlphaNum(prev) || isAlphaNum(next)) continue; const grid = token.toUpperCase();
return grid; if (/^[A-R]{2}\\d{2}(?:[A-X]{2})?$/.test(grid)) return grid;
i = j;
} else {
i += 1;
}
} }
return null; return null;
} }