Skip to content

Commit 50a6c7a

Browse files
ScottN-PVclaude
andcommitted
fix(web): keep typed underscores literal in the rich text composer
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a01c48f commit 50a6c7a

5 files changed

Lines changed: 102 additions & 2 deletions

File tree

‎apps/web/package.json‎

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

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
caretTakesMarksBefore,
6262
convertBulletItemToTask,
6363
ComposerCodeExtension,
64+
ComposerItalicExtension,
6465
ComposerTaskItemExtension,
6566
ComposerTaskListExtension,
6667
flatToCollapsed,
@@ -1059,8 +1060,10 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
10591060
// The list extensions handle Backspace and Delete themselves.
10601061
listKeymap: false,
10611062
undoRedo: { newGroupDelay: COMPOSER_UNDO_GROUP_DELAY },
1063+
// Rich mode adds ComposerItalicExtension below.
1064+
italic: false,
10621065
// Plain mode has no marks: typed markers stay literal characters.
1063-
...(richText ? {} : { bold: false, italic: false, strike: false }),
1066+
...(richText ? {} : { bold: false, strike: false }),
10641067
}),
10651068
ComposerUndoGroupingExtension,
10661069
ComposerMentionExtension,
@@ -1070,6 +1073,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
10701073
ComposerMarkersExtension,
10711074
...(richText
10721075
? [
1076+
ComposerItalicExtension,
10731077
ComposerCodeExtension,
10741078
ComposerCodeBlockExtension.extend({
10751079
addNodeView() {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Editor } from "@tiptap/core";
2+
import { TextSelection } from "@tiptap/pm/state";
3+
import StarterKit from "@tiptap/starter-kit";
4+
import { describe, expect, it } from "vite-plus/test";
5+
6+
import {
7+
buildDocJson,
8+
ComposerCodeExtension,
9+
ComposerItalicExtension,
10+
serializeEditorDoc,
11+
} from "./composer-rich-text-doc";
12+
13+
/**
14+
* Types `text` at the end of `value`, the way the composer's editor would
15+
* receive it, and returns the stored Markdown and whether any text is italic.
16+
*/
17+
function typeAtEnd(value: string, text: string) {
18+
const editor = new Editor({
19+
extensions: [
20+
StarterKit.configure({
21+
blockquote: false,
22+
bulletList: false,
23+
codeBlock: false,
24+
heading: false,
25+
horizontalRule: false,
26+
listItem: false,
27+
orderedList: false,
28+
trailingNode: false,
29+
code: false,
30+
italic: false,
31+
}),
32+
ComposerItalicExtension,
33+
ComposerCodeExtension,
34+
],
35+
content: buildDocJson(value, (name) => ({ label: name, description: null })),
36+
});
37+
editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
38+
// What the view's `someProp("handleTextInput")` does, without mounting one:
39+
// offer the text to each extension plugin, then insert it if none took it.
40+
const { from, to } = editor.state.selection;
41+
const insert = () => editor.state.tr.insertText(text, from, to);
42+
const handled = editor.extensionManager.plugins.some((plugin) =>
43+
plugin.props.handleTextInput?.call(plugin, editor.view, from, to, text, insert),
44+
);
45+
if (!handled) editor.view.dispatch(insert());
46+
let italic = false;
47+
editor.state.doc.descendants((node) => {
48+
italic ||= node.marks.some((mark) => mark.type.name === "italic");
49+
});
50+
return { value: serializeEditorDoc(editor.state.doc).value, italic };
51+
}
52+
53+
describe("ComposerItalicExtension", () => {
54+
it("keeps an underscore typed inside a snake_case word", () => {
55+
expect(typeAtEnd("clean _build and node", "_")).toEqual({
56+
value: "clean _build and node_",
57+
italic: false,
58+
});
59+
});
60+
61+
it("leaves the underscore in a finished code span alone", () => {
62+
expect(typeAtEnd("clean `_build` and `node", "_")).toEqual({
63+
value: "clean `_build` and `node_",
64+
italic: false,
65+
});
66+
});
67+
68+
it("still italicizes text typed between asterisks", () => {
69+
expect(typeAtEnd("clean *build", "*")).toEqual({ value: "clean *build*", italic: true });
70+
});
71+
});

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { Blockquote } from "@tiptap/extension-blockquote";
55
import { CodeBlock } from "@tiptap/extension-code-block";
66
import { Heading } from "@tiptap/extension-heading";
77
import { HorizontalRule } from "@tiptap/extension-horizontal-rule";
8-
import { type Command, type Editor, mergeAttributes } from "@tiptap/core";
8+
import { Italic, starInputRegex, starPasteRegex } from "@tiptap/extension-italic";
9+
import {
10+
type Command,
11+
type Editor,
12+
markInputRule,
13+
markPasteRule,
14+
mergeAttributes,
15+
} from "@tiptap/core";
916
import { BulletList, ListItem, OrderedList } from "@tiptap/extension-list";
1017
import { TaskItem } from "@tiptap/extension-task-item";
1118
import { TaskList } from "@tiptap/extension-task-list";
@@ -70,6 +77,20 @@ export const ComposerCodeExtension = Code.extend({
7077
exitable: false,
7178
});
7279

80+
/**
81+
* The composer writes italic as `*text*`, so it keeps only the `*` shortcuts.
82+
* Tiptap's `_text_` rules fire on the `_` in names like `node_modules` and
83+
* italicize back to an earlier `_build`, even one inside a code span.
84+
*/
85+
export const ComposerItalicExtension = Italic.extend({
86+
addInputRules() {
87+
return [markInputRule({ find: starInputRegex, type: this.type })];
88+
},
89+
addPasteRules() {
90+
return [markPasteRule({ find: starPasteRegex, type: this.type })];
91+
},
92+
});
93+
7394
/**
7495
* Tiptap's block extensions each bind a chord that turns the current block
7596
* into their node (Mod-Shift-8 for a list, Mod-Alt-c for a fence, and so on).

‎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)