Skip to content

Commit

Permalink
feat: add support for parseOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed May 12, 2024
1 parent 7902131 commit 3d5ac24
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/src/content/docs/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```

Expand Down
7 changes: 4 additions & 3 deletions projects/ngx-editor/src/lib/Editor.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -26,6 +26,7 @@ interface Options {
features?: EditorFeatures;
handleScrollToSelection?: EditorProps['handleScrollToSelection'];
linkValidationPattern?: string;
parseOptions?:ParseOptions;
}

interface EditorFeatures {
Expand Down Expand Up @@ -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 ?? {};
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 9 additions & 5 deletions projects/ngx-editor/src/lib/parsers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,16 +24,20 @@ export const toHTML = (json: Record<string, any>, inputSchema?: Schema): string
return div.innerHTML;
};

export const toDoc = (html: HTML, inputSchema?: Schema): Record<string, any> => {
export const toDoc = (html: HTML, inputSchema?: Schema, options?:ParseOptions): Record<string, any> => {
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<string, any> | null, schema: Schema): ProseMirrorNode => {
export const parseContent = (
value: HTML | Record<string, any> | null,
schema: Schema,
options?: ParseOptions,
): ProseMirrorNode => {
if (!value) {
return schema.nodeFromJSON(emptyDoc);
}
Expand All @@ -42,6 +46,6 @@ export const parseContent = (value: HTML | Record<string, any> | 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);
};

0 comments on commit 3d5ac24

Please sign in to comment.