Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(frontend): preserve nonempty lists when pressing Enter (#4224)

Jarvisa7a5f47625f3
diff --git a/frontend/src/utils/markdownEditing.test.ts b/frontend/src/utils/markdownEditing.test.ts
index d6e8ca237..0edc7a1fc 100644
--- a/frontend/src/utils/markdownEditing.test.ts
+++ b/frontend/src/utils/markdownEditing.test.ts
@@ -20,6 +20,35 @@ test('Enter on an empty item ends the list instead of stacking markers', () => {
assert.deepEqual(patch, { value: '- first\n', start: 8, end: 8 })
})
+for (const { value, at, expected } of [
+ { value: '- hello', at: 2, expected: '- \n- hello' },
+ { value: '* hello', at: 2, expected: '* \n* hello' },
+ { value: '1. hello', at: 3, expected: '1. \n2. hello' },
+ { value: '- [x] hello', at: 6, expected: '- [x] \n- [ ] hello' },
+ { value: ' - hello', at: 4, expected: ' - \n - hello' },
+ { value: 'before\n- hello\nafter', at: 9, expected: 'before\n- \n- hello\nafter' },
+]) {
+ test(`Enter before existing list content preserves the marker: ${JSON.stringify(value)}`, () => {
+ const patch = continueListOnEnter(caret(value, at))
+ assert.ok(patch)
+ assert.equal(patch.value, expected)
+ assert.equal(patch.start, expected.indexOf('hello'))
+ assert.equal(patch.end, patch.start)
+ })
+}
+
+test('Enter in the middle of list content splits the item', () => {
+ assert.deepEqual(continueListOnEnter(caret('- hello world', 7)), {
+ value: '- hello\n- world', start: 10, end: 10,
+ })
+})
+
+test('an empty item with trailing whitespace still exits the list', () => {
+ assert.deepEqual(continueListOnEnter(caret('- first\n- ', 10)), {
+ value: '- first\n ', start: 8, end: 8,
+ })
+})
+
test('Enter outside a list and over a selection is left to the browser', () => {
assert.equal(continueListOnEnter(caret('plain text', 10)), null)
assert.equal(continueListOnEnter({ value: '- first', start: 2, end: 7 }), null)
diff --git a/frontend/src/utils/markdownEditing.ts b/frontend/src/utils/markdownEditing.ts
index e959f12e4..5a2ab661d 100644
--- a/frontend/src/utils/markdownEditing.ts
+++ b/frontend/src/utils/markdownEditing.ts
@@ -47,7 +47,8 @@ export function continueListOnEnter({ value, start, end }: EditorState): EditorP
if (!match) return null
const [, indent, marker, checkbox, content] = match
- if (!content.trim()) {
+ const remainingContent = value.slice(start, lineEndAt(value, start))
+ if (!content.trim() && !remainingContent.trim()) {
// Empty item: drop the marker and leave the caret on a clean line.
const nextValue = value.slice(0, lineStart) + value.slice(start)
return { value: nextValue, start: lineStart, end: lineStart }