-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Disallow suggestions inside inline code marks and code blocks (#…
…154)
- Loading branch information
Showing
6 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Editor, Range } from '@tiptap/core' | ||
|
||
/** | ||
* Check if a node of a specific type can be inserted at a specific position in the editor. | ||
* | ||
* @return True if the node can be inserted, false otherwise. | ||
*/ | ||
function canInsertNodeAt({ | ||
editor, | ||
nodeType, | ||
range, | ||
}: { | ||
editor: Editor | ||
nodeType: string | ||
range: Range | ||
}) { | ||
return editor.can().insertContentAt(range, { | ||
type: nodeType, | ||
}) | ||
} | ||
|
||
export { canInsertNodeAt } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Editor } from '@tiptap/core' | ||
import { EditorState } from '@tiptap/pm/state' | ||
|
||
/** | ||
* Check if a suggestion can be inserted within the current editor selection. | ||
* | ||
* @return True if the suggestion can be inserted, false otherwise. | ||
*/ | ||
function canInsertSuggestion({ editor, state }: { editor: Editor; state: EditorState }) { | ||
const { selection } = state | ||
|
||
const isInsideCodeMark = editor.isActive('code') | ||
|
||
const isInsideCodeBlockNode = selection.$from.parent.type.name === 'codeBlock' | ||
|
||
const hasCodeMarkBefore = state.doc | ||
.nodeAt(selection.$from.parentOffset - 1) | ||
?.marks.some((mark) => mark.type.name === 'code') | ||
|
||
const isComposingInlineCode = selection.$from.nodeBefore?.text | ||
?.split(' ') | ||
.some((word) => word.startsWith('`')) | ||
|
||
return ( | ||
!isInsideCodeMark && !isInsideCodeBlockNode && !hasCodeMarkBefore && !isComposingInlineCode | ||
) | ||
} | ||
|
||
export { canInsertSuggestion } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Utilities | ||
|
||
Utilities are public helper functions that can be used by custom extensions implemented in the consuming applications. The intent is to provide small reusable functions with the DRY principle in mind. | ||
|
||
## `canInsertNodeAt` | ||
|
||
This function is a shorthand to `editor.can().insertContentAt()`, and checks if a node of a specific type can be inserted at a specific position in the editor. | ||
|
||
## `canInsertSuggestion` | ||
|
||
This function checks if a suggestion – like a mention – can be inserted within the current editor selection, and the main purpose is to check for all possible edge cases and disallow suggestions from being inserted within inline code marks or code blocks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Meta } from '@storybook/addon-docs' | ||
|
||
import { MarkdownRenderer } from '../../components/markdown-renderer.tsx' | ||
|
||
import rawUtilities from './utilities.md?raw' | ||
|
||
<Meta | ||
title="Documentation/Reference/Utilities" | ||
parameters={{ | ||
viewMode: 'docs', | ||
options: { | ||
isToolshown: false, | ||
}, | ||
}} | ||
/> | ||
|
||
<MarkdownRenderer markdown={rawUtilities} /> |