Skip to content

Commit bb76f12

Browse files
ScottN-PVclaudejuliusmarminge
authored
fix(web): keep typed underscores literal in the rich text composer (pingdotgg#19186)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 15341e8 commit bb76f12

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎apps/web/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@tiptap/extension-code-block": "^3.31.3",
4646
"@tiptap/extension-heading": "^3.31.3",
4747
"@tiptap/extension-horizontal-rule": "^3.31.3",
48+
"@tiptap/extension-italic": "^3.31.3",
4849
"@tiptap/extension-list": "^3.31.3",
4950
"@tiptap/extension-task-item": "^3.31.3",
5051
"@tiptap/extension-task-list": "^3.31.3",

‎apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,42 @@ function typeText(editor: Editor, from: number, to: number, text: string) {
116116
}
117117
}
118118

119+
function textWithMark(editor: Editor, name: string) {
120+
const text: string[] = [];
121+
editor.state.doc.descendants((node) => {
122+
if (node.isText && node.marks.some((mark) => mark.type.name === name)) {
123+
text.push(node.textContent);
124+
}
125+
});
126+
return text;
127+
}
128+
129+
it.each([
130+
{ value: "clean _build and node", code: [] },
131+
{ value: "clean `_build` and `node", code: ["_build"] },
132+
])("keeps a typed underscore literal after $value", async ({ value, code }) => {
133+
const editor = await mountEditor(value);
134+
await act(async () => {
135+
editor.commands.focus("end");
136+
const { from, to } = editor.state.selection;
137+
typeText(editor, from, to, "_");
138+
});
139+
expect(serializeEditorDoc(editor.state.doc).value).toBe(`${value}_`);
140+
expect(textWithMark(editor, "italic")).toEqual([]);
141+
expect(textWithMark(editor, "code")).toEqual(code);
142+
});
143+
144+
it("still italicizes text when typing a closing asterisk", async () => {
145+
const editor = await mountEditor("clean *build");
146+
await act(async () => {
147+
editor.commands.focus("end");
148+
const { from, to } = editor.state.selection;
149+
typeText(editor, from, to, "*");
150+
});
151+
expect(serializeEditorDoc(editor.state.doc).value).toBe("clean *build*");
152+
expect(textWithMark(editor, "italic")).toEqual(["build"]);
153+
});
154+
119155
it.each([false, true])(
120156
"composes an umlaut without wrapping the preedit text (rich text: %s)",
121157
async (richTextEnabled) => {

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import {
6868
caretTakesMarksBefore,
6969
convertBulletItemToTask,
7070
ComposerCodeExtension,
71+
ComposerItalicExtension,
7172
ComposerTaskItemExtension,
7273
ComposerTaskListExtension,
7374
flatToMarkdown,
@@ -843,7 +844,8 @@ function createComposerExtensions(richText: boolean) {
843844
code: false,
844845
listKeymap: false,
845846
undoRedo: { newGroupDelay: COMPOSER_UNDO_GROUP_DELAY },
846-
...(richText ? {} : { bold: false, italic: false, strike: false }),
847+
italic: false,
848+
...(richText ? {} : { bold: false, strike: false }),
847849
}),
848850
ComposerUndoGroupingExtension,
849851
ComposerMentionExtension,
@@ -853,6 +855,7 @@ function createComposerExtensions(richText: boolean) {
853855
ComposerMarkersExtension,
854856
...(richText
855857
? [
858+
ComposerItalicExtension,
856859
ComposerCodeExtension,
857860
ComposerCodeBlockExtension.extend({
858861
addNodeView() {

‎apps/web/src/composer-rich-text-doc.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import { Blockquote } from "@tiptap/extension-blockquote";
1111
import { CodeBlock } from "@tiptap/extension-code-block";
1212
import { Heading } from "@tiptap/extension-heading";
1313
import { HorizontalRule } from "@tiptap/extension-horizontal-rule";
14-
import { type Command, type Editor, mergeAttributes } from "@tiptap/core";
14+
import { Italic, starInputRegex, starPasteRegex } from "@tiptap/extension-italic";
15+
import {
16+
type Command,
17+
type Editor,
18+
markInputRule,
19+
markPasteRule,
20+
mergeAttributes,
21+
} from "@tiptap/core";
1522
import { BulletList, ListItem, OrderedList } from "@tiptap/extension-list";
1623
import { TaskItem } from "@tiptap/extension-task-item";
1724
import { TaskList } from "@tiptap/extension-task-list";
@@ -127,6 +134,20 @@ export const ComposerCodeExtension = Code.extend({
127134
},
128135
});
129136

137+
/**
138+
* The composer writes italic as `*text*`, so it keeps only the `*` shortcuts.
139+
* Tiptap's `_text_` rules fire on the `_` in names like `node_modules` and
140+
* italicize back to an earlier `_build`, even one inside a code span.
141+
*/
142+
export const ComposerItalicExtension = Italic.extend({
143+
addInputRules() {
144+
return [markInputRule({ find: starInputRegex, type: this.type })];
145+
},
146+
addPasteRules() {
147+
return [markPasteRule({ find: starPasteRegex, type: this.type })];
148+
},
149+
});
150+
130151
/**
131152
* Tiptap's block extensions each bind a chord that turns the current block into
132153
* their node (Mod-Shift-8 for a list, Mod-Alt-c for a fence, and so on). The

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)