Skip to content

Math

The math extension adds support for mathematical equations in your document, including mathInline node for inline expressions and mathBlock node for block expressions. It is renderer agnostic, allowing you to use any math rendering library such as KaTeX or Temml.

'use client'

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

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

import { sampleContent } from '../../sample/sample-doc-tex.ts'

import { defineExtension } from './extension.ts'

interface EditorProps {
  initialContent?: NodeJSON
}

export default function Editor(props: EditorProps) {
  const defaultContent = props.initialContent ?? sampleContent
  const editor = useMemo(() => {
    const extension = defineExtension()
    return createEditor({ extension, defaultContent })
  }, [defaultContent])

  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 dark:border-gray-700 shadow-sm flex flex-col bg-[canvas] text-black dark:text-white">
        <div className="relative w-full flex-1 box-border overflow-y-auto">
          <div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
        </div>
      </div>
    </ProseKit>
  )
}
import Temmlconst Temml: Temml from 'temml'
import { defineMathfunction defineMath(options: MathOptions): MathExtension } from 'prosekit/extensions/math'

const extensionconst extension: MathExtension = defineMathfunction defineMath(options: MathOptions): MathExtension({
  renderMathInlineMathOptions.renderMathInline: RenderMathInline
The function to render the math inline.
: (texttext: string: string, elementelement: HTMLElement: HTMLElement) => {
// Render inline math into the given <span> element Temmlconst Temml: Temml.render
Temml.render(text: string, element: HTMLElement, options: {
    displayMode: boolean;
}): void
(texttext: string, elementelement: HTMLElement, { displayModedisplayMode: boolean: false })
}, renderMathBlockMathOptions.renderMathBlock: RenderMathBlock
The function to render the math block.
: (texttext: string: string, elementelement: HTMLElement: HTMLElement) => {
// Render block math into the given <div> element Temmlconst Temml: Temml.render
Temml.render(text: string, element: HTMLElement, options: {
    displayMode: boolean;
}): void
(texttext: string, elementelement: HTMLElement, { displayModedisplayMode: boolean: true })
}, })

Type $...$ to create an inline math expression.

Type $$ on an empty line and press Enter to create a block-level math expression.