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

Function splitIntoTextChunks

lib/embed-docs/utils.ts:66–103  ·  view source on GitHub ↗
(
  sectionText: string,
  ignoreLines: string[],
)

Source from the content-addressed store, hash-verified

64}
65
66export function splitIntoTextChunks(
67 sectionText: string,
68 ignoreLines: string[],
69): string[] {
70 const codeAndNonCodeChunks = splitOutCodeChunks(sectionText);
71 return codeAndNonCodeChunks
72 .map((codeNonCodeChunk): string[] => {
73 if (codeNonCodeChunk.startsWith("```")) {
74 return [codeNonCodeChunk];
75 }
76 return joinShortChunks(
77 codeNonCodeChunk
78 .split("\n")
79 .filter(
80 (chunk) =>
81 !(
82 ignoreLines.includes(RemoveMarkdown(chunk.trim())) ||
83 ignoreLines.includes(chunk.trim())
84 ),
85 )
86 // Below aims to remove not-useful lines. E.g. link to privacy policy, today's date etc
87 .filter((chunk) => isTextWithSubstance(chunk.trim()))
88 // Tabs are more token-efficient than 4 spaces
89 .map((chunk) => chunk.replaceAll(/ {4}/g, "\t") + "\n")
90 // Remove duplicate lines
91 .filter((line, idx, lines) => idx === 0 || line !== lines[idx - 1])
92 .map((chunk) => {
93 // Break up large paragraphs into sentences
94 const chunkTokenCount = getTokenCount(chunk);
95 if (chunkTokenCount <= 80) return chunk;
96 return splitMarkdownIntoSentences(chunk);
97 })
98 .flat(),
99 25,
100 );
101 })
102 .flat();
103}
104
105export function splitOutCodeChunks(sectionText: string): string[] {
106 const chunks: string[] = [];

Callers 3

embedDocsFromUrlFunction · 0.90
embedTextFunction · 0.90

Calls 5

getTokenCountFunction · 0.90
splitOutCodeChunksFunction · 0.85
joinShortChunksFunction · 0.85
isTextWithSubstanceFunction · 0.85

Tested by

no test coverage detected