diff --git a/pages/star-search/index.tsx b/pages/star-search/index.tsx index 1de4dda78e..890ab8067d 100644 --- a/pages/star-search/index.tsx +++ b/pages/star-search/index.tsx @@ -170,13 +170,37 @@ export default function StarSearchPage({ userId, bearerToken, ogImageUrl }: Star values .filter((v) => v.startsWith("data:")) .forEach((v) => { - const matched = v.match(/data:\s(?.+)/); + /* + * regex for capturing star-search stream SSEs: + * data:\s?(?.*) + * + * The aim of this regex is to capture all characters coming from + * the star-search server side events while also preserving the + * empty "data:" frames that may come through (which are newlines). + * + * 'data:' - matches the "data:" characters explicitly. + * '\s' - matches any whitespace that follows the data. In most cases, this is a single space ' '. + * '?' - matches the previous whitespace token zero or one times. Aka, is optional. + * + * '(?.*)' - optional named capture group "result". + * ├────── '?' - capture group is optional. + * ├────── '' - capture group is named "result". + * └────── '.*' - matches any characters (including zero characters) after the "data:\s?" segment. + * this is in service of also capturing empty strings as newlines. + */ + + const matched = v.match(/data:\s?(?.*)/); + if (!matched || !matched.groups) { return; } const temp = [...chat]; const changed = temp.at(temp.length - 1); - changed!.content += matched.groups.result; + if (matched.groups.result === "") { + changed!.content += "  \n"; + } else { + changed!.content += matched.groups.result; + } setChat(temp); }); }