MCPcopy Create free account
hub / github.com/Superflows-AI/superflows / splitMarkdownIntoSentences

Function splitMarkdownIntoSentences

lib/embed-docs/utils.ts:199–234  ·  view source on GitHub ↗
(markdown: string)

Source from the content-addressed store, hash-verified

197}
198
199export function splitMarkdownIntoSentences(markdown: string): string[] {
200 /** Does what it says on the tin.
201 *
202 * Splits a paragraph of markdown (since we previously split by newline) into
203 * sentences. *Does not* split by newline. **/
204 let tmpMarkedItems: string[] = [];
205 let tmpString = markdown;
206
207 const markdownUrlRegex = /(!\[[\w\s\W]+]\([\w.\-\/]+\))/g;
208 let result;
209 while ((result = markdownUrlRegex.exec(markdown))) {
210 tmpMarkedItems.push(result[0]);
211 tmpString = tmpString.replace(result[0], `{${tmpMarkedItems.length - 1}}`);
212 }
213
214 let sentences = tmpString
215 .split(/(?<=[.!?])\s/g)
216 .map((e) => e.trim())
217 .filter(Boolean);
218
219 sentences = sentences.map((sentence) => {
220 const placeholderRegex = /\{(\d+)}/g;
221 let result;
222 while ((result = placeholderRegex.exec(sentence))) {
223 sentence = sentence.replace(result[0], tmpMarkedItems[Number(result[1])]);
224 }
225 return sentence + " ";
226 });
227
228 // Remove the space at the end of the last sentence
229 sentences[sentences.length - 1] = sentences[sentences.length - 1].slice(
230 0,
231 -1,
232 );
233 return sentences;
234}
235
236export function joinShortChunks(arr: string[], tokenLimit: number): string[] {
237 const result = [];

Callers 2

splitIntoTextChunksFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected