2017-05-08 12:44:02 +03:00
|
|
|
import {
|
|
|
|
replaceWithHeaderSection,
|
|
|
|
replaceWithListSection
|
|
|
|
} from 'mobiledoc-kit/editor/text-input-handlers';
|
|
|
|
|
|
|
|
// Text expansions watch text entry events and will look for matches, replacing
|
|
|
|
// the matches with additional markup, atoms, or cards
|
|
|
|
// https://github.com/bustlelabs/mobiledoc-kit#responding-to-text-input
|
2017-02-27 07:44:15 +03:00
|
|
|
|
|
|
|
export default function (editor) {
|
2017-05-08 12:44:02 +03:00
|
|
|
// We don't want to run all our content rules on every text entry event,
|
|
|
|
// instead we check to see if this text entry event could match a content
|
|
|
|
// rule, and only then run the rules. Right now we only want to match
|
|
|
|
// content ending with *, _, ), ~, and `. This could increase as we support
|
|
|
|
// more markdown.
|
2017-02-27 07:44:15 +03:00
|
|
|
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.onTextInput({
|
|
|
|
name: 'inline_markdown',
|
|
|
|
match: /[*_)~`]$/,
|
|
|
|
run(postEditor, matches) {
|
|
|
|
let text = postEditor.range.head.section.textUntil(postEditor.range.head);
|
|
|
|
|
|
|
|
switch (matches[0]) {
|
|
|
|
case '*':
|
|
|
|
matchStrongStar(postEditor, text);
|
|
|
|
matchEmStar(postEditor, text);
|
|
|
|
break;
|
|
|
|
case '_':
|
|
|
|
matchStrongUl(postEditor, text);
|
|
|
|
matchEmUl(postEditor, text);
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
matchLink(postEditor, text);
|
|
|
|
matchImage(postEditor, text);
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
matchStrikethrough(postEditor, text);
|
|
|
|
break;
|
|
|
|
case '`':
|
|
|
|
matchMarkdown(postEditor, text);
|
|
|
|
break;
|
2017-02-27 07:44:15 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
});
|
2017-02-27 07:44:15 +03:00
|
|
|
|
|
|
|
// block level markdown
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.onTextInput({
|
|
|
|
name: 'dashul',
|
|
|
|
match: /^- $/,
|
|
|
|
run(editor) {
|
|
|
|
replaceWithListSection(editor, 'ul');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.onTextInput({
|
|
|
|
name: 'blockquote',
|
|
|
|
match: /^> $/,
|
|
|
|
run(editor) {
|
|
|
|
replaceWithHeaderSection(editor, 'blockquote');
|
|
|
|
}
|
|
|
|
});
|
2017-02-27 07:44:15 +03:00
|
|
|
|
|
|
|
// inline matches
|
|
|
|
function matchStrongStar(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
let {range} = editor;
|
2017-02-27 07:44:15 +03:00
|
|
|
let matches = text.match(/\*\*(.+?)\*\*$/);
|
2017-03-02 19:51:57 +03:00
|
|
|
if (matches) {
|
2017-02-27 07:44:15 +03:00
|
|
|
range = range.extend(-(matches[0].length));
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
let bold = postEditor.builder.createMarkup('strong');
|
|
|
|
let nextPosition = postEditor.insertTextWithMarkup(position, matches[1], [bold]);
|
|
|
|
postEditor.insertTextWithMarkup(nextPosition, '', []);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
2017-02-27 07:44:15 +03:00
|
|
|
function matchStrongUl(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
let {range} = editor;
|
2017-02-27 07:44:15 +03:00
|
|
|
let matches = text.match(/__(.+?)__$/);
|
2017-03-02 19:51:57 +03:00
|
|
|
if (matches) {
|
2017-02-27 07:44:15 +03:00
|
|
|
range = range.extend(-(matches[0].length));
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
let bold = postEditor.builder.createMarkup('strong');
|
|
|
|
let nextPosition = postEditor.insertTextWithMarkup(position, matches[1], [bold]);
|
|
|
|
postEditor.insertTextWithMarkup(nextPosition, '', []);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
2017-02-27 07:44:15 +03:00
|
|
|
function matchEmStar(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
let {range} = editor;
|
2018-01-05 18:38:23 +03:00
|
|
|
let matches = text.match(/(^|[^*])\*([^*].*?)\*$/);
|
2017-03-02 19:51:57 +03:00
|
|
|
if (matches) {
|
2017-02-27 07:44:15 +03:00
|
|
|
let match = matches[0][0] === '*' ? matches[0] : matches[0].substr(1);
|
|
|
|
range = range.extend(-(match.length));
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
let em = postEditor.builder.createMarkup('em');
|
|
|
|
let nextPosition = postEditor.insertTextWithMarkup(position, matches[2], [em]);
|
|
|
|
postEditor.insertTextWithMarkup(nextPosition, '', []);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
2017-02-27 07:44:15 +03:00
|
|
|
function matchEmUl(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
let {range} = editor;
|
2017-02-27 07:44:15 +03:00
|
|
|
let matches = text.match(/(^|[^_])_([^_].+?)_$/);
|
2017-03-02 19:51:57 +03:00
|
|
|
if (matches) {
|
2017-02-27 07:44:15 +03:00
|
|
|
let match = matches[0][0] === '_' ? matches[0] : matches[0].substr(1);
|
|
|
|
range = range.extend(-(match.length));
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
let em = postEditor.builder.createMarkup('em');
|
|
|
|
let nextPosition = postEditor.insertTextWithMarkup(position, matches[2], [em]);
|
|
|
|
postEditor.insertTextWithMarkup(nextPosition, '', []);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
2017-02-27 07:44:15 +03:00
|
|
|
function matchLink(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
let {range} = editor;
|
2017-02-27 07:44:15 +03:00
|
|
|
let matches = text.match(/(^|[^!])\[(.*?)\]\((.*?)\)$/);
|
2017-03-02 19:51:57 +03:00
|
|
|
if (matches) {
|
2017-02-27 07:44:15 +03:00
|
|
|
let url = matches[3];
|
|
|
|
let text = matches[2];
|
|
|
|
let match = matches[0][0] === '[' ? matches[0] : matches[0].substr(1);
|
|
|
|
range = range.extend(-match.length);
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
let a = postEditor.builder.createMarkup('a', {href: url});
|
|
|
|
let nextPosition = postEditor.insertTextWithMarkup(position, text, [a]);
|
|
|
|
postEditor.insertTextWithMarkup(nextPosition, '', []); // insert the un-marked-up space
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
2017-02-27 07:44:15 +03:00
|
|
|
function matchImage(editor, text) {
|
|
|
|
let matches = text.match(/!\[(.*?)\]\((.*?)\)$/);
|
2017-03-02 19:51:57 +03:00
|
|
|
if (matches) {
|
2017-02-27 07:44:15 +03:00
|
|
|
let img = matches[2];
|
|
|
|
let alt = matches[1];
|
|
|
|
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.run((postEditor) => {
|
2017-03-24 13:03:52 +03:00
|
|
|
let card = postEditor.builder.createCardSection('card-image', {pos: 'top', img, alt});
|
2018-01-05 18:38:23 +03:00
|
|
|
|
|
|
|
editor.range.extend(-(matches[0].length));
|
2017-02-27 07:44:15 +03:00
|
|
|
postEditor.replaceSection(editor.range.headSection, card);
|
2018-01-05 18:38:23 +03:00
|
|
|
|
2017-04-11 12:57:52 +03:00
|
|
|
if (!editor.range.headSection.next) {
|
|
|
|
let newSection = editor.builder.createMarkupSection('p');
|
|
|
|
postEditor.insertSectionAtEnd(newSection);
|
|
|
|
}
|
2017-02-27 07:44:15 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
2017-02-27 07:44:15 +03:00
|
|
|
function matchStrikethrough(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
let {range} = editor;
|
2017-02-27 07:44:15 +03:00
|
|
|
let matches = text.match(/~~(.+?)~~$/);
|
2017-03-02 19:51:57 +03:00
|
|
|
if (matches) {
|
2017-02-27 07:44:15 +03:00
|
|
|
range = range.extend(-(matches[0].length));
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
let s = postEditor.builder.createMarkup('s');
|
|
|
|
let nextPosition = postEditor.insertTextWithMarkup(position, matches[1], [s]);
|
|
|
|
postEditor.insertTextWithMarkup(nextPosition, '', []); // insert the un-marked-up space
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
2017-02-27 07:44:15 +03:00
|
|
|
function matchMarkdown(editor, text) {
|
|
|
|
let matches = text.match(/```([\s\S]*?)```$/);
|
2017-03-02 19:51:57 +03:00
|
|
|
if (matches) {
|
2017-02-27 07:44:15 +03:00
|
|
|
let code = matches[0];
|
2018-01-05 18:38:23 +03:00
|
|
|
|
2017-03-02 19:51:57 +03:00
|
|
|
editor.run((postEditor) => {
|
2017-03-24 13:03:52 +03:00
|
|
|
let card = postEditor.builder.createCardSection('card-markdown', {pos: 'top', markdown: code});
|
2018-01-05 18:38:23 +03:00
|
|
|
|
|
|
|
editor.range.extend(-(matches[0].length));
|
2017-02-27 07:44:15 +03:00
|
|
|
postEditor.replaceSection(editor.range.headSection, card);
|
2018-01-05 18:38:23 +03:00
|
|
|
|
2017-04-11 12:57:52 +03:00
|
|
|
if (!editor.range.headSection.next) {
|
|
|
|
let newSection = editor.builder.createMarkupSection('p');
|
|
|
|
postEditor.insertSectionAtEnd(newSection);
|
|
|
|
}
|
2017-02-27 07:44:15 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|