Skip to content

Commit

Permalink
feat(Breaks): parse <br/> to preffered break
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v committed Oct 31, 2023
1 parent aadf65f commit b28729a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
17 changes: 15 additions & 2 deletions src/extensions/markdown/Breaks/BreaksSpecs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {ParseRule} from 'prosemirror-model';
import type {ExtensionAuto} from '../../../../core';
import {nodeTypeFactory} from '../../../../utils/schema';

Expand All @@ -9,15 +10,26 @@ 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<BreaksSpecsOptions> = (builder, opts) => {
const {preferredBreak = 'hard'} = opts;

const parseDOM: ParseRule[] = [{tag: 'br'}];

builder.addNode(BreakNodeName.HardBreak, () => ({
spec: {
inline: true,
group: 'inline break',
marks: '',
isBreak: true,
selectable: false,
parseDOM: [{tag: 'br'}],
parseDOM: preferredBreak === 'hard' ? parseDOM : undefined,
toDOM() {
return ['br'];
},
Expand All @@ -44,6 +56,7 @@ export const BreaksSpecs: ExtensionAuto = (builder) => {
marks: '',
isBreak: true,
selectable: false,
parseDOM: preferredBreak === 'soft' ? parseDOM : undefined,
toDOM() {
return ['br'];
},
Expand Down
6 changes: 3 additions & 3 deletions src/extensions/markdown/Breaks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -17,8 +17,6 @@ export type BreaksOptions = {
};

export const Breaks: ExtensionAuto<BreaksOptions> = (builder, opts) => {
builder.use(BreaksSpecs);

let preferredBreak: 'hard' | 'soft';
if (builder.context.has('breaks')) {
preferredBreak = builder.context.get('breaks') ? 'soft' : 'hard';
Expand All @@ -29,6 +27,8 @@ export const Breaks: ExtensionAuto<BreaksOptions> = (builder, opts) => {
);
}

builder.use<BreaksSpecsOptions>(BreaksSpecs, {preferredBreak});

builder.addKeymap(({schema}) => {
const cmd = addBr((preferredBreak === 'soft' ? sbType : hbType)(schema));
const keys: Keymap = {
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/markdown/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -63,6 +63,7 @@ export const MarkdownMarksSpecsPreset: ExtensionAuto<MarkdownMarksSpecsPresetOpt

export type MarkdownBlocksSpecsPresetOptions = {
image?: false | Extension;
breaks?: BreaksSpecsOptions;
codeBlock?: CodeBlockSpecsOptions;
deflist?: DeflistSpecsOptions;
heading?: false | Extension | HeadingSpecsOptions;
Expand All @@ -76,7 +77,7 @@ export const MarkdownBlocksSpecsPreset: ExtensionAuto<MarkdownBlocksSpecsPresetO
.use(Html)
.use(ListsSpecs)
.use(TableSpecs)
.use(BreaksSpecs)
.use(BreaksSpecs, opts.breaks ?? {})
.use(BlockquoteSpecs)
.use(HorizontalRuleSpecs)
.use(DeflistSpecs, opts.deflist ?? {})
Expand Down

0 comments on commit b28729a

Please sign in to comment.