snapshot of https://github.com/ueberdosis/tiptap.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
Verify / Install node dependencies (push) Failing after 2s
Verify / Build packages (push) Has been skipped
Verify / Check linting & formatting (push) Has been skipped
Verify / Run unit tests (push) Has been skipped
Verify / Run e2e tests (shard 1/4) (push) Has been skipped
Verify / Run e2e tests (shard 2/4) (push) Has been skipped
Verify / Run e2e tests (shard 3/4) (push) Has been skipped
Verify / Run e2e tests (shard 4/4) (push) Has been skipped
Publish / Resolve publish config (push) Failing after 2s
Publish / Send notification to Slack (push) Has been skipped
Publish / Send failure notification to Slack (push) Failing after 8s
Verify / Merge e2e reports (push) Has been skipped
Publish / Build (push) Has been skipped
Publish / Bump Versions OR Release to Registry (push) Has been skipped
Some checks failed
Verify / Install node dependencies (push) Failing after 2s
Verify / Build packages (push) Has been skipped
Verify / Check linting & formatting (push) Has been skipped
Verify / Run unit tests (push) Has been skipped
Verify / Run e2e tests (shard 1/4) (push) Has been skipped
Verify / Run e2e tests (shard 2/4) (push) Has been skipped
Verify / Run e2e tests (shard 3/4) (push) Has been skipped
Verify / Run e2e tests (shard 4/4) (push) Has been skipped
Publish / Resolve publish config (push) Failing after 2s
Publish / Send notification to Slack (push) Has been skipped
Publish / Send failure notification to Slack (push) Failing after 8s
Verify / Merge e2e reports (push) Has been skipped
Publish / Build (push) Has been skipped
Publish / Bump Versions OR Release to Registry (push) Has been skipped
This commit is contained in:
1685
packages/extension-blockquote/CHANGELOG.md
Normal file
1685
packages/extension-blockquote/CHANGELOG.md
Normal file
File diff suppressed because it is too large
Load Diff
18
packages/extension-blockquote/README.md
Normal file
18
packages/extension-blockquote/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# @tiptap/extension-blockquote
|
||||
|
||||
[](https://www.npmjs.com/package/@tiptap/extension-blockquote)
|
||||
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
||||
[](https://www.npmjs.com/package/@tiptap/extension-blockquote)
|
||||
[](https://github.com/sponsors/ueberdosis)
|
||||
|
||||
## Introduction
|
||||
|
||||
Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as _New York Times_, _The Guardian_ or _Atlassian_.
|
||||
|
||||
## Official Documentation
|
||||
|
||||
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
||||
|
||||
## License
|
||||
|
||||
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
||||
52
packages/extension-blockquote/package.json
Normal file
52
packages/extension-blockquote/package.json
Normal file
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"name": "@tiptap/extension-blockquote",
|
||||
"version": "3.30.3",
|
||||
"description": "blockquote extension for tiptap",
|
||||
"keywords": [
|
||||
"tiptap",
|
||||
"tiptap extension"
|
||||
],
|
||||
"homepage": "https://tiptap.dev",
|
||||
"bugs": {
|
||||
"url": "https://github.com/ueberdosis/tiptap/issues"
|
||||
},
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/ueberdosis/tiptap",
|
||||
"directory": "packages/extension-blockquote"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/ueberdosis"
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
"dist"
|
||||
],
|
||||
"type": "module",
|
||||
"main": "dist/index.cjs",
|
||||
"module": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": {
|
||||
"import": "./dist/index.d.ts",
|
||||
"require": "./dist/index.d.cts"
|
||||
},
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.cjs"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"build": "vp pack"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tiptap/core": "workspace:^",
|
||||
"@tiptap/pm": "workspace:^"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@tiptap/core": "workspace:*",
|
||||
"@tiptap/pm": "workspace:*"
|
||||
}
|
||||
}
|
||||
143
packages/extension-blockquote/src/blockquote.tsx
Normal file
143
packages/extension-blockquote/src/blockquote.tsx
Normal file
@@ -0,0 +1,143 @@
|
||||
/** @jsxImportSource @tiptap/core */
|
||||
import { mergeAttributes, Node, wrappingInputRule } from '@tiptap/core'
|
||||
|
||||
import { handleBackspace } from './handleBackspace.js'
|
||||
|
||||
export interface BlockquoteOptions {
|
||||
/**
|
||||
* HTML attributes to add to the blockquote element
|
||||
* @default {}
|
||||
* @example { class: 'foo' }
|
||||
*/
|
||||
HTMLAttributes: Record<string, any>
|
||||
}
|
||||
|
||||
declare module '@tiptap/core' {
|
||||
interface Commands<ReturnType> {
|
||||
blockQuote: {
|
||||
/**
|
||||
* Set a blockquote node
|
||||
*/
|
||||
setBlockquote: () => ReturnType
|
||||
/**
|
||||
* Toggle a blockquote node
|
||||
*/
|
||||
toggleBlockquote: () => ReturnType
|
||||
/**
|
||||
* Unset a blockquote node
|
||||
*/
|
||||
unsetBlockquote: () => ReturnType
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches a blockquote to a `>` as input.
|
||||
*/
|
||||
export const inputRegex = /^\s*>\s$/
|
||||
|
||||
/**
|
||||
* This extension allows you to create blockquotes.
|
||||
* @see https://tiptap.dev/api/nodes/blockquote
|
||||
*/
|
||||
export const Blockquote = Node.create<BlockquoteOptions>({
|
||||
name: 'blockquote',
|
||||
|
||||
addOptions() {
|
||||
return {
|
||||
HTMLAttributes: {},
|
||||
}
|
||||
},
|
||||
|
||||
content: 'block+',
|
||||
|
||||
group: 'block',
|
||||
|
||||
defining: true,
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'blockquote' }]
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
return (
|
||||
<blockquote {...mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)}>
|
||||
<slot />
|
||||
</blockquote>
|
||||
)
|
||||
},
|
||||
|
||||
parseMarkdown: (token, helpers) => {
|
||||
const parseBlockChildren = helpers.parseBlockChildren ?? helpers.parseChildren
|
||||
|
||||
return helpers.createNode('blockquote', undefined, parseBlockChildren(token.tokens || []))
|
||||
},
|
||||
|
||||
renderMarkdown: (node, h) => {
|
||||
if (!node.content) {
|
||||
return ''
|
||||
}
|
||||
|
||||
// Use a single '>' prefix regardless of nesting level
|
||||
// Nested blockquotes will add their own '>' prefix recursively
|
||||
const prefix = '>'
|
||||
const result: string[] = []
|
||||
|
||||
node.content.forEach((child, index) => {
|
||||
const childContent = h.renderChild?.(child, index) ?? h.renderChildren([child])
|
||||
const lines = childContent.split('\n')
|
||||
|
||||
const linesWithPrefix = lines.map(line => {
|
||||
// Don't add prefix to empty lines
|
||||
if (line.trim() === '') {
|
||||
return prefix
|
||||
}
|
||||
|
||||
// Nested blockquotes will already have their own prefixes
|
||||
// We just need to add our own prefix at the start
|
||||
return `${prefix} ${line}`
|
||||
})
|
||||
|
||||
result.push(linesWithPrefix.join('\n'))
|
||||
})
|
||||
|
||||
// Add separator lines between children
|
||||
return result.join(`\n${prefix}\n`)
|
||||
},
|
||||
|
||||
addCommands() {
|
||||
return {
|
||||
setBlockquote:
|
||||
() =>
|
||||
({ commands }) => {
|
||||
return commands.wrapIn(this.name)
|
||||
},
|
||||
toggleBlockquote:
|
||||
() =>
|
||||
({ commands }) => {
|
||||
return commands.toggleWrap(this.name)
|
||||
},
|
||||
unsetBlockquote:
|
||||
() =>
|
||||
({ commands }) => {
|
||||
return commands.lift(this.name)
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
addKeyboardShortcuts() {
|
||||
return {
|
||||
'Mod-Shift-b': () => this.editor.commands.toggleBlockquote(),
|
||||
Backspace: () => handleBackspace(this.editor, this.type),
|
||||
}
|
||||
},
|
||||
|
||||
addInputRules() {
|
||||
return [
|
||||
wrappingInputRule({
|
||||
find: inputRegex,
|
||||
type: this.type,
|
||||
}),
|
||||
]
|
||||
},
|
||||
})
|
||||
45
packages/extension-blockquote/src/handleBackspace.test.ts
Normal file
45
packages/extension-blockquote/src/handleBackspace.test.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Editor } from '@tiptap/core'
|
||||
import Document from '@tiptap/extension-document'
|
||||
import Image from '@tiptap/extension-image'
|
||||
import Paragraph from '@tiptap/extension-paragraph'
|
||||
import Text from '@tiptap/extension-text'
|
||||
import { GapCursor } from '@tiptap/pm/gapcursor'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vite-plus/test'
|
||||
|
||||
import { handleBackspace } from './handleBackspace.js'
|
||||
import { Blockquote } from './blockquote.js'
|
||||
|
||||
describe('Blockquote handleBackspace', () => {
|
||||
let editor: Editor
|
||||
|
||||
beforeEach(() => {
|
||||
const element = document.createElement('div')
|
||||
document.body.appendChild(element)
|
||||
editor = new Editor({
|
||||
element,
|
||||
extensions: [Document, Paragraph, Text, Image, Blockquote],
|
||||
content: '<img src="https://example.com/image.png">',
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
editor.destroy()
|
||||
})
|
||||
|
||||
// Regression test for https://github.com/ueberdosis/tiptap/issues/7973
|
||||
// With a leading image, pressing backspace at the very start of the document
|
||||
// resolved the caret to the top (doc) level, where `$from.depth` is 0. The
|
||||
// handler then dereferenced `$from.node(-1)` (undefined) and crashed with
|
||||
// `TypeError: can't access property "type", parent is undefined`.
|
||||
it('does not throw when backspacing at the very start of the document', () => {
|
||||
const { state, view } = editor
|
||||
// Before the leading image, the caret resolves to a gap cursor at depth 0.
|
||||
const selection = new GapCursor(state.doc.resolve(0))
|
||||
view.dispatch(state.tr.setSelection(selection))
|
||||
|
||||
const blockquoteType = editor.schema.nodes.blockquote
|
||||
|
||||
expect(() => handleBackspace(editor, blockquoteType)).not.toThrow()
|
||||
expect(handleBackspace(editor, blockquoteType)).toBe(false)
|
||||
})
|
||||
})
|
||||
75
packages/extension-blockquote/src/handleBackspace.ts
Normal file
75
packages/extension-blockquote/src/handleBackspace.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import type { Editor } from '@tiptap/core'
|
||||
import { Slice, type NodeType } from '@tiptap/pm/model'
|
||||
import { TextSelection } from '@tiptap/pm/state'
|
||||
|
||||
/**
|
||||
* Restructure the blockquote boundary at the caret.
|
||||
*
|
||||
* Two cases are handled in a single backspace:
|
||||
*
|
||||
* 1. Caret at the start of a non-first child of a blockquote — lift the
|
||||
* current child out, splitting the blockquote around it.
|
||||
* 2. Caret at the start of a top-level textblock whose previous sibling is
|
||||
* a blockquote with a textblock last child — merge the current
|
||||
* textblock's inline content into the blockquote's last textblock
|
||||
* instead of letting joinBackward pull the paragraph back inside.
|
||||
*
|
||||
* Returns true when the backspace was consumed.
|
||||
*/
|
||||
export const handleBackspace = (editor: Editor, type: NodeType): boolean => {
|
||||
const { state } = editor
|
||||
const { selection } = state
|
||||
if (!selection.empty) return false
|
||||
|
||||
const { $from } = selection
|
||||
if ($from.parentOffset !== 0) return false
|
||||
|
||||
const parentDepth = $from.depth - 1
|
||||
// At the very start of the document the caret can resolve to the top (doc)
|
||||
// level — for example a gap cursor before a leading image — where there is
|
||||
// no parent block. Bail out so backspace is a no-op instead of dereferencing
|
||||
// an undefined parent at a negative depth. (#7973)
|
||||
if (parentDepth < 0) return false
|
||||
|
||||
const parent = $from.node(parentDepth)
|
||||
const index = $from.index(parentDepth)
|
||||
if (index === 0) return false
|
||||
|
||||
// Non-first child of a blockquote: lift to split the blockquote around it.
|
||||
if (parent.type === type) {
|
||||
return editor.commands.lift(type.name)
|
||||
}
|
||||
|
||||
// Previous sibling is a blockquote whose last child is a textblock:
|
||||
// merge the inline content in instead of letting joinBackward pull the
|
||||
// paragraph back inside the blockquote.
|
||||
const previous = parent.child(index - 1)
|
||||
if (previous.type !== type || !previous.lastChild?.isTextblock) {
|
||||
return false
|
||||
}
|
||||
|
||||
const blockStart = $from.before()
|
||||
// `blockStart` sits in the shared parent at the position right after
|
||||
// the previous blockquote. In ProseMirror coordinates, each closing
|
||||
// token costs one position: step one back to land inside the blockquote
|
||||
// right after its last child, then one more to land inside that last
|
||||
// child at the end of its inline content.
|
||||
const insideBlockquoteEnd = blockStart - 1
|
||||
const targetPos = insideBlockquoteEnd - 1
|
||||
|
||||
return editor.commands.command(({ tr, dispatch }) => {
|
||||
if (!dispatch) {
|
||||
return true
|
||||
}
|
||||
|
||||
const content = $from.parent.content
|
||||
const slice = new Slice(content, 0, 0)
|
||||
|
||||
tr.replace(targetPos, $from.after(), slice)
|
||||
tr.setSelection(TextSelection.create(tr.doc, targetPos + content.size))
|
||||
tr.scrollIntoView()
|
||||
dispatch(tr)
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
5
packages/extension-blockquote/src/index.ts
Normal file
5
packages/extension-blockquote/src/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Blockquote } from './blockquote.jsx'
|
||||
|
||||
export * from './blockquote.jsx'
|
||||
|
||||
export default Blockquote
|
||||
9
packages/extension-blockquote/vite.config.ts
Normal file
9
packages/extension-blockquote/vite.config.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { defineConfig } from 'vite-plus'
|
||||
import { basePackConfig } from '../../pack.config.mjs'
|
||||
|
||||
export default defineConfig({
|
||||
pack: {
|
||||
entry: ['src/index.ts'],
|
||||
...basePackConfig(),
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user