Skip to content
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
4 changes: 2 additions & 2 deletions src/extensions/base/BaseSchema/BaseSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {builders} from 'prosemirror-test-builder';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSchema} from './index';
import {BaseNode, BaseSchemaSpecs} from './BaseSchemaSpecs';

const {schema, parser, serializer} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSchema, {}),
extensions: (builder) => builder.use(BaseSchemaSpecs, {}),
}).buildDeps();

const {doc, p} = builders(schema, {
Expand Down
61 changes: 61 additions & 0 deletions src/extensions/base/BaseSchema/BaseSchemaSpecs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type {NodeSpec} from 'prosemirror-model';
import type {ExtensionAuto} from '../../../../core';
import {nodeTypeFactory} from '../../../../utils/schema';

export enum BaseNode {
Doc = 'doc',
Text = 'text',
Paragraph = 'paragraph',
}

export const pType = nodeTypeFactory(BaseNode.Paragraph);

export type BaseSchemaSpecsOptions = {
paragraphPlaceholder?: NonNullable<NodeSpec['placeholder']>['content'];
};

export const BaseSchemaSpecs: ExtensionAuto<BaseSchemaSpecsOptions> = (builder, opts) => {
const {paragraphPlaceholder} = opts;

builder
.addNode(BaseNode.Doc, () => ({
spec: {
content: 'block+',
},
fromYfm: {tokenSpec: {name: BaseNode.Doc, type: 'block', ignore: true}},
toYfm: () => {
throw new Error('Unexpected toYfm() call on doc node');
},
}))
.addNode(BaseNode.Text, () => ({
spec: {
group: 'inline',
},
fromYfm: {tokenSpec: {name: BaseNode.Text, type: 'node', ignore: true}},
toYfm: (state, node, parent) => {
const {escapeText} = parent.type.spec;
state.text(node.text, escapeText ?? !state.isAutolink);
},
}))
.addNode(BaseNode.Paragraph, () => ({
spec: {
content: 'inline*',
group: 'block',
parseDOM: [{tag: 'p'}],
toDOM() {
return ['p', 0];
},
placeholder: paragraphPlaceholder
? {
content: paragraphPlaceholder,
alwaysVisible: false,
}
: undefined,
},
fromYfm: {tokenSpec: {name: BaseNode.Paragraph, type: 'block'}},
toYfm: (state, node) => {
state.renderInline(node);
state.closeBlock(node);
},
}));
};
58 changes: 5 additions & 53 deletions src/extensions/base/BaseSchema/index.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,24 @@
import type {NodeSpec} from 'prosemirror-model';
import type {Command} from 'prosemirror-state';
import {setBlockType} from 'prosemirror-commands';
import {hasParentNodeOfType} from 'prosemirror-utils';
import type {Action, ExtensionAuto} from '../../../core';
import {nodeTypeFactory} from '../../../utils/schema';
import {BaseSchemaSpecs, BaseSchemaSpecsOptions, pType} from './BaseSchemaSpecs';

export enum BaseNode {
Doc = 'doc',
Text = 'text',
Paragraph = 'paragraph',
}
export {BaseNode, pType} from './BaseSchemaSpecs';

export const pType = nodeTypeFactory(BaseNode.Paragraph);
const pAction = 'toParagraph';

export const toParagraph: Command = (state, dispatch) =>
setBlockType(pType(state.schema))(state, dispatch);

export type BaseSchemaOptions = {
export type BaseSchemaOptions = BaseSchemaSpecsOptions & {
paragraphKey?: string | null;
paragraphPlaceholder?: NonNullable<NodeSpec['placeholder']>['content'];
};

export const BaseSchema: ExtensionAuto<BaseSchemaOptions> = (builder, opts) => {
const {paragraphKey, paragraphPlaceholder} = opts;

builder
.addNode(BaseNode.Doc, () => ({
spec: {
content: 'block+',
},
fromYfm: {tokenSpec: {name: BaseNode.Doc, type: 'block', ignore: true}},
toYfm: () => {
throw new Error('Unexpected toYfm() call on doc node');
},
}))
.addNode(BaseNode.Text, () => ({
spec: {
group: 'inline',
},
fromYfm: {tokenSpec: {name: BaseNode.Text, type: 'node', ignore: true}},
toYfm: (state, node, parent) => {
const {escapeText} = parent.type.spec;
state.text(node.text, escapeText ?? !state.isAutolink);
},
}))
.addNode(BaseNode.Paragraph, () => ({
spec: {
content: 'inline*',
group: 'block',
parseDOM: [{tag: 'p'}],
toDOM() {
return ['p', 0];
},
placeholder: paragraphPlaceholder
? {
content: paragraphPlaceholder,
alwaysVisible: false,
}
: undefined,
},
fromYfm: {tokenSpec: {name: BaseNode.Paragraph, type: 'block'}},
toYfm: (state, node) => {
state.renderInline(node);
state.closeBlock(node);
},
}));
builder.use(BaseSchemaSpecs, opts);

const {paragraphKey} = opts;
if (paragraphKey) {
builder.addKeymap(({schema}) => ({[paragraphKey]: setBlockType(pType(schema))}));
}
Expand Down
13 changes: 13 additions & 0 deletions src/extensions/base/specs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type {ExtensionAuto} from '../../core';

import {BaseSchemaSpecs, BaseSchemaSpecsOptions} from './BaseSchema/BaseSchemaSpecs';

export * from './BaseSchema/BaseSchemaSpecs';

export type BaseSpecPresetOptions = {
baseSchema?: BaseSchemaSpecsOptions;
};

export const BaseSpecsPreset: ExtensionAuto<BaseSpecPresetOptions> = (builder, opts) => {
builder.use(BaseSchemaSpecs, opts.baseSchema ?? {});
};
7 changes: 1 addition & 6 deletions src/extensions/behavior/Placeholder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ import {cn} from '../../../classname';
import type {ExtensionAuto} from '../../../core';
import {isNodeEmpty} from '../../../utils/nodes';
import {isTextSelection} from '../../../utils/selection';
import {getPlaceholderContent} from '../../../utils/placeholder';

import './index.scss';

export const getPlaceholderContent = (node: Node, parent?: Node | null) => {
const content = node.type.spec.placeholder?.content || '';

return typeof content === 'function' ? content(node, parent) : content;
};

const getPlaceholderPluginKeys = (schema: Schema) => {
const pluginKeys = [];
for (const node in schema.nodes) {
Expand Down
8 changes: 4 additions & 4 deletions src/extensions/markdown/Blockquote/Blockquote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import {builders} from 'prosemirror-test-builder';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {parseDOM} from '../../../../tests/parse-dom';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSchema} from '../../base/BaseSchema';
import {blockquote, Blockquote} from './index';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {blockquoteNodeName, BlockquoteSpecs} from './BlockquoteSpecs';

const {schema, parser, serializer} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSchema, {}).use(Blockquote, {}),
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(BlockquoteSpecs),
}).buildDeps();

const {doc, p, q} = builders(schema, {
doc: {nodeType: BaseNode.Doc},
p: {nodeType: BaseNode.Paragraph},
q: {nodeType: blockquote},
q: {nodeType: blockquoteNodeName},
}) as PMTestBuilderResult<'doc' | 'p' | 'q'>;

const {same} = createMarkupChecker({parser, serializer});
Expand Down
23 changes: 23 additions & 0 deletions src/extensions/markdown/Blockquote/BlockquoteSpecs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {ExtensionAuto} from '../../../../core';
import {nodeTypeFactory} from '../../../../utils/schema';

export const blockquoteNodeName = 'blockquote';
export const blockquoteType = nodeTypeFactory(blockquoteNodeName);

export const BlockquoteSpecs: ExtensionAuto = (builder) => {
builder.addNode(blockquoteNodeName, () => ({
spec: {
content: 'block+',
group: 'block',
defining: true,
parseDOM: [{tag: 'blockquote'}],
toDOM() {
return ['blockquote', 0];
},
},
fromYfm: {tokenSpec: {name: blockquoteNodeName, type: 'block'}},
toYfm: (state, node) => {
state.wrapBlock('> ', null, node, () => state.renderContent(node));
},
}));
};
9 changes: 6 additions & 3 deletions src/extensions/markdown/Blockquote/const.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {nodeTypeFactory} from '../../../utils/schema';
import {blockquoteNodeName, blockquoteType} from './BlockquoteSpecs';

export const blockquote = 'blockquote';
export const bqType = nodeTypeFactory(blockquote);
export {blockquoteNodeName, blockquoteType} from './BlockquoteSpecs';
/** @deprecated Use `blockquoteNodeName` instead */
export const blockquote = blockquoteNodeName;
/** @deprecated Use `blockquoteType` instead */
export const bqType = blockquoteType;
26 changes: 6 additions & 20 deletions src/extensions/markdown/Blockquote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,31 @@ import {wrappingInputRule} from 'prosemirror-inputrules';
import {hasParentNodeOfType} from 'prosemirror-utils';
import type {Action, ExtensionAuto} from '../../../core';
import {selectQuoteBeforeCursor, liftFromQuote, toggleQuote} from './commands';
import {blockquote, bqType} from './const';
import {BlockquoteSpecs, blockquoteType} from './BlockquoteSpecs';

export {blockquote};
export {blockquote, blockquoteNodeName, blockquoteType} from './const';
const bqAction = 'quote';

export type BlockquoteOptions = {
qouteKey?: string | null;
};

export const Blockquote: ExtensionAuto<BlockquoteOptions> = (builder, opts) => {
builder.addNode(blockquote, () => ({
spec: {
content: 'block+',
group: 'block',
defining: true,
parseDOM: [{tag: 'blockquote'}],
toDOM() {
return ['blockquote', 0];
},
},
fromYfm: {tokenSpec: {name: blockquote, type: 'block'}},
toYfm: (state, node) => {
state.wrapBlock('> ', null, node, () => state.renderContent(node));
},
}));
builder.use(BlockquoteSpecs);

if (opts?.qouteKey) {
const {qouteKey} = opts;
builder.addKeymap(({schema}) => ({[qouteKey]: wrapIn(bqType(schema))}));
builder.addKeymap(({schema}) => ({[qouteKey]: wrapIn(blockquoteType(schema))}));
}

builder.addKeymap(() => ({
Backspace: chainCommands(liftFromQuote, selectQuoteBeforeCursor),
}));

builder.addInputRules(({schema}) => ({rules: [blockQuoteRule(bqType(schema))]}));
builder.addInputRules(({schema}) => ({rules: [blockQuoteRule(blockquoteType(schema))]}));

builder.addAction(bqAction, ({schema}) => {
const bq = bqType(schema);
const bq = blockquoteType(schema);
return {
isActive: (state) => hasParentNodeOfType(bq)(state.selection),
isEnable: toggleQuote,
Expand Down
8 changes: 4 additions & 4 deletions src/extensions/markdown/Bold/Bold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import {builders} from 'prosemirror-test-builder';
import {createMarkupChecker} from '../../../../tests/sameMarkup';
import {parseDOM} from '../../../../tests/parse-dom';
import {ExtensionsManager} from '../../../core';
import {BaseNode, BaseSchema} from '../../base/BaseSchema';
import {bold, Bold} from './index';
import {BaseNode, BaseSpecsPreset} from '../../base/specs';
import {boldMarkName, BoldSpecs} from './BoldSpecs';

const {schema, parser, serializer} = new ExtensionsManager({
extensions: (builder) => builder.use(BaseSchema, {}).use(Bold, {}),
extensions: (builder) => builder.use(BaseSpecsPreset, {}).use(BoldSpecs),
}).buildDeps();

const {doc, p, b} = builders(schema, {
doc: {nodeType: BaseNode.Doc},
p: {nodeType: BaseNode.Paragraph},
b: {nodeType: bold},
b: {nodeType: boldMarkName},
}) as PMTestBuilderResult<'doc' | 'p', 'b'>;

const {same} = createMarkupChecker({parser, serializer});
Expand Down
30 changes: 30 additions & 0 deletions src/extensions/markdown/Bold/BoldSpecs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type {ExtensionAuto} from '../../../../core';
import {markTypeFactory} from '../../../../utils/schema';

export const boldMarkName = 'strong';
export const boldType = markTypeFactory(boldMarkName);

export const BoldSpecs: ExtensionAuto = (builder) => {
builder.addMark(boldMarkName, () => ({
spec: {
parseDOM: [
{tag: 'b'},
{tag: 'strong'},
{
style: 'font-weight',
getAttrs: (value) => /^(bold(er)?|[5-9]\d{2,})$/.test(value as string) && null,
},
],
toDOM() {
return ['strong'];
},
},
fromYfm: {
tokenSpec: {
name: boldMarkName,
type: 'mark',
},
},
toYfm: {open: '**', close: '**', mixable: true, expelEnclosingWhitespace: true},
}));
};
Loading