Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 35x 35x 35x 2x 2x 35x 35x 5x 5x 5x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 28x 17x 17x 17x 20x 20x 43x 2x 2x 2x 2x 20x 20x 27x 27x 27x 4x 4x 4x 4x 4x 4x 4x 23x 27x 3x 3x 3x 20x 27x 1x 1x 1x 1x 19x 19x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 12x 12x 12x 12x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 10x 10x 12x 1x 1x | /**
* MT AI web chat (SB-1500): the pure half — safe Markdown, table helpers and
* error wording for POST /api/ai/chat.
*
* Answers come from a model, so they are untrusted text. The renderer escapes
* every character of HTML first and only then adds back a small, fixed set of
* tags (strong, em, code, ul/ol/li, p, br, and links to http(s)/mailto). No
* attribute is ever built from model text except a link's href, which has been
* escaped and must start with an allowed scheme. That is why the output is
* safe for v-html without a sanitizer library — the project has none, and
* `marked` passes raw HTML through.
*/
const HTML_ESCAPES = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
export const escapeHtml = text =>
String(text ?? '').replace(/[&<>"']/g, ch => HTML_ESCAPES[ch]);
// Matched against the already-escaped URL, so `&` appears as `&`.
const SAFE_URL = /^(https?:\/\/|mailto:)[^\s]+$/i;
/**
* Inline Markdown on one line of text: `code`, [links](url), **bold**, *em*.
* Escapes first; the patterns then run over escaped text only.
*/
export function renderInline(text) {
// Code spans are parked behind a private-use placeholder so the patterns
// below can't touch them; strip any the input already carries.
const codeSpans = [];
const source = String(text ?? '').replace(//g, '');
let html = escapeHtml(source).replace(/`([^`\n]+)`/g, (_, code) => {
codeSpans.push(code);
return `\uE000${codeSpans.length - 1}\uE000`;
});
html = html.replace(/\[([^\]\n]+)\]\(([^)\s]+)\)/g, (_, label, url) =>
SAFE_URL.test(url)
? `<a href="${url}" target="_blank" rel="noopener noreferrer nofollow">${label}</a>`
: label
);
html = html.replace(/\*\*(?=\S)([^\n]*?\S)\*\*/g, '<strong>$1</strong>');
html = html.replace(
/(^|[^*\w])\*(?=\S)([^*\n]*?\S)\*(?![*\w])/g,
'$1<em>$2</em>'
);
return html.replace(
/\uE000(\d+)\uE000/g,
(_, i) => `<code>${codeSpans[Number(i)]}</code>`
);
}
const BULLET = /^\s*[-*•]\s+(.*)$/;
const NUMBERED = /^\s*\d+[.)]\s+(.*)$/;
const HEADING = /^\s*#{1,6}\s+(.*)$/;
/**
* Block-level Markdown: paragraphs (blank-line separated, single newlines kept
* as <br>), bullet and numbered lists, and headings shown as bold lines.
*/
export function renderMarkdown(text) {
const lines = String(text ?? '')
.replace(/\r\n?/g, '\n')
.split('\n');
const out = [];
let paragraph = [];
let list = null; // { tag: 'ul' | 'ol', items: [] }
const flushParagraph = () => {
if (paragraph.length) {
out.push(`<p>${paragraph.map(renderInline).join('<br>')}</p>`);
paragraph = [];
}
};
const flushList = () => {
if (list) {
const items = list.items.map(i => `<li>${renderInline(i)}</li>`);
out.push(`<${list.tag}>${items.join('')}</${list.tag}>`);
list = null;
}
};
for (const line of lines) {
const bullet = line.match(BULLET);
const numbered = !bullet && line.match(NUMBERED);
if (bullet || numbered) {
const tag = bullet ? 'ul' : 'ol';
flushParagraph();
if (list && list.tag !== tag) flushList();
if (!list) list = { tag, items: [] };
list.items.push((bullet || numbered)[1]);
continue;
}
flushList();
if (!line.trim()) {
flushParagraph();
continue;
}
const heading = line.match(HEADING);
if (heading) {
flushParagraph();
out.push(`<p><strong>${renderInline(heading[1])}</strong></p>`);
continue;
}
paragraph.push(line.trim());
}
flushParagraph();
flushList();
return out.join('');
}
// "12", "-3", "+5", "1.5", "50%" — what standings and stats columns hold.
const NUMERIC_CELL = /^[+\-−]?\d+(\.\d+)?%?$/;
/**
* Indexes of columns whose every non-empty cell is a number, so they can be
* right-aligned. A column with no values at all is not numeric.
*/
export function numericColumns(table) {
const columns = table?.columns || [];
const rows = table?.rows || [];
return columns
.map((_, col) => {
const cells = rows
.map(row => String(row?.[col] ?? '').trim())
.filter(Boolean);
return cells.length > 0 && cells.every(c => NUMERIC_CELL.test(c));
})
.reduce((acc, numeric, col) => (numeric ? [...acc, col] : acc), []);
}
// Why POST /api/ai/chat failed, keyed by HTTP status.
const CHAT_ERRORS = {
401: 'Your session has expired. Please log in again.',
403: "Chat isn't available for your account.",
404: "That conversation couldn't be found. Start a new chat and try again.",
429: "You've reached the chat limit for now. Please try again later.",
502: 'The assistant had trouble answering. Please try again.',
503: 'Chat is temporarily unavailable. Please try again in a few minutes.',
};
/**
* The friendly message for a failed chat request. Errors without a status
* (timeout, offline, expired session) already carry a readable message from
* the auth store.
*/
export function chatErrorMessage(error) {
if (error?.status && CHAT_ERRORS[error.status]) {
return CHAT_ERRORS[error.status];
}
if (!error?.status && error?.message) return error.message;
return 'Something went wrong. Please try again.';
}
|