tiptap-markdown

repository·main·Indexed 19 days ago

https://github.com/aguingand/tiptap-markdown

A markdown extension for the Tiptap editor that enables markdown input and output. It allows users to set editor content via markdown strings and export editor state as markdown using `editor.storage.markdown.getMarkdown()`. The library supports custom serialization and parsing through MarkdownNodeSpec and MarkdownMarkSpec, and provides configuration options for HTML support, tight lists, linkification, and clipboard transformations.

Tokens
5.1K
Snippets
21
Records
25
Agent score
68%

What's inside tiptap-markdown

  1. Configure Markdown options in v0.7.0

    main

    In version 0.7.0, markdown configuration options (such as breaks) are no longer passed in the main Editor configuration object. Instead, use the .configure() method on the Markdown extension.

    const editor = new Editor({
       extensions: [
    +    Markdown.configure({
    +      breaks: true,
    +    })
       ],
    -  markdown: {
    -    breaks: true,
    -  },
    })
  2. Implement custom extensions in v0.7.0

    main

    The createMarkdownExtension() function has been removed in version 0.7.0. To add custom markdown serialization and parsing logic to an existing Tiptap Node or Mark, use the Tiptap addStorage() method within the extension definition.

    Define a markdown object inside the returned storage object containing your serialize() and parse() functions.

    - import { createMarkdownExtension } from 'tiptap-markdown'
    
    const CustomNode = Node.create({
    +  addStorage() {
    +    return {
    +      markdown: {
    +        serialize() { /* implementation */ },
    +        parse: { /* implementation */ },
    +      }
    +    }
    +  }
    +})
    
    new Editor({
      extensions: [
        CustomNode,
      ]
    -  markdown: {
    -    extensions: [
    -      createMarkdownExtension(CustomNode, {
    -        serialize() {},
    -        parse: {},
    -      })
    -    ]
    -  }
    })
  3. Migrate to tiptap-markdown v0.7.0

    main

    When upgrading to version 0.7.0, the API for initializing the markdown functionality has changed from a factory function (createMarkdownEditor) to a standard Tiptap extension (Markdown).

    Key Changes:

    • Instead of using createMarkdownEditor(Editor), import Markdown from tiptap-markdown and add it to your editor's extensions array.
    • To retrieve markdown content, access it via the editor storage: editor.storage.markdown.getMarkdown() instead of the direct editor.getMarkdown() method.
    import { Editor } from '@tiptap/core'
    - import { createMarkdownEditor } from 'tiptap-markdown'
    + import { Markdown } from 'tiptap-markdown'
    
    - const MarkdownEditor = createMarkdownEditor(Editor)
    - const editor = new MarkdownEditor({
    + const editor = new Editor({
        extensions: [
    +     Markdown,
        ],
    })
    - const markdownOutput = editor.getMarkdown()
    + const markdownOutput = editor.storage.markdown.getMarkdown()
  4. Configure Markdown extension options

    main

    You can customize the behavior of the Markdown extension using the .configure() method.

    Available options:

    • html: (boolean) Allow HTML input/output. Default: true.
    • tightLists: (boolean) If true, prevents <p> tags inside <li> in markdown output. Default: true.
    • tightListClass: (string) The CSS class added to <ul> to allow removing <p> margins when using tightLists. Default: 'tight'.
    • bulletListMarker: (string) The prefix used for <li> in markdown output. Default: '-'.
    • linkify: (boolean) Whether to create links from "https://..." text. Default: false.
    • breaks: (boolean) Whether new lines (\n) in markdown input are converted to <br>. Default: false.
    • transformPastedText: (boolean) Whether to allow pasting markdown text directly into the editor. Default: false.
    • transformCopiedText: (boolean) Whether copied text is transformed to markdown. Default: false.
    Markdown.configure({
      html: true,
      tightLists: true,
      tightListClass: 'tight',
      bulletListMarker: '-',
      linkify: false,
      breaks: false,
      transformPastedText: false,
      transformCopiedText: false,
    })
  5. Basic usage of tiptap-markdown

    main

    To use markdown in your Tiptap editor, import the Markdown extension and add it to your extensions array. Once configured, you can retrieve the editor content as a markdown string using editor.storage.markdown.getMarkdown().

    import { Editor } from '@tiptap/core';
    import StarterKit from '@tiptap/starter-kit';
    import { Markdown } from 'tiptap-markdown';
    
    const editor = new Editor({
        content: "# Title",
        extensions: [
            StarterKit,
            Markdown,
        ],
    });
    
    // Retrieve content as markdown
    const markdownOutput = editor.storage.markdown.getMarkdown();
  6. Use Markdown methods and commands

    main

    The tiptap-markdown extension enables markdown support through standard Tiptap commands and specialized storage methods:

    • Set content as markdown: Use editor.commands.setContent('...') with a markdown string.
    • Get content as markdown: Use editor.storage.markdown.getMarkdown() to export the current editor state as a markdown string.
    // Set content using markdown syntax
    editor.commands.setContent('**test**')
    
    // Get current content as markdown
    const markdown = editor.storage.markdown.getMarkdown();
  7. Configure MarkdownOptions for the Tiptap Markdown extension

    main

    When initializing the Markdown extension, you can provide a MarkdownOptions object to control how Markdown is parsed and serialized.

    Available options:

    • html: Whether to allow HTML in the output.
    • tightLists: Whether to use tight lists (no extra spacing between items).
    • tightListClass: A CSS class to apply to tight lists.
    • bulletListMarker: A custom marker for bullet lists.
    • linkify: Whether to automatically convert text URLs into links.
    • breaks: Whether to use <br> tags for line breaks.
    • transformPastedText: Whether to transform text when it is pasted into the editor.
    • transformCopiedText: Whether to transform text when it is copied from the editor.
    // Example configuration
    const markdownExtension = Markdown.configure({
      html: true,
      tightLists: true,
      linkify: true
    });
  8. Configure the MarkdownClipboard extension

    main

    The MarkdownClipboard extension allows you to control whether text is transformed into Markdown during paste and copy operations. You can configure this via the addOptions object when initializing the extension.

    Available options:

    • transformPastedText (boolean): If true, the extension will attempt to parse pasted Markdown text into the editor's schema. If false (default), it behaves like a standard paste.
    • transformCopiedText (boolean): If true, the extension will serialize the editor's content into Markdown when copying. If false (default), it behaves like a standard copy.
    import { MarkdownClipboard } from './path-to/clipboard';
    
    const editor = new Editor({
      extensions: [
        MarkdownClipboard.configure({
          transformPastedText: true,
          transformCopiedText: true,
        }),
      ],
    });
  9. Configure the Markdown extension options

    main

    When initializing the Markdown extension, you can provide an options object to control how Markdown is parsed, serialized, and handled in the editor.

    Available options:

    • html: (boolean) Whether to support HTML. Defaults to true.
    • tightLists: (boolean) Whether to use tight lists. Defaults to true.
    • tightListClass: (string) The CSS class applied to tight lists. Defaults to 'tight'.
    • bulletListMarker: (string) The marker used for bullet lists. Defaults to '-'.
    • linkify: (boolean) Whether to automatically linkify URLs. Defaults to false.
    • breaks: (boolean) Whether to use line breaks. Defaults to false.
    • transformPastedText: (boolean) Whether to transform text when pasting. Defaults to false.
    • transformCopiedText: (boolean) Whether to transform text when copying. Defaults to false.
    import { Markdown } from 'tiptap-markdown';
    
    const editor = new Editor({
      extensions: [
        Markdown.configure({
          html: true,
          tightLists: true,
          tightListClass: 'my-tight-list',
          bulletListMarker: '*', 
          linkify: true,
        }),
      ],
    });
  10. Configure the MarkdownTightLists extension

    main

    The MarkdownTightLists extension allows you to manage list spacing (tight vs. loose) by applying CSS classes and data attributes to list elements. You can configure the following options when creating the extension:

    • tight: A boolean indicating whether lists should be tight by default. Defaults to true.
    • tightClass: The CSS class name applied to the list element when it is in 'tight' mode. Defaults to 'tight'.
    • listTypes: An array of Tiptap node names that this extension should apply to. Defaults to ['bulletList', 'orderedList'].
    import { MarkdownTightLists } from './path-to-extension';
    
    const extension = MarkdownTightLists.configure({
      tight: true,
      tightClass: 'my-custom-tight-class',
      listTypes: ['bulletList', 'orderedList'],
    });