Skip to content
Snippets Groups Projects
Commit 7a1afff6 authored by chris's avatar chris
Browse files

link intput rule

parent feb91fd8
No related branches found
No related tags found
1 merge request!387Short cuts
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;
......@@ -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
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment