Skip to content
tsx
import 'prosekit/basic/style.css'

import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { defineExtension } from './extension'

export default function Editor() {
  const editor = useMemo(() => {
    return createEditor({ extension: defineExtension() })
  }, [])

  return (
    <ProseKit editor={editor}>
      <div className='box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 shadow dark:border-zinc-700 flex flex-col bg-white dark:bg-neutral-900'>
        <div className='relative w-full flex-1 box-border overflow-y-scroll'>
          <div ref={editor.mount} className='ProseMirror box-border min-h-full px-[max(4rem,_calc(50%-20rem))] py-8 outline-none outline-0 [&_span[data-mention="user"]]:text-blue-500 [&_span[data-mention="tag"]]:text-violet-500 [&_pre]:text-white [&_pre]:bg-zinc-800'></div>
        </div>
      </div>
    </ProseKit>
  )
}
ts
import {
  defineBaseCommands,
  defineBaseKeymap,
  defineDoc,
  defineHistory,
  defineParagraph,
  defineText,
  union,
} from 'prosekit/core'
import { defineLinkMarkRule, defineLinkSpec } from 'prosekit/extensions/link'
import { definePlaceholder } from 'prosekit/extensions/placeholder'
import { defineVirtualSelection } from 'prosekit/extensions/virtual-selection'

import { defineIssueLink } from './issue-link'

export function defineExtension() {
  return union(
    defineDoc(),
    defineText(),
    defineParagraph(),
    defineHistory(),
    defineBaseKeymap(),
    defineBaseCommands(),
    defineVirtualSelection(),
    defineLinkSpec(),
    defineLinkMarkRule(),
    definePlaceholder({ placeholder: 'Try typing #123' }),
    defineIssueLink(),
  )
}

export type EditorExtension = ReturnType<typeof defineExtension>
ts
import { defineMarkSpec, union } from 'prosekit/core'
import { defineMarkRule } from 'prosekit/extensions/mark-rule'

export function defineIssueLink() {
  return union(
    defineMarkSpec({
      name: 'issueLink',
      inclusive: false,
      attrs: {
        issueNumber: {},
      },
      toDOM(node) {
        const issueNumber = node.attrs.issueNumber as number
        return [
          'a',
          {
            href: `https://example.com/issues/${issueNumber}`,
            title: `Issue #${issueNumber}`,
          },
          0,
        ]
      },
    }),
    defineMarkRule({
      regex: /#(\d+)/g,
      type: 'issueLink',
      attrs: (match) => {
        return { issueNumber: Number.parseInt(match[1]) }
      },
    }),
  )
}