Skip to content

Commit 213b391

Browse files
fix(web): composer emphasis shortcuts ignore delimiters inside inline code (pingdotgg#25659)
Co-authored-by: maria-rcks <[email protected]>
1 parent 7370b92 commit 213b391

5 files changed

Lines changed: 140 additions & 3 deletions

File tree

‎apps/web/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
"@tanstack/react-router": "^1.160.2",
4242
"@tiptap/core": "^3.31.3",
4343
"@tiptap/extension-blockquote": "^3.31.3",
44+
"@tiptap/extension-bold": "^3.31.3",
4445
"@tiptap/extension-code": "^3.31.3",
4546
"@tiptap/extension-code-block": "^3.31.3",
4647
"@tiptap/extension-heading": "^3.31.3",
4748
"@tiptap/extension-horizontal-rule": "^3.31.3",
4849
"@tiptap/extension-italic": "^3.31.3",
4950
"@tiptap/extension-list": "^3.31.3",
51+
"@tiptap/extension-strike": "^3.31.3",
5052
"@tiptap/extension-task-item": "^3.31.3",
5153
"@tiptap/extension-task-list": "^3.31.3",
5254
"@tiptap/pm": "^3.31.3",

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ import {
7373
ComposerBoldSelectionOnlyExtension,
7474
convertBulletItemToTask,
7575
ComposerCodeExtension,
76+
ComposerBoldExtension,
7677
ComposerItalicExtension,
78+
ComposerStrikeExtension,
7779
ComposerTaskItemExtension,
7880
ComposerTaskListExtension,
7981
flatToMarkdown,
@@ -831,7 +833,8 @@ function createComposerExtensions(richText: boolean) {
831833
listKeymap: false,
832834
undoRedo: { newGroupDelay: COMPOSER_UNDO_GROUP_DELAY },
833835
italic: false,
834-
...(richText ? {} : { bold: false, strike: false }),
836+
bold: false,
837+
strike: false,
835838
}),
836839
ComposerUndoGroupingExtension,
837840
ComposerTextDirectionExtension,
@@ -844,6 +847,8 @@ function createComposerExtensions(richText: boolean) {
844847
? [
845848
ComposerBoldSelectionOnlyExtension,
846849
ComposerItalicExtension,
850+
ComposerBoldExtension,
851+
ComposerStrikeExtension,
847852
ComposerCodeExtension,
848853
ComposerCodeBlockExtension.extend({
849854
addNodeView() {

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import {
2020
ComposerBlockExtensions,
2121
ComposerCodeBlockExtension,
2222
ComposerListExtensions,
23+
ComposerBoldExtension,
2324
ComposerCodeExtension,
25+
ComposerItalicExtension,
26+
ComposerStrikeExtension,
2427
ComposerTaskItemExtension,
2528
flatToMarkdown,
2629
flatToPm,
@@ -1003,7 +1006,17 @@ describe("inline code entry", () => {
10031006
const editor = new Editor({
10041007
element: null,
10051008
extensions: [
1006-
StarterKit.configure({ code: false, codeBlock: false, trailingNode: false }),
1009+
StarterKit.configure({
1010+
bold: false,
1011+
code: false,
1012+
codeBlock: false,
1013+
italic: false,
1014+
strike: false,
1015+
trailingNode: false,
1016+
}),
1017+
ComposerBoldExtension,
1018+
ComposerItalicExtension,
1019+
ComposerStrikeExtension,
10071020
ComposerCodeExtension,
10081021
ComposerCodeBlockExtension,
10091022
stubAtom("composer-mention", { path: { default: "" }, source: { default: "" } }),
@@ -1165,4 +1178,70 @@ describe("inline code entry", () => {
11651178
editor.commands.setTextSelection(editor.state.doc.content.size - 1);
11661179
expect(editor.state.doc.toJSON()).toEqual(before);
11671180
});
1181+
1182+
function marked(editor: Editor, name: string) {
1183+
const text: string[] = [];
1184+
editor.state.doc.descendants((node) => {
1185+
if (node.isText && node.marks.some((mark) => mark.type.name === name)) {
1186+
text.push(node.text ?? "");
1187+
}
1188+
});
1189+
return text;
1190+
}
1191+
1192+
/** Types `text` into a backtick pair and steps the caret out so it formats. */
1193+
function typeCode(editor: Editor, text: string) {
1194+
const offset = editor.state.selection.from;
1195+
type(editor, "``");
1196+
editor.commands.setTextSelection(offset + 1);
1197+
type(editor, text);
1198+
editor.commands.setTextSelection(offset + text.length + 2);
1199+
expect(marked(editor, "code")).toContain(text);
1200+
}
1201+
1202+
it.each([
1203+
{ mark: "italic", open: "*" },
1204+
{ mark: "bold", open: "**" },
1205+
{ mark: "strike", open: "~~" },
1206+
])("keeps a $mark delimiter inside an earlier code span literal", ({ mark, open }) => {
1207+
const editor = editorFor();
1208+
typeCode(editor, `${open}foo`);
1209+
type(editor, ` bar \`${open}`);
1210+
expect(serializeEditorDoc(editor.state.doc).value).toBe(`\`${open}foo\` bar \`${open}`);
1211+
expect(marked(editor, mark)).toEqual([]);
1212+
expect(marked(editor, "code")).toEqual([`${open}foo`]);
1213+
});
1214+
1215+
it.each([
1216+
{ mark: "italic", open: "*" },
1217+
{ mark: "bold", open: "**" },
1218+
{ mark: "strike", open: "~~" },
1219+
])("still wraps a code span in $mark when both delimiters are outside it", ({ mark, open }) => {
1220+
const editor = editorFor();
1221+
type(editor, open);
1222+
typeCode(editor, "foo");
1223+
type(editor, ` bar${open}`);
1224+
expect(serializeEditorDoc(editor.state.doc).value).toBe(`${open}\`foo\` bar${open}`);
1225+
expect(marked(editor, mark)).toEqual(["foo", " bar"]);
1226+
1227+
editor.commands.setContent({
1228+
type: "doc",
1229+
content: [
1230+
{
1231+
type: "paragraph",
1232+
content: [
1233+
{ type: "text", text: `${open}foo tail ` },
1234+
{ type: "text", text: "x", marks: [{ type: "code" }] },
1235+
],
1236+
},
1237+
],
1238+
});
1239+
editor.commands.setTextSelection({
1240+
from: open.length + 4,
1241+
to: editor.state.doc.content.size - 1,
1242+
});
1243+
type(editor, open);
1244+
expect(serializeEditorDoc(editor.state.doc).value).toBe(`${open}foo${open}`);
1245+
expect(marked(editor, mark)).toEqual(["foo"]);
1246+
});
11681247
});

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import {
66
type Node as ProseMirrorNode,
77
type ResolvedPos,
88
} from "@tiptap/pm/model";
9+
import {
10+
Bold,
11+
starInputRegex as boldStarInputRegex,
12+
underscoreInputRegex as boldUnderscoreInputRegex,
13+
} from "@tiptap/extension-bold";
914
import { Code } from "@tiptap/extension-code";
1015
import { Blockquote } from "@tiptap/extension-blockquote";
1116
import { CodeBlock } from "@tiptap/extension-code-block";
@@ -16,11 +21,13 @@ import {
1621
type Command,
1722
type Editor,
1823
Extension,
24+
InputRule,
1925
markInputRule,
2026
markPasteRule,
2127
mergeAttributes,
2228
} from "@tiptap/core";
2329
import { BulletList, ListItem, OrderedList } from "@tiptap/extension-list";
30+
import { Strike, inputRegex as strikeInputRegex } from "@tiptap/extension-strike";
2431
import { TaskItem } from "@tiptap/extension-task-item";
2532
import { TaskList } from "@tiptap/extension-task-list";
2633
import {
@@ -163,20 +170,58 @@ export const ComposerCodeExtension = Code.extend({
163170
},
164171
});
165172

173+
// Code permits nested emphasis, so Tiptap's exclusion check cannot protect literal delimiters.
174+
function markInputRuleOutsideCode(find: RegExp, type: MarkType) {
175+
const rule = markInputRule({ find, type });
176+
return new InputRule({
177+
find,
178+
handler: (props) => {
179+
const { state, range, match } = props;
180+
const code = state.schema.marks.code;
181+
const content = match[match.length - 1] ?? "";
182+
const textStart = range.from + match[0].indexOf(content);
183+
const textEnd = textStart + content.length;
184+
if (
185+
code &&
186+
(state.doc.rangeHasMark(range.from + match[0].search(/\S/), textStart, code) ||
187+
state.doc.rangeHasMark(textEnd, state.selection.from, code))
188+
) {
189+
return null;
190+
}
191+
return rule.handler(props);
192+
},
193+
});
194+
}
195+
166196
/**
167197
* The composer writes italic as `*text*`, so it keeps only the `*` shortcuts.
168198
* Tiptap's `_text_` rules fire on the `_` in names like `node_modules` and
169199
* italicize back to an earlier `_build`, even one inside a code span.
170200
*/
171201
export const ComposerItalicExtension = Italic.extend({
172202
addInputRules() {
173-
return [markInputRule({ find: starInputRegex, type: this.type })];
203+
return [markInputRuleOutsideCode(starInputRegex, this.type)];
174204
},
175205
addPasteRules() {
176206
return [markPasteRule({ find: starPasteRegex, type: this.type })];
177207
},
178208
});
179209

210+
export const ComposerBoldExtension = Bold.extend({
211+
addInputRules() {
212+
return [
213+
markInputRuleOutsideCode(boldStarInputRegex, this.type),
214+
markInputRuleOutsideCode(boldUnderscoreInputRegex, this.type),
215+
];
216+
},
217+
});
218+
219+
export const ComposerStrikeExtension = Strike.extend({
220+
addInputRules() {
221+
return [markInputRuleOutsideCode(strikeInputRegex, this.type)];
222+
},
223+
});
224+
180225
/**
181226
* Mod+B bolds selected text only. Without a selection Tiptap would toggle bold
182227
* for the next typed text, which users hit by accident when they mean the

‎pnpm-lock.yaml‎

Lines changed: 6 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)