Skip to content
TypeScript

fix(client-runtime): pull request bodies with CRLF line endings keep their code blocks whole (#30352)

MarcoDotIO4821696509d2
diff --git a/packages/client-runtime/src/pullRequestMarkdown.test.ts b/packages/client-runtime/src/pullRequestMarkdown.test.ts
index 8fab1fdd3b6..a5508e81974 100644
--- a/packages/client-runtime/src/pullRequestMarkdown.test.ts
+++ b/packages/client-runtime/src/pullRequestMarkdown.test.ts
@@ -80,6 +80,27 @@ describe("pull request body segmentation", () => {
]);
});
+ // GitHub keeps CRLF in a body written in its web editor.
+ it("recognizes fences in a body with CRLF line endings", () => {
+ const body = "```\r\nhttps://example.com/demo.mp4\r\n```";
+ expect(splitPullRequestBody(body)).toEqual([
+ { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
+ ]);
+ });
+
+ it("closes a CRLF fence, so a video after it still lifts out", () => {
+ const code = '```html\r\n<video src="https://example.com/example.mp4"></video>\r\n```';
+ expect(splitPullRequestBody(`${code}\r\n\r\nhttps://example.com/demo.mp4\r\n`)).toEqual([
+ { id: "markdown:0", kind: "markdown", text: code, sourceOffset: 0 },
+ {
+ id: "attachment:1",
+ kind: "attachment",
+ url: "https://example.com/demo.mp4",
+ media: "video",
+ },
+ ]);
+ });
+
it("does not let a different fence marker close an open fence", () => {
const body = "```\n~~~\nhttps://example.com/demo.mp4\n```";
expect(splitPullRequestBody(body)).toEqual([
diff --git a/packages/client-runtime/src/pullRequestMarkdown.ts b/packages/client-runtime/src/pullRequestMarkdown.ts
index 9524f3e660e..92c7d6de70c 100644
--- a/packages/client-runtime/src/pullRequestMarkdown.ts
+++ b/packages/client-runtime/src/pullRequestMarkdown.ts
@@ -16,7 +16,8 @@ export type PullRequestBodySegment =
readonly media: "video" | "image" | "unknown";
};
-const FENCE_PATTERN = /^\s{0,3}((?:`{3,})|(?:~{3,}))(.*)$/u;
+// `.` stops at the `\r` of a CRLF body, which is how GitHub stores one written in its web editor.
+const FENCE_PATTERN = /^\s{0,3}((?:`{3,})|(?:~{3,}))(.*)\r?$/u;
/**
* How far a `<video>` tag may reach for its closing tag. Real embeds are one to three lines,
* and the bound keeps an unclosed tag from making each one rescan the rest of the body.