diff --git a/wax-prosemirror-services/src/LinkService/LinkInputRule.js b/wax-prosemirror-services/src/LinkService/LinkInputRule.js new file mode 100644 index 0000000000000000000000000000000000000000..b549f3cddbea4213902e92c7efdf20cba85cb0a2 --- /dev/null +++ b/wax-prosemirror-services/src/LinkService/LinkInputRule.js @@ -0,0 +1,36 @@ +import { InputRule } from 'prosemirror-inputrules'; + +const linkRule = markType => { + return MarkInputRule( + /(?:(?:(https|http|ftp)+):\/\/)?(?:\S+(?::\S*)?(@))?(?:(?:([a-z0-9][a-z0-9\-]*)?[a-z0-9]+)(?:\.(?:[a-z0-9\-])*[a-z0-9]+)*(?:\.(?:[a-z]{2,})(:\d{1,5})?))(?:\/[^\s]*)?\s$/i, + markType, + match => ({ type: match[2] === '@' ? 'email' : 'uri' }), + ); +}; + +const MarkInputRule = (regexp, markType, getAttrs) => { + return new InputRule(regexp, (state, match, start, end) => { + const $start = state.doc.resolve(start); + const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; + if (!$start.parent.type.allowsMarkType(markType)) return null; + const linkString = match[0].substring(0, match[0].length - 1); + + const linkAttrs = + attrs.type === 'email' + ? { href: `mailto:${linkString}` } + : { href: linkString, target: '_blank' }; + const oLink = markType.create(linkAttrs); + + const tr = state.tr + .addMark(start, end, oLink) + .insertText(' ', start + linkString.length); + // .removeMark( + // start + linkString.length, + // start + linkString.length, + // markType, + // ); + return tr; + }); +}; + +export default linkRule; diff --git a/wax-prosemirror-services/src/RulesService/Rules.js b/wax-prosemirror-services/src/RulesService/Rules.js index 466bf45fc459a320ec127683e8a4ea63d2ba75b4..f6cf3e1e9a6df1d8a26c0112af3392fc9a845ced 100644 --- a/wax-prosemirror-services/src/RulesService/Rules.js +++ b/wax-prosemirror-services/src/RulesService/Rules.js @@ -9,6 +9,7 @@ import { // TODO add through service. import inlineInputRule from '../MathService/InlineInputRule'; import blockInputRule from '../MathService/BlockInputRule'; +import linkRule from '../LinkService/LinkInputRule'; @injectable() class Rules { @@ -33,6 +34,7 @@ class Rules { return [ ...smartQuotes, // > blockquote + linkRule(this.schema.marks.link), wrappingInputRule(/^\s*>\s$/, this.schema.nodes.blockquote), // 1. ordered list