Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
toEqualRemirrorDocument(actual: ProsemirrorNode, expected: TaggedProsemirrorNode) {
// Because schema is created dynamically, expected value is a function (schema) => PMNode;
// That's why this magic is necessary. It simplifies writing assertions, so
// instead of expect(doc).toEqualRemirrorDocument(doc(p())(schema)) we can just do:
// expect(doc).toEqualRemirrorDocument(doc(p())).
// Also it fixes issues that happens sometimes when actual schema and expected schema
// are different objects, making this case impossible by always using actual schema to create expected node.
if (!isProsemirrorNode(expected) || !isProsemirrorNode(actual)) {
return {
pass: false,
actual,
expected,
name: 'toEqualRemirrorDocument',
message: () => 'Expected both values to be instance of prosemirror-model Node.',
};
}
if (expected.type.schema !== actual.type.schema) {
return {
pass: false,
actual,
expected,
name: 'toEqualRemirrorDocument',
message: () => 'Expected both values to be using the same schema.',
const createInitialContent = ({ content, schema }: CreateInitialContentParams): Content => {
if (isString(content)) {
return {
markdown: content,
wysiwyg: fromMarkdown(content, schema),
};
}
if (isProsemirrorNode(content)) {
return {
markdown: toMarkdown(content),
wysiwyg: content,
};
}
if (!isObjectNode(content)) {
throw new Error('Invalid content passed into the editor');
}
const pmNode = createDocumentNode({ content, schema });
return {
markdown: toMarkdown(pmNode),
wysiwyg: pmNode,
};
toContainRemirrorDocument(state: EditorState, expected: TaggedProsemirrorNode) {
if (!isProsemirrorNode(expected)) {
return {
pass: false,
actual: state,
expected,
name: 'toContainRemirrorDocument',
message: () => 'The expected value should be an instance of ProsemirrorNode.',
};
}
if (!isEditorState(state)) {
return {
pass: false,
actual: state,
expected,
name: 'toContainRemirrorDocument',
message: () => 'Expected the value passed in to be an EditorState',
const isTaggedProsemirrorNode = (value: unknown): value is TaggedProsemirrorNode =>
isProsemirrorNode(value) && !isTagTracker(value);
return node.reduce((acc, next) => {
if (isProsemirrorNode(next)) {
acc.push(PMNode.fromJSON(schema, next.toJSON()));
}
return acc;
}, [] as PMNode[]);
}
export const isTaggedNode = (val: unknown): val is TaggedProsemirrorNode =>
isProsemirrorNode(val) && isObject((val as any).tags);
export const clean = ({ schema, content }: CleanParams) => {
const node = content;
if (Array.isArray(node)) {
return node.reduce((acc, next) => {
if (isProsemirrorNode(next)) {
acc.push(PMNode.fromJSON(schema, next.toJSON()));
}
return acc;
}, [] as PMNode[]);
}
return isProsemirrorNode(node) ? PMNode.fromJSON(schema, node.toJSON()) : undefined;
};