OverviewFilesCommitsIssuesDocsReleasesPeers

holepunchto-deps / @holepunchto / keet-editor / src / extensions / file-image.js

25 lines · javascript ref:
import { nodeInputRule } from '@tiptap/core'

import { FileBase } from './file-base.js'
import { NODE_TYPE } from '../constants.js'

const IMAGE_INPUT_REGEX = /!\[(.+|:?)\]\((\S+)(?:(?:\s+)["'](\S+)["'])?\)/

export const FileImage = FileBase.extend({
  name: NODE_TYPE.FILE_IMAGE,

  addInputRules () {
    return [
      nodeInputRule({
        find: IMAGE_INPUT_REGEX,
        type: this.type,
        getAttributes: (match) => {
          const [, alt, src, title] = match
          return { src, alt, title }
        }
      })
    ]
  }

})