diff --git a/docs/src/content/docs/editor.md b/docs/src/content/docs/editor.md index 0835c70d..0604b7a8 100644 --- a/docs/src/content/docs/editor.md +++ b/docs/src/content/docs/editor.md @@ -18,6 +18,7 @@ const editor = new Editor({ nodeViews: {}, //https://prosemirror.net/docs/guide/#state, attributes: {}, // https://prosemirror.net/docs/ref/#view.EditorProps.attributes linkValidationPattern: '', + parseOptions: {}, // https://prosemirror.net/docs/ref/#model.ParseOptions }); ``` diff --git a/projects/ngx-editor/src/lib/Editor.ts b/projects/ngx-editor/src/lib/Editor.ts index adcd76ba..cd9912b8 100644 --- a/projects/ngx-editor/src/lib/Editor.ts +++ b/projects/ngx-editor/src/lib/Editor.ts @@ -1,4 +1,4 @@ -import { Schema } from 'prosemirror-model'; +import { ParseOptions, Schema } from 'prosemirror-model'; import { EditorState, Plugin, Transaction } from 'prosemirror-state'; import { EditorProps, EditorView } from 'prosemirror-view'; import { Observable, Subject } from 'rxjs'; @@ -26,6 +26,7 @@ interface Options { features?: EditorFeatures; handleScrollToSelection?: EditorProps['handleScrollToSelection']; linkValidationPattern?: string; + parseOptions?:ParseOptions; } interface EditorFeatures { @@ -107,7 +108,7 @@ class Editor { const { content = null, nodeViews } = options; const { history = true, keyboardShortcuts = true, inputRules = true } = options; - const doc = parseContent(content, schema); + const doc = parseContent(content, schema, options.parseOptions); const plugins: Plugin[] = options.plugins ?? []; const attributes: EditorProps['attributes'] = options.attributes ?? {}; @@ -139,7 +140,7 @@ class Editor { const { state } = this.view; const { tr, doc } = state; - const newDoc = parseContent(content, this.schema); + const newDoc = parseContent(content, this.schema, this.options.parseOptions); tr.replaceWith(0, state.doc.content.size, newDoc); diff --git a/projects/ngx-editor/src/lib/parsers.ts b/projects/ngx-editor/src/lib/parsers.ts index 0498ec47..3208a29e 100644 --- a/projects/ngx-editor/src/lib/parsers.ts +++ b/projects/ngx-editor/src/lib/parsers.ts @@ -1,4 +1,4 @@ -import { DOMSerializer, Schema, DOMParser, Node as ProseMirrorNode } from 'prosemirror-model'; +import { DOMSerializer, Schema, DOMParser, Node as ProseMirrorNode, ParseOptions } from 'prosemirror-model'; import defaultSchema from './schema'; import { HTML, isHtml } from './trustedTypesUtil'; @@ -24,16 +24,20 @@ export const toHTML = (json: Record, inputSchema?: Schema): string return div.innerHTML; }; -export const toDoc = (html: HTML, inputSchema?: Schema): Record => { +export const toDoc = (html: HTML, inputSchema?: Schema, options?:ParseOptions): Record => { const schema = inputSchema ?? defaultSchema; const el = document.createElement('div'); el.innerHTML = html as any; - return DOMParser.fromSchema(schema).parse(el).toJSON(); + return DOMParser.fromSchema(schema).parse(el, options).toJSON(); }; -export const parseContent = (value: HTML | Record | null, schema: Schema): ProseMirrorNode => { +export const parseContent = ( + value: HTML | Record | null, + schema: Schema, + options?: ParseOptions, +): ProseMirrorNode => { if (!value) { return schema.nodeFromJSON(emptyDoc); } @@ -42,6 +46,6 @@ export const parseContent = (value: HTML | Record | null, schema: S return schema.nodeFromJSON(value); } - const docJson = toDoc(value as HTML, schema); + const docJson = toDoc(value as HTML, schema, options); return schema.nodeFromJSON(docJson); };