Commit 295b2ad2 authored by Enrique Alcantara's avatar Enrique Alcantara Committed by Enrique Alcántara

Add an input rule for creating links

parent 03664283
import { markInputRule } from '@tiptap/core';
import { Link } from '@tiptap/extension-link';
import { defaultMarkdownSerializer } from 'prosemirror-markdown/src/to_markdown';
export const tiptapExtension = Link.configure({
openOnClick: false,
});
const insertLinkInputRuleRegExp = /(?:^|\s)\[([\w|\s|-]+)\]\(.+?\)$/gm;
export const tiptapExtension = Link
.extend({
addInputRules() {
return [
markInputRule(insertLinkInputRuleRegExp, this.type, (match) => {
const extractHrefRegExp = /\[([\w|\s|-]+)\]\((?<href>.+?)\)$/gm;
return extractHrefRegExp.exec(match[0]).groups;
}),
];
}
})
.configure({
openOnClick: false,
});
export const serializer = defaultMarkdownSerializer.marks.link;
import { tiptapExtension as Link } from '~/content_editor/extensions/link';
import { createTestEditor } from '../test_utils';
describe('content_editor/extensions/link', () => {
let tiptapEditor;
beforeEach(() => {
tiptapEditor = createTestEditor({ extensions: [Link] });
});
it.each`
input
${'[gitlab](https://gitlab.com)'}
`('creates a link when the input rule matches $input', ({ input }) => {
const { view } = tiptapEditor;
const { selection } = view.state;
tiptapEditor.chain().insertContent(input).run();
/**
* Calls the event handler that executes the input rule
* https://github.com/ProseMirror/prosemirror-inputrules/blob/master/src/inputrules.js#L65
* */
view.someProp('handleTextInput', (f) => f(view, selection.from, selection.to, input));
const serializedDoc = tiptapEditor.getJSON();
console.log(serializedDoc.content[0].content[0])
});
});
Markdown is supported
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