fix: hashtag extraction and make remote links absolute

This commit is contained in:
2023-08-02 15:05:35 +02:00
parent 2d56710223
commit cebbb8af2b
9 changed files with 253 additions and 99 deletions

View File

@ -30,3 +30,49 @@ export const fixRenderedMarkdown = (content: string) => {
}
});
};
export async function fetchStream(url: string, cb: (chunk: string) => void) {
const response = await fetch(url);
const reader = response?.body?.getReader();
if (reader) {
while (true) {
const { done, value } = await reader.read();
if (done) return;
const data = new TextDecoder().decode(value);
data
.split("$")
.filter((d) => d && d.length)
.map((d) => cb(Array.isArray(d) ? d[0] : d));
}
}
}
export const createStreamResponse = () => {
let controller: ReadableStreamController<ArrayBufferView>;
const body = new ReadableStream({
start(cont) {
controller = cont;
},
});
const response = new Response(body, {
headers: {
"content-type": "text/plain",
"x-content-type-options": "nosniff",
},
});
function cancel() {
controller.close();
}
function enqueue(chunk: string) {
controller?.enqueue(new TextEncoder().encode("$" + chunk));
}
return {
response,
cancel,
enqueue,
};
};

View File

@ -20,9 +20,12 @@ export function extractHashTags(inputString: string) {
const hashtags = [];
for (
const [hashtag] of inputString.matchAll(/(?<!\()\B(\#[a-zA-Z\-]+\b)(?!;)/g)
const [hashtag] of inputString.matchAll(/(?:^|\s)#\S*(?<!\))/g)
) {
hashtags.push(hashtag.replace(/\#/g, ""));
const cleaned = hashtag.replace(/\#/g, "").trim();
if (cleaned.length > 2) {
hashtags.push(cleaned);
}
}
return hashtags;