File tree Expand file tree Collapse file tree
src/app/core/main/editor/markdown Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 'use client'
2+
3+ import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
4+
5+ export const StableCodeBlockLowlight = CodeBlockLowlight . extend ( {
6+ addKeyboardShortcuts ( ) {
7+ const parentShortcuts = this . parent ?.( ) ?? { }
8+
9+ return {
10+ ...parentShortcuts ,
11+ Enter : ( { editor } ) => {
12+ const { selection } = editor . state
13+ const { $from, empty } = selection
14+
15+ if ( ! empty || $from . parent . type !== this . type ) {
16+ return false
17+ }
18+
19+ const isAtEnd = $from . parentOffset === $from . parent . nodeSize - 2
20+ const endsWithDoubleNewline = $from . parent . textContent . endsWith ( '\n\n' )
21+ const action = isAtEnd && endsWithDoubleNewline ? 'exit' : 'newline'
22+
23+ if ( action === 'exit' ) {
24+ return editor . chain ( )
25+ . command ( ( { tr } ) => {
26+ tr . delete ( $from . pos - 2 , $from . pos )
27+ return true
28+ } )
29+ . exitCode ( )
30+ . run ( )
31+ }
32+
33+ return editor . commands . insertContent ( '\n' )
34+ } ,
35+ }
36+ } ,
37+ } )
Original file line number Diff line number Diff line change @@ -6,7 +6,6 @@ import Placeholder from '@tiptap/extension-placeholder'
66import Link from '@tiptap/extension-link'
77import TaskList from '@tiptap/extension-task-list'
88import TaskItem from '@tiptap/extension-task-item'
9- import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
109import CharacterCount from '@tiptap/extension-character-count'
1110import Highlight from '@tiptap/extension-highlight'
1211import Underline from '@tiptap/extension-underline'
@@ -52,6 +51,7 @@ import { AISuggestionFloating } from './ai-suggestion-floating'
5251import emitter from '@/lib/emitter'
5352import { QuoteMark } from './quote-mark'
5453import { MarkdownParagraph } from './markdown-paragraph'
54+ import { StableCodeBlockLowlight } from './code-block-extension'
5555import useSettingStore from '@/stores/setting'
5656import useChatStore from '@/stores/chat'
5757import { Loader2 , X } from 'lucide-react'
@@ -315,7 +315,7 @@ export function TipTapEditor({
315315 TaskItem . configure ( {
316316 nested : true ,
317317 } ) ,
318- CodeBlockLowlight . configure ( {
318+ StableCodeBlockLowlight . configure ( {
319319 lowlight,
320320 } ) ,
321321 CharacterCount ,
You can’t perform that action at this time.
0 commit comments