Skip to content

Commit bd6ad4d

Browse files
fix(web): a slash alone no longer makes a word a path
`and/_or_` and `_and_/or` are prose and keep their emphasis as on main. A word with a slash counts as a URL or path only when it also has a dot, a scheme or port, or starts at a root (`/`, `~/`). Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 0bdb42f commit bd6ad4d

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe("composer rich text markdown", () => {
7373
["_see src/_a.ts_.", "see src/_a.ts", "italic", "."],
7474
["_notes.md_ here", "notes.md", "italic", " here"],
7575
["_e.g._ this", "e.g.", "italic", " this"],
76+
["_and_/or", "and", "italic", "/or"],
7677
] as const) {
7778
expect(parseInlineMarkdown(text)).toEqual([
7879
{ text: styled, marks: [mark] },
@@ -90,6 +91,10 @@ describe("composer rich text markdown", () => {
9091
{ text: second, marks: [mark] },
9192
]);
9293
}
94+
expect(parseInlineMarkdown("and/_or_")).toEqual([
95+
{ text: "and/", marks: [] },
96+
{ text: "or", marks: ["italic"] },
97+
]);
9398
expect(parseInlineMarkdown("_build_/file.ts and _x_, __init__.py and __y__")).toEqual([
9499
{ text: "_build_/file.ts and ", marks: [] },
95100
{ text: "x", marks: ["italic"] },

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function sameMarks(left: readonly RichTextMark[], right: readonly RichTextMark[]
2323
return left.length === right.length && left.every((mark, index) => mark === right[index]);
2424
}
2525

26-
// Brackets, quotes, commas, and Markdown delimiters end a URL or path token.
27-
const PATH_TOKEN = /[\p{L}\p{N}_/.:@%+=&?#-]+/gu;
26+
// Brackets, quotes, commas, and Markdown delimiters end a URL or path token; a lone `~` does not.
27+
const PATH_TOKEN = /(?:[\p{L}\p{N}_/.:@%+=&?#-]|(?<!~)~(?!~))+/gu;
2828
const FILE_EXTENSION = /^\.[a-z][a-z0-9]*(?::\d+){0,2}$/i;
2929
// `_yes_/_no_`: each side of a slash is its own emphasis span, so the word is not a path.
3030
const EMPHASIS_ALTERNATIVES = /^_[^/]+_(?:\/_[^/]+_)+$/;
@@ -43,7 +43,11 @@ function literalUnderscoreRuns(text: string): Set<number> {
4343
if (EMPHASIS_ALTERNATIVES.test(token.slice(0, bodyEnd))) continue;
4444
while (bodyEnd > 0 && token[bodyEnd - 1] === "_") bodyEnd -= 1;
4545
const body = token.slice(0, bodyEnd);
46-
if (!body.includes("/") && !FILE_EXTENSION.test(body.slice(body.lastIndexOf(".")))) continue;
46+
// A slash alone is prose (`and/_or_`); a path also has a dot, a scheme or port, or a root.
47+
const isPath = body.includes("/")
48+
? /^~?\/|[.:]/.test(body)
49+
: FILE_EXTENSION.test(body.slice(body.lastIndexOf(".")));
50+
if (!isPath) continue;
4751
let closesInside = false;
4852
let start = body.indexOf("_");
4953
while (start !== -1) {

0 commit comments

Comments
 (0)