UNPKG

@atlaskit/editor-plugin-image-upload

Version:

Image upload plugin for @atlaskit/editor-core

29 lines (27 loc) 1.03 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { createRule } from '@atlaskit/editor-common/utils'; import { createPlugin } from '@atlaskit/prosemirror-input-rules'; import { createExternalMediaNode } from '../ui/hooks/utils'; // eslint-disable-next-line require-unicode-regexp var MARKDOWN_IMAGE_REGEX = /!\[(.*)\]\((\S+)\)$/; export function inputRulePlugin(schema, featureFlags) { if (!schema.nodes.media || !schema.nodes.mediaSingle) { return; } // ![something](link) should convert to an image // eslint-disable-next-line require-unicode-regexp var imageRule = createRule(MARKDOWN_IMAGE_REGEX, function (state, match, start, end) { var schema = state.schema; var attrs = { src: match[2], alt: match[1] }; var node = createExternalMediaNode(attrs.src, schema); if (node) { return state.tr.replaceWith(start, end, node); } return null; }); return new SafePlugin(createPlugin('image-upload', [imageRule])); } export default inputRulePlugin;