refactor: unify inline directives and media fetch

This commit is contained in:
Peter Steinberger
2026-01-10 03:01:04 +01:00
parent 4075895c4c
commit f28a4a34ad
15 changed files with 345 additions and 178 deletions

View File

@@ -0,0 +1,87 @@
export type InlineDirectiveParseResult = {
text: string;
audioAsVoice: boolean;
replyToId?: string;
replyToCurrent: boolean;
hasAudioTag: boolean;
hasReplyTag: boolean;
};
type InlineDirectiveParseOptions = {
currentMessageId?: string;
stripAudioTag?: boolean;
stripReplyTags?: boolean;
};
const AUDIO_TAG_RE = /\[\[\s*audio_as_voice\s*\]\]/gi;
const REPLY_TAG_RE =
/\[\[\s*(?:reply_to_current|reply_to\s*:\s*([^\]\n]+))\s*\]\]/gi;
function normalizeDirectiveWhitespace(text: string): string {
return text
.replace(/[ \t]+/g, " ")
.replace(/[ \t]*\n[ \t]*/g, "\n")
.trim();
}
export function parseInlineDirectives(
text?: string,
options: InlineDirectiveParseOptions = {},
): InlineDirectiveParseResult {
const {
currentMessageId,
stripAudioTag = true,
stripReplyTags = true,
} = options;
if (!text) {
return {
text: "",
audioAsVoice: false,
replyToCurrent: false,
hasAudioTag: false,
hasReplyTag: false,
};
}
let cleaned = text;
let audioAsVoice = false;
let hasAudioTag = false;
let hasReplyTag = false;
let sawCurrent = false;
let lastExplicitId: string | undefined;
cleaned = cleaned.replace(AUDIO_TAG_RE, (match) => {
audioAsVoice = true;
hasAudioTag = true;
return stripAudioTag ? " " : match;
});
cleaned = cleaned.replace(
REPLY_TAG_RE,
(match, idRaw: string | undefined) => {
hasReplyTag = true;
if (idRaw === undefined) {
sawCurrent = true;
} else {
const id = idRaw.trim();
if (id) lastExplicitId = id;
}
return stripReplyTags ? " " : match;
},
);
cleaned = normalizeDirectiveWhitespace(cleaned);
const replyToId =
lastExplicitId ??
(sawCurrent ? currentMessageId?.trim() || undefined : undefined);
return {
text: cleaned,
audioAsVoice,
replyToId,
replyToCurrent: sawCurrent,
hasAudioTag,
hasReplyTag,
};
}