Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Extra paragraph node inserted above an Horizontal Rule #313

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/extensions/rich-text/rich-text-horizontal-rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { InputRule } from '@tiptap/core'
import { HorizontalRule } from '@tiptap/extension-horizontal-rule'

import type { HorizontalRuleOptions } from '@tiptap/extension-horizontal-rule'

/**
* The input regex for Markdown horizontal rules.
*/
const inputRegex = /^(?:---|—-|___\s|\*\*\*\s)$/

/**
* Custom extension that extends the built-in `HorizontalRule` extension to fix an issue with the
* built-in input rule that adds extra paragraph node above the horizontal rule.
*
* @see https://github.com/ueberdosis/tiptap/issues/3809
* @see https://github.com/ueberdosis/tiptap/pull/3859#issuecomment-1536799740
*/
const RichTextHorizontalRule = HorizontalRule.extend({
addInputRules() {
const { type } = this

return [
new InputRule({
find: inputRegex,
handler({ state: { tr }, range }) {
tr.insert(range.from - 1, type.create({})).delete(
tr.mapping.map(range.from),
tr.mapping.map(range.to),
)
},
}),
]
},
})

export { RichTextHorizontalRule }

export type { HorizontalRuleOptions as RichTextHorizontalRuleOptions }
8 changes: 4 additions & 4 deletions src/extensions/rich-text/rich-text-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Gapcursor } from '@tiptap/extension-gapcursor'
import { HardBreak } from '@tiptap/extension-hard-break'
import { Heading } from '@tiptap/extension-heading'
import { History } from '@tiptap/extension-history'
import { HorizontalRule } from '@tiptap/extension-horizontal-rule'
import { Italic } from '@tiptap/extension-italic'
import { ListItem } from '@tiptap/extension-list-item'
import { OrderedList } from '@tiptap/extension-ordered-list'
Expand All @@ -28,6 +27,7 @@ import { CurvenoteCodemark } from './curvenote-codemark'
import { PasteEmojis } from './paste-emojis'
import { PasteMarkdown } from './paste-markdown'
import { RichTextDocument } from './rich-text-document'
import { RichTextHorizontalRule } from './rich-text-horizontal-rule'
import { RichTextImage } from './rich-text-image'
import { RichTextLink } from './rich-text-link'

Expand All @@ -41,13 +41,13 @@ import type { DropcursorOptions } from '@tiptap/extension-dropcursor'
import type { HardBreakOptions } from '@tiptap/extension-hard-break'
import type { HeadingOptions } from '@tiptap/extension-heading'
import type { HistoryOptions } from '@tiptap/extension-history'
import type { HorizontalRuleOptions } from '@tiptap/extension-horizontal-rule'
import type { ItalicOptions } from '@tiptap/extension-italic'
import type { ListItemOptions } from '@tiptap/extension-list-item'
import type { OrderedListOptions } from '@tiptap/extension-ordered-list'
import type { ParagraphOptions } from '@tiptap/extension-paragraph'
import type { StrikeOptions } from '@tiptap/extension-strike'
import type { RichTextDocumentOptions } from './rich-text-document'
import type { RichTextHorizontalRuleOptions } from './rich-text-horizontal-rule'
import type { RichTextImageOptions } from './rich-text-image'
import type { RichTextLinkOptions } from './rich-text-link'

Expand Down Expand Up @@ -113,7 +113,7 @@ type RichTextKitOptions = {
/**
* Set options for the `HorizontalRule` extension, or `false` to disable.
*/
horizontalRule: Partial<HorizontalRuleOptions> | false
horizontalRule: Partial<RichTextHorizontalRuleOptions> | false

/**
* Set options for the `Image` extension, or `false` to disable.
Expand Down Expand Up @@ -276,7 +276,7 @@ const RichTextKit = Extension.create<RichTextKitOptions>({
}

if (this.options.horizontalRule !== false) {
extensions.push(HorizontalRule.configure(this.options?.horizontalRule))
extensions.push(RichTextHorizontalRule.configure(this.options?.horizontalRule))
}

if (this.options.image !== false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ div + .editorContainer {
margin: 0;
}

:global(div[data-typist-editor] p + hr) {
margin-top: 8px;
}

:global(div[data-typist-editor] :is(pre, code)) {
font-family: var(--storybook-theme-fontCode);
}
Expand Down