Poll: help fallback, // separators, quoted questions, multi-vote
This commit is contained in:
@@ -198,13 +198,37 @@ export interface ParsedPoll {
|
||||
|
||||
/** Parse `question | option 1 | option 2 [| option 3 [| option 4]]`. */
|
||||
export function parsePoll(text: string): ParsedPoll | undefined {
|
||||
const parts = text.split("|").map(part => part.trim()).filter(Boolean);
|
||||
if (parts.length < 3 || parts.length > 5) return undefined;
|
||||
// A quoted question lets the first option follow without a separator:
|
||||
// "What country is best" Sweden // Denmark // Madagaskar
|
||||
const quoted = text.match(/^\s*["'“”](.+?)["'“”]\s*([\s\S]*)$/);
|
||||
|
||||
const [question, ...options] = parts;
|
||||
if (!question || options.length < 2 || options.some(option => option.length > 80)) return undefined;
|
||||
let question: string;
|
||||
let optionParts: string[];
|
||||
|
||||
return { question: question.slice(0, 200), options: options.map(option => option.slice(0, 80)) };
|
||||
if (quoted) {
|
||||
question = quoted[1].trim();
|
||||
optionParts = splitPollParts(quoted[2]);
|
||||
} else {
|
||||
const parts = splitPollParts(text);
|
||||
question = parts[0]?.trim() ?? "";
|
||||
optionParts = parts.slice(1);
|
||||
}
|
||||
|
||||
if (!question || optionParts.length < 2 || optionParts.length > 4) return undefined;
|
||||
if (optionParts.some(option => option.length > 80)) return undefined;
|
||||
|
||||
return { question: question.slice(0, 200), options: optionParts.map(option => option.slice(0, 80)) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Split poll parts on `|` or on `//` with whitespace on both sides. The
|
||||
* whitespace guard keeps URLs such as `https://example.com` in one piece.
|
||||
*/
|
||||
function splitPollParts(text: string): string[] {
|
||||
return text
|
||||
.split(/\s+\/\/\s+|\s*\|\s*/)
|
||||
.map(part => part.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
/* Secret redaction for persistent logs. Keep it minimal. */
|
||||
|
||||
Reference in New Issue
Block a user