diff --git a/src/extensions/markdown/Breaks/BreaksSpecs/index.ts b/src/extensions/markdown/Breaks/BreaksSpecs/index.ts index 404a2c77..ede3543c 100644 --- a/src/extensions/markdown/Breaks/BreaksSpecs/index.ts +++ b/src/extensions/markdown/Breaks/BreaksSpecs/index.ts @@ -1,3 +1,4 @@ +import type {ParseRule} from 'prosemirror-model'; import type {ExtensionAuto} from '../../../../core'; import {nodeTypeFactory} from '../../../../utils/schema'; @@ -9,7 +10,18 @@ export enum BreakNodeName { export const hbType = nodeTypeFactory(BreakNodeName.HardBreak); export const sbType = nodeTypeFactory(BreakNodeName.SoftBreak); -export const BreaksSpecs: ExtensionAuto = (builder) => { +export type BreaksSpecsOptions = { + /** + * @default 'hard' + */ + preferredBreak?: 'hard' | 'soft'; +}; + +export const BreaksSpecs: ExtensionAuto = (builder, opts) => { + const {preferredBreak = 'hard'} = opts; + + const parseDOM: ParseRule[] = [{tag: 'br'}]; + builder.addNode(BreakNodeName.HardBreak, () => ({ spec: { inline: true, @@ -17,7 +29,7 @@ export const BreaksSpecs: ExtensionAuto = (builder) => { marks: '', isBreak: true, selectable: false, - parseDOM: [{tag: 'br'}], + parseDOM: preferredBreak === 'hard' ? parseDOM : undefined, toDOM() { return ['br']; }, @@ -44,6 +56,7 @@ export const BreaksSpecs: ExtensionAuto = (builder) => { marks: '', isBreak: true, selectable: false, + parseDOM: preferredBreak === 'soft' ? parseDOM : undefined, toDOM() { return ['br']; }, diff --git a/src/extensions/markdown/Breaks/index.ts b/src/extensions/markdown/Breaks/index.ts index a566c552..08d4514e 100644 --- a/src/extensions/markdown/Breaks/index.ts +++ b/src/extensions/markdown/Breaks/index.ts @@ -3,7 +3,7 @@ import {chainCommands, exitCode} from 'prosemirror-commands'; import {logger} from '../../../logger'; import type {ExtensionAuto, Keymap} from '../../../core'; import {isMac} from '../../../utils/platform'; -import {BreaksSpecs, hbType, sbType} from './BreaksSpecs'; +import {BreaksSpecs, BreaksSpecsOptions, hbType, sbType} from './BreaksSpecs'; export {BreaksSpecs, BreakNodeName, hbType, sbType} from './BreaksSpecs'; @@ -17,8 +17,6 @@ export type BreaksOptions = { }; export const Breaks: ExtensionAuto = (builder, opts) => { - builder.use(BreaksSpecs); - let preferredBreak: 'hard' | 'soft'; if (builder.context.has('breaks')) { preferredBreak = builder.context.get('breaks') ? 'soft' : 'hard'; @@ -29,6 +27,8 @@ export const Breaks: ExtensionAuto = (builder, opts) => { ); } + builder.use(BreaksSpecs, {preferredBreak}); + builder.addKeymap(({schema}) => { const cmd = addBr((preferredBreak === 'soft' ? sbType : hbType)(schema)); const keys: Keymap = { diff --git a/src/extensions/markdown/specs.ts b/src/extensions/markdown/specs.ts index a8501761..439d39d7 100644 --- a/src/extensions/markdown/specs.ts +++ b/src/extensions/markdown/specs.ts @@ -15,7 +15,7 @@ import {Html} from './Html'; import {TableSpecs} from './Table/TableSpecs'; import {ImageSpecs} from './Image/ImageSpecs'; import {ListsSpecs} from './Lists/ListsSpecs'; -import {BreaksSpecs} from './Breaks/BreaksSpecs'; +import {BreaksSpecs, BreaksSpecsOptions} from './Breaks/BreaksSpecs'; import {BlockquoteSpecs} from './Blockquote/BlockquoteSpecs'; import {DeflistSpecs, DeflistSpecsOptions} from './Deflist/DeflistSpecs'; import {HeadingSpecs, HeadingSpecsOptions} from './Heading/HeadingSpecs'; @@ -63,6 +63,7 @@ export const MarkdownMarksSpecsPreset: ExtensionAuto