diff --git a/packages/core/src/commands/insertContentAt.ts b/packages/core/src/commands/insertContentAt.ts index e5174a4c..f346bfb7 100644 --- a/packages/core/src/commands/insertContentAt.ts +++ b/packages/core/src/commands/insertContentAt.ts @@ -20,7 +20,11 @@ declare module '@tiptap/core' { export const insertContentAt: RawCommands['insertContentAt'] = (position, value) => ({ tr, dispatch, editor }) => { if (dispatch) { - const content = createNodeFromContent(value, editor.schema) + const content = createNodeFromContent(value, editor.schema, { + parseOptions: { + preserveWhitespace: 'full', + }, + }) const { from, to } = typeof position === 'number' ? { from: position, to: position } : position diff --git a/packages/core/src/utilities/elementFromString.ts b/packages/core/src/utilities/elementFromString.ts index 14f711b0..5bbf3d1e 100644 --- a/packages/core/src/utilities/elementFromString.ts +++ b/packages/core/src/utilities/elementFromString.ts @@ -1,3 +1,6 @@ export default function elementFromString(value: string): HTMLElement { - return new window.DOMParser().parseFromString(value, 'text/html').body + // add a wrapper to preserve leading and trailing whitespace + const wrappedValue = `${value}` + + return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body }