Skip to content

Commit

Permalink
feat: add markdown shortcuts for bold and italics
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Jul 8, 2021
1 parent f36305b commit ab7d175
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
28 changes: 28 additions & 0 deletions projects/ngx-editor/helpers/markInputRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { InputRule } from "prosemirror-inputrules"
import { MarkType } from "prosemirror-model"

export const markInputRule = (regexp: RegExp, markType: MarkType, attrs: Record<string, unknown>): InputRule => {

return new InputRule(regexp, (state, match, start, end) => {

const tr = state.tr

if (match[2]) {
const textStart = start + match[0].indexOf(match[2])
const textEnd = textStart + match[2].length

if (textEnd < end) {
tr.delete(textEnd, end)
}

if (textStart > start) {
tr.delete(start, textStart)
}

end = start + match[2].length
}
return tr.addMark(start, end, markType.create(attrs))
})
}

export default markInputRule
1 change: 1 addition & 0 deletions projects/ngx-editor/helpers/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './getSelectionMarks';
export * from './bubblePosition';
export * from './getSelectionNodes';
export * from './markApplies';
export * from './markInputRule';
14 changes: 13 additions & 1 deletion projects/ngx-editor/src/lib/defaultPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeType, Schema } from 'prosemirror-model';
import { MarkType, NodeType, Schema } from 'prosemirror-model';
import { Plugin } from 'prosemirror-state';
import { keymap } from 'prosemirror-keymap';
import { toggleMark, baseKeymap, chainCommands, exitCode } from 'prosemirror-commands';
Expand All @@ -9,6 +9,8 @@ import {
smartQuotes, emDash, ellipsis, InputRule
} from 'prosemirror-inputrules';

import { markInputRule } from 'ngx-editor/helpers';

interface Options {
history: boolean;
keyboardShortcuts: boolean;
Expand Down Expand Up @@ -70,12 +72,22 @@ const headingRule = (nodeType: NodeType, maxLevel: number): InputRule => {
);
};

const boldRule = (markType: MarkType): InputRule => {
return markInputRule(/(?:^|\s)((?:\*\*|__)((?:[^*_]+))(?:\*\*|__))$/, markType, {})
}

const emRule = (markType: MarkType): InputRule => {
return markInputRule(/(?:^|\s)((?:\*|_)((?:[^*_]+))(?:\*|_))$/, markType, {})
}

// : (Schema) → Plugin
// A set of input rules for creating the basic block quotes, lists,
// code blocks, and heading.
const buildInputRules = (schema: Schema): Plugin => {
const rules = smartQuotes.concat(ellipsis, emDash);

rules.push(boldRule(schema.marks.strong));
rules.push(emRule(schema.marks.em));
rules.push(blockQuoteRule(schema.nodes.blockquote));
rules.push(orderedListRule(schema.nodes.ordered_list));
rules.push(bulletListRule(schema.nodes.bullet_list));
Expand Down

0 comments on commit ab7d175

Please sign in to comment.