Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Breaks): parse <br/> to preffered break #147

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading