Skip to content
Open
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
14 changes: 13 additions & 1 deletion common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,24 @@ export function neat(params: TemplateStringsArray, ...rest: string[]) {
}

const END_SYMBOLS = new Set(`."”;’'*!!??)}]\`>~`.split(''))
const END_SEQUENCES = ['\n```', '\n---', '\n***', '\n___', '\n===', '\n"""', '\n*/']
const MID_SYMBOLS = new Set(`.)}’'!?\``.split(''))

export function trimSentence(text: string) {
let index = -1,
checkpoint = -1
for (let i = text.length - 1; i >= 0; i--) {
sentence_loop: for (let i = text.length - 1; i >= 0; i--) {
// first check for end sequences
for (const seq of END_SEQUENCES) {
if (text.slice(i, i + seq.length) === seq) {
// only trim if it's not an opening sequence
if (i + seq.length < text.length && /\p{L}/u.test(text[i + seq.length])) {
break
}
index = i + seq.length - 1
break sentence_loop
}
}
if (END_SYMBOLS.has(text[i])) {
// Skip ahead if the punctuation mark is preceded by white space
if (i && /[\p{White_Space}\n<]/u.test(text[i - 1])) {
Expand Down
18 changes: 18 additions & 0 deletions tests/trim-sentence.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,22 @@ describe('trimSentence', () => {
const result = trimSentence(text)
expect(result).to.eq(`Hello world.`)
})

it('should stop at multi-symbol sequences', () => {
const text = '```javascript\nconst x = 1\n```\nMy code'
const result = trimSentence(text)
expect(result).to.eq('```javascript\nconst x = 1\n```')
})

it('should trim opening multi-symbol sequences that are followed by letters', () => {
const text = 'Beginning of my code block.\n```javascript'
const result = trimSentence(text)
expect(result).to.eq('Beginning of my code block.')
})

it('should trim duplicating symbols in multi-symbol sequences', () => {
const text = 'Hello World.\n------- Separated orphan'
const result = trimSentence(text)
expect(result).to.eq('Hello World.\n---')
})
})