quilljs-markdown

repository·master·Indexed 42 days ago

https://github.com/cloverhearts/quilljs-markdown

An extension for the QuillJS editor that enables Markdown shortcut and paste support. It automatically converts Markdown syntax—including headings, blockquotes, bold, italic, links, inline code, code blocks, lists, strikethrough, and checkboxes—into Quill formats. Version 1.2.0.

Tokens
4.5K
Snippets
16
Records
21
Agent score
52%

What's inside quilljs-markdown

  1. Use quilljs-markdown via manual instantiation

    master

    Alternatively, you can manually instantiate QuillMarkdown by passing an existing Quill editor instance and an options object. This approach is useful if you want to control exactly when markdown support is enabled or disabled.

    import Quill from 'quill'
    import QuillMarkdown from 'quilljs-markdown'
    import 'quilljs-markdown/dist/quilljs-markdown-common-style.css'
    
    const editor = new Quill('#editor', { theme: 'snow' })
    const markdownOptions = {
      ignoreTags: ['pre'],
      tags: {
        bold: { pattern: /^\|\s/g }
      }
    }
    
    const quillMarkdown = new QuillMarkdown(editor, markdownOptions)
  2. Use quilljs-markdown as a Quill module

    master

    To use quilljs-markdown as a standard Quill module, register it using Quill.register and pass the configuration via the modules object in the Quill constructor. This is the recommended way to integrate it into a standard Quill setup.

    Configuration Options:

    • ignoreTags: An array of tag names (e.g., ['pre', 'strikethrough']) that the markdown parser should ignore.
    • tags: An object defining custom regex patterns for specific markdown tags. Each tag entry requires a pattern key.

    Example registration and initialization:

    import Quill from 'quill'
    import QuillMarkdown from 'quilljs-markdown'
    
    const options = {
      theme: 'snow',
      modules: {
        toolbar: [['bold', 'italic', 'underline', 'strike'], ['link', 'image']],
        QuillMarkdown: {
           ignoreTags: [ 'pre', 'strikethrough'],
           tags: {
            blockquote: { pattern: /^(\|){1,6}\s/g },
            bold: { pattern: /^(\|){1,6}\s/g },
            italic: { pattern: /(\_){1}(.+?)(?:\1){1}/g },
          },
        }
      }
    }
    
    // Register the module
    Quill.register('modules/QuillMarkdown', QuillMarkdown, true)
    
    // Initialize Quill
    new Quill('#editor', options)
  3. Header tag pattern and behavior

    master

    The Header tag uses a regular expression to detect Markdown heading syntax.

    Default Pattern: /^(#){1,6}\s/g

    Behavior:

    1. It matches between 1 and 6 # characters followed by a space.
    2. The heading level is determined by the length of the match (number of # symbols).
    3. Upon matching, it uses quillJS.formatLine to apply the header format and quillJS.deleteText to remove the Markdown symbols, leaving only the text.
    4. It maps the number of # symbols to Quill header sizes: # becomes h1, ## becomes h2, up to ###### which becomes h6.
  4. Disable markdown support with destroy()

    master

    To remove event listeners and disable markdown support, call the .destroy() method on your QuillMarkdown instance. To re-enable support, you must create a new instance of QuillMarkdown.

    const quillMarkdown = new QuillMarkdown(editor, markdownOptions)
    // markdown is enabled
    
    quillMarkdown.destroy()
    // markdown is now disabled
  5. Supported Markdown syntax

    master

    The library provides paste support and shortcut support for the following Markdown patterns:

    FeatureMarkdown Example
    Heading# Heading 1 to ###### Heading 6
    Blockquote> blockquote text
    Bold**Bold Text** or __Bold Text__
    Italic*Italics Text* or _Italics Text_
    Link[link text](https://link_url)
    Inline Code`inline code block`
    Code Block``` (triple backticks)
    Lists1. Ordered or * Unordered
    Strikethrough~~Strikethrough~~
    Checkbox[ ] (unchecked) or [x] (checked)
  6. Configure Codeblock pattern and ignored tags

    master

    When instantiating the Codeblock class, you can pass an options object to customize its behavior:

    • pattern: A regular expression used to identify code blocks. By default, it uses /^(```).*/g (matching triple backticks).
    • ignoreTags: An array of tag names to be excluded from the activeTags list, preventing the tag from triggering if these tags are present.
  7. Import and use QuillMarkdown

    master

    You can import the QuillMarkdown module directly into your project. If you are working in a browser environment, the library also attaches itself to the window.QuillMarkdown global object for convenience.

    import QuillMarkdown from './src/app.js';
  8. Initialize Markdown support with MarkdownActivity

    master

    To enable Markdown support in a Quill editor, instantiate the MarkdownActivity class. You must pass an existing Quill editor instance as the first argument. You can optionally provide an options object to configure the behavior.

    Options

    • ignoreTags: An array of HTML tag names to ignore during Markdown processing. By default, 'PRE' is ignored. You can extend this by passing additional tags in the array.

    Lifecycle

    • Initialization: The class attaches a listener to the Quill text-change event to monitor user input.
    • Cleanup: To prevent memory leaks and remove event listeners, you must call the .destroy() method when the editor is no longer in use.
    import MarkdownActivity from 'quilljs-markdown';
    
    // Assuming 'quill' is your initialized Quill editor instance
    const markdownSupport = new MarkdownActivity(quill, {
      ignoreTags: ['PRE', 'CODE']
    });
    
    // When the editor is destroyed or removed from the DOM
    // markdownSupport.destroy();
  9. Use the Header tag to support Markdown-style headings

    master

    The Header class is a plugin component for quilljs-markdown that enables Markdown-style heading syntax (e.g., # , ## ) to be converted into Quill header formats.

    When a user types a pattern matching the header regex, the plugin identifies the heading level based on the number of # characters and applies the corresponding Quill format (e.g., h1 through h6).

    Configuration

    • options.ignoreTags: An array of tag names to ignore. If 'header' is included in this list, the header transformation will not trigger.
    • options.pattern: A custom regular expression to define what constitutes a header. By default, it uses /^(#){1,6}\s/g.
    // Example conceptual usage within the quilljs-markdown ecosystem
    const headerTag = new Header(quillJS, {
      ignoreTags: [], // Do not ignore headers
      // pattern is defaulted to /^(#){1,6}\s/g
    });
  10. Initialize TagsOperators to manage markdown tag operators

    master

    The TagsOperators class is the central registry for managing markdown tag operators within the Quill editor. When instantiated, it automatically categorizes operators into two groups: supportInlineTags (for inline formatting) and supportfullTextTags (for full-text/block-level formatting).

    To use it, pass an instance of quillJS and an optional options object containing a tags configuration.

    Supported inline tags include:

    • Header
    • Blockquote
    • Bold
    • Codeblock
    • InlineCode
    • Strikethrough
    • Italics
    • Link
    import TagsOperators from './src/tags/index.js';
    
    // Assuming quillJS is an initialized Quill instance
    const options = { tags: {} };
    const tagsOperators = new TagsOperators(quillJS, options);
  11. Implement Strikethrough formatting

    master

    The Bold class (exported as the strikethrough implementation) extends AbstractTag to handle markdown-style strikethrough patterns in the Quill editor. It detects patterns like ~~text~~ or __text__ and converts them into Quill's native strike format.

    Pattern Detection

    By default, it uses the regex /(?:~|_){2}((.+?))(?:~|_){2}/g. This matches text wrapped in either two tildes (~~) or two underscores (__).

    Configuration

    You can pass a custom pattern via the options object in the constructor to override the default regex. You can also provide ignoreTags to exclude specific tags from being processed.

    Behavior

    When a match is found, the tag:

    1. Deletes the annotated text (including the delimiters).
    2. Inserts the matched text.
    3. Applies the Quill strike: true format to the inserted text.
    // Note: The class is named 'Bold' in the source but implements 'strikethrough'
    import Strikethrough from 'quilljs-markdown/src/tags/strikethrough/index.js';
    
    const strikethrough = new Strikethrough(quillInstance, {
      pattern: /(?:~){2}(.+?)(?:~){2}/g, // Example of a custom pattern
      ignoreTags: ['some-other-tag']
    });