Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/core/splitters/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export class MarkdownSplitter {
})
}

/**
* Appends a chunk to an accumulator string, preserving whitespace
*/
reconstructChunk(accumulator: string, chunk: Chunk): string {
return accumulator + (chunk.leadingWhitespace || '') + chunk.content + (chunk.trailingWhitespace || '')
}

/**
* Default split method - automatically detects and uses the appropriate splitting strategy.
* If the content contains ::page directives, splits by those.
Expand Down
16 changes: 4 additions & 12 deletions src/core/translators/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@ export class MarkdownTranslator {
let response = ''

for (const chunk of chunks) {
response += chunk.leadingWhitespace || ''

if (chunk.shouldTranslate) {
// eslint-disable-next-line no-await-in-loop
const translatedChunk = await this.chatModel.invoke(this.buildMessages({...options, content: chunk.content}))

response += translatedChunk.content as string
response = this.splitter.reconstructChunk(response, {...chunk, content: translatedChunk.content as string})
} else {
response += chunk.content
response = this.splitter.reconstructChunk(response, chunk)
}

response += chunk.trailingWhitespace || ''
}

return response
Expand All @@ -49,9 +45,7 @@ export class MarkdownTranslator {
const chunks = await this.splitter.split(options.content)

for (const chunk of chunks) {
if (chunk.leadingWhitespace) {
yield chunk.leadingWhitespace
}
yield chunk.leadingWhitespace || ''

if (chunk.shouldTranslate) {
// eslint-disable-next-line no-await-in-loop
Expand All @@ -65,9 +59,7 @@ export class MarkdownTranslator {
yield chunk.content
}

if (chunk.trailingWhitespace) {
yield chunk.trailingWhitespace
}
yield chunk.trailingWhitespace || ''
}
}

Expand Down