mirror of
https://github.com/open-webui/open-webui
synced 2025-04-25 16:49:46 +00:00
refac
This commit is contained in:
parent
335b6b6c7a
commit
7ce51f2b4c
@ -6,7 +6,12 @@
|
|||||||
import { EditorState, Plugin, TextSelection } from 'prosemirror-state';
|
import { EditorState, Plugin, TextSelection } from 'prosemirror-state';
|
||||||
import { EditorView, Decoration, DecorationSet } from 'prosemirror-view';
|
import { EditorView, Decoration, DecorationSet } from 'prosemirror-view';
|
||||||
import { undo, redo, history } from 'prosemirror-history';
|
import { undo, redo, history } from 'prosemirror-history';
|
||||||
import { schema, defaultMarkdownParser, defaultMarkdownSerializer } from 'prosemirror-markdown';
|
import {
|
||||||
|
schema,
|
||||||
|
defaultMarkdownParser,
|
||||||
|
MarkdownParser,
|
||||||
|
defaultMarkdownSerializer
|
||||||
|
} from 'prosemirror-markdown';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
inputRules,
|
inputRules,
|
||||||
@ -19,7 +24,6 @@
|
|||||||
import { baseKeymap, chainCommands } from 'prosemirror-commands';
|
import { baseKeymap, chainCommands } from 'prosemirror-commands';
|
||||||
import { DOMParser, DOMSerializer, Schema, Fragment } from 'prosemirror-model';
|
import { DOMParser, DOMSerializer, Schema, Fragment } from 'prosemirror-model';
|
||||||
|
|
||||||
import { marked } from 'marked'; // Import marked for markdown parsing
|
|
||||||
import { dev } from '$app/environment';
|
import { dev } from '$app/environment';
|
||||||
|
|
||||||
export let className = 'input-prose';
|
export let className = 'input-prose';
|
||||||
@ -67,14 +71,31 @@
|
|||||||
.replace(/'/g, "'");
|
.replace(/'/g, "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method to convert markdown content to ProseMirror-compatible document
|
// Custom parsing rule that creates proper paragraphs for newlines and empty lines
|
||||||
function markdownToProseMirrorDoc(markdown: string) {
|
function markdownToProseMirrorDoc(markdown: string) {
|
||||||
console.log('Markdown:', markdown);
|
// Split the markdown into lines
|
||||||
|
const lines = markdown.split('\n\n');
|
||||||
|
|
||||||
// Parse the Markdown content into a ProseMirror document
|
// Create an array to hold our paragraph nodes
|
||||||
let doc = defaultMarkdownParser.parse(markdown || '');
|
const paragraphs = [];
|
||||||
|
|
||||||
return doc;
|
// Process each line
|
||||||
|
lines.forEach((line) => {
|
||||||
|
if (line.trim() === '') {
|
||||||
|
// For empty lines, create an empty paragraph
|
||||||
|
paragraphs.push(schema.nodes.paragraph.create());
|
||||||
|
} else {
|
||||||
|
// For non-empty lines, parse as usual
|
||||||
|
const doc = defaultMarkdownParser.parse(line);
|
||||||
|
// Extract the content of the parsed document
|
||||||
|
doc.content.forEach((node) => {
|
||||||
|
paragraphs.push(node);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a new document with these paragraphs
|
||||||
|
return schema.node('doc', null, paragraphs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a custom serializer for paragraphs
|
// Create a custom serializer for paragraphs
|
||||||
@ -84,7 +105,7 @@
|
|||||||
|
|
||||||
// If the paragraph is empty, just add an empty line.
|
// If the paragraph is empty, just add an empty line.
|
||||||
if (content === '') {
|
if (content === '') {
|
||||||
state.write('\n');
|
state.write('\n\n');
|
||||||
} else {
|
} else {
|
||||||
state.renderInline(node);
|
state.renderInline(node);
|
||||||
state.closeBlock(node);
|
state.closeBlock(node);
|
||||||
|
Loading…
Reference in New Issue
Block a user