Keep asterisks between spaces in comment previews

"2 * 3 * 4" read as emphasis and lost its asterisks. Markdown doesn't
treat a marker with a space just inside it as emphasis.
This commit is contained in:
Rūdolfs Ošiņš
2026-10-01 18:20:24 +02:00
parent f6a23e0b0f
commit c7387ac540
2 changed files with 16 additions and 8 deletions
+6 -2
View File
@@ -9,8 +9,12 @@ export function plainText(body: string): string {
.replace(/`/gu, "")
.replace(/~~(.+?)~~/gu, "$1")
// Emphasis only where a marker sits on a word boundary, so `snake_case`,
// `MAX_SIZE` and `_private` survive being read as italics.
.replace(/(^|[^\w])[*_]{1,2}([^*_]+?)[*_]{1,2}(?=[^\w]|$)/gu, "$1$2")
// `MAX_SIZE` and `_private` survive being read as italics, and with no
// space just inside it, so `2 * 3 * 4` does too.
.replace(
/(^|[^\w])[*_]{1,2}(?=\S)([^*_]+?)(?<=\S)[*_]{1,2}(?=[^\w]|$)/gu,
"$1$2",
)
.replace(/^\s{0,3}#{1,6}\s+/gmu, "")
.replace(/^\s{0,3}>\s?/gmu, "")
.replace(/^\s{0,3}(?:[-+*]|\d+\.)\s+/gmu, "")
+10 -6
View File
@@ -19,9 +19,13 @@ test.each([
expect(plainText(markdown)).toBe(expected);
});
test.each(["snake_case_name", "MAX_SIZE", "_private", "a*b*c"])(
"leaves %j alone",
text => {
expect(plainText(text)).toBe(text);
},
);
test.each([
"snake_case_name",
"MAX_SIZE",
"_private",
"see _foo_bar",
"a*b*c",
"2 * 3 * 4",
])("leaves %j alone", text => {
expect(plainText(text)).toBe(text);
});