Skip to content
TypeScript

fix(web,mobile): pull request text breaks lines when providers do (#26060)

Cody Ray Hoefte428a2c1f52b
diff --git a/apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx b/apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx
index 63946016d59..b840ce47d9e 100644
--- a/apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx
+++ b/apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx
@@ -11,7 +11,10 @@ import {
import { videoMimeType } from "@t3tools/shared/video";
import { createContext, useContext, useMemo, useState } from "react";
import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";
-import type { ReferenceAutolink } from "@t3tools/client-runtime/source-control-clients";
+import {
+ sourceControlClients,
+ type ReferenceAutolink,
+} from "@t3tools/client-runtime/source-control-clients";
import { linkReferenceAutolinks } from "./pullRequestAutolinks";
import { useOpenPullRequest } from "../../native/useOpenPullRequest";
@@ -31,6 +34,7 @@ import type { MarkdownImageRenderer } from "../../native/SelectableMarkdownText"
const AutolinksContext = createContext<((text: string) => ReadonlyArray<ReferenceAutolink>) | null>(
null,
);
+const LineBreaksContext = createContext(false);
export function PullRequestMarkdownRepository(props: {
/** The change request the markdown belongs to, whose host decides what its text links. */
@@ -44,6 +48,7 @@ export function PullRequestMarkdownRepository(props: {
provider === undefined || url === undefined
? null
: changeRequestReferenceAutolinker({ provider, url });
+ const lineBreaks = sourceControlClients.find(provider ?? "")?.hardLineBreaks ?? false;
const target = pullRequestTarget(props.params);
const project = useProject({
environmentId: target.environmentId,
@@ -53,7 +58,9 @@ export function PullRequestMarkdownRepository(props: {
const media = cwd ? { environmentId: target.environmentId, cwd } : null;
return (
<AutolinksContext value={autolinks}>
- <PullRequestMediaContext value={media}>{props.children}</PullRequestMediaContext>
+ <LineBreaksContext value={lineBreaks}>
+ <PullRequestMediaContext value={media}>{props.children}</PullRequestMediaContext>
+ </LineBreaksContext>
</AutolinksContext>
);
}
@@ -88,6 +95,7 @@ function MarkdownImage(props: {
export function PullRequestMarkdown(props: {
readonly markdown: string;
readonly imageGroupMarkdown?: string;
+ readonly lineBreaks?: boolean;
}) {
const segments = useMemo(() => splitPullRequestBody(props.markdown), [props.markdown]);
return (
@@ -98,6 +106,7 @@ export function PullRequestMarkdown(props: {
key={segment.id}
markdown={segment.text}
imageGroupMarkdown={props.imageGroupMarkdown ?? props.markdown}
+ lineBreaks={props.lineBreaks}
/>
) : (
<PullRequestMedia key={segment.id} url={segment.url} kind={segment.media} />
@@ -110,7 +119,9 @@ export function PullRequestMarkdown(props: {
function PullRequestMarkdownContent(props: {
readonly markdown: string;
readonly imageGroupMarkdown?: string;
+ readonly lineBreaks: boolean | undefined;
}) {
+ const hostLineBreaks = useContext(LineBreaksContext);
const autolinks = useContext(AutolinksContext);
const mediaContext = useContext(PullRequestMediaContext);
const markdown = linkReferenceAutolinks(props.markdown, autolinks);
@@ -136,6 +147,7 @@ function PullRequestMarkdownContent(props: {
<FilePreviewGallery value={gallery}>
<SelectableMarkdownText
markdown={markdown}
+ preserveSoftBreaks={props.lineBreaks ?? hostLineBreaks}
textStyle={styles.nativeTextStyle}
onLinkPress={openLink}
renderImage={renderImage}
diff --git a/apps/mobile/src/features/pull-requests/guide/GuideOutline.tsx b/apps/mobile/src/features/pull-requests/guide/GuideOutline.tsx
index 1308d4caee7..9fd845eff92 100644
--- a/apps/mobile/src/features/pull-requests/guide/GuideOutline.tsx
+++ b/apps/mobile/src/features/pull-requests/guide/GuideOutline.tsx
@@ -59,7 +59,7 @@ export function GuideOutline({
</View>
{guide.stops[0]?.summary ? (
<View className="mb-6">
- <PullRequestMarkdown markdown={guide.stops[0].summary} />
+ <PullRequestMarkdown lineBreaks={false} markdown={guide.stops[0].summary} />
</View>
) : null}
{guide.stops.map((stop, position) => {
diff --git a/apps/mobile/src/features/pull-requests/guide/PullRequestGuideScreen.tsx b/apps/mobile/src/features/pull-requests/guide/PullRequestGuideScreen.tsx
index caa1e72938c..3cae97487f9 100644
--- a/apps/mobile/src/features/pull-requests/guide/PullRequestGuideScreen.tsx
+++ b/apps/mobile/src/features/pull-requests/guide/PullRequestGuideScreen.tsx
@@ -188,7 +188,7 @@ export function PullRequestGuideStopScreen({ route }: StaticScreenProps<GuideSto
<>
<GuideNotice state={state} />
<Text className="text-2xl leading-8 font-t3-semibold">{stop.title}</Text>
- <PullRequestMarkdown markdown={stop.summary} />
+ <PullRequestMarkdown lineBreaks={false} markdown={stop.summary} />
{stop.attention ? (
<View className="gap-3 rounded-xl border border-border bg-grouped-card p-4">
<View className="flex-row items-center gap-2">
@@ -197,7 +197,7 @@ export function PullRequestGuideStopScreen({ route }: StaticScreenProps<GuideSto
{attentionLabels[stop.attention.kind]}
</Text>
</View>
- <PullRequestMarkdown markdown={stop.attention.note} />
+ <PullRequestMarkdown lineBreaks={false} markdown={stop.attention.note} />
</View>
) : null}
{stale ? (
@@ -237,7 +237,7 @@ export function PullRequestGuideStopScreen({ route }: StaticScreenProps<GuideSto
<Text className="text-xs text-foreground-muted">
{note.path.split("/").at(-1)}:{note.line}
</Text>
- <PullRequestMarkdown markdown={note.body} />
+ <PullRequestMarkdown lineBreaks={false} markdown={note.body} />
</View>
</View>
))}
@@ -266,7 +266,7 @@ export function PullRequestGuideStopScreen({ route }: StaticScreenProps<GuideSto
<View className="gap-3">
<Text className="font-t3-semibold">Questions to consider</Text>
{stop.questions.map((question) => (
- <PullRequestMarkdown key={question} markdown={question} />
+ <PullRequestMarkdown key={question} lineBreaks={false} markdown={question} />
))}
</View>
) : null}
diff --git a/apps/mobile/src/features/pull-requests/guide/ReviewGuideExcerpt.tsx b/apps/mobile/src/features/pull-requests/guide/ReviewGuideExcerpt.tsx
index aa5f9e8d87f..c280c6ea465 100644
--- a/apps/mobile/src/features/pull-requests/guide/ReviewGuideExcerpt.tsx
+++ b/apps/mobile/src/features/pull-requests/guide/ReviewGuideExcerpt.tsx
@@ -55,7 +55,9 @@ export function ReviewGuideExcerpt({
const [NativeDiff] = useState(() => resolveNativeReviewDiffView());
return (
<View className="gap-3">
- {excerpt.caption.trim() ? <PullRequestMarkdown markdown={excerpt.caption} /> : null}
+ {excerpt.caption.trim() ? (
+ <PullRequestMarkdown lineBreaks={false} markdown={excerpt.caption} />
+ ) : null}
<View className="overflow-hidden rounded-lg border border-border-subtle">
<Pressable
accessibilityRole="button"
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
index 135ae2970ce..6fd281aa145 100644
--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
@@ -239,7 +239,11 @@ function pullRequestMarkdownContext(
detail: Pick<PullRequestDetail, "provider" | "url"> | null,
threadRef: ScopedThreadRef | null,
) {
- return { autolinks: changeRequestReferenceAutolinker(detail), threadRef };
+ return {
+ autolinks: changeRequestReferenceAutolinker(detail),
+ lineBreaks: sourceControlClients.find(detail?.provider ?? "")?.hardLineBreaks ?? false,
+ threadRef,
+ };
}
function pullRequestStackReference(
diff --git a/apps/web/src/components/pullRequest/PullRequestMarkdown.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestMarkdown.browser.test.tsx
new file mode 100644
index 00000000000..46a7e882843
--- /dev/null
+++ b/apps/web/src/components/pullRequest/PullRequestMarkdown.browser.test.tsx
@@ -0,0 +1,45 @@
+import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
+import { EnvironmentId } from "@t3tools/contracts";
+import { expect, it } from "vite-plus/test";
+
+import { render } from "../../../test/render";
+import { PullRequestMarkdown, PullRequestMarkdownContext } from "./PullRequestMarkdown";
+
+async function renderBody(host: string, options: { lineBreaks?: boolean } = {}) {
+ const context = {
+ autolinks: null,
+ lineBreaks: sourceControlClients.get(host).hardLineBreaks ?? false,
+ threadRef: null,
+ };
+ return render(
+ <PullRequestMarkdownContext value={context}>
+ <PullRequestMarkdown
+ text={"first line\nsecond line"}
+ cwd="/repo"
+ environmentId={EnvironmentId.make("local")}
+ {...options}
+ />
+ </PullRequestMarkdownContext>,
+ );
+}
+
+it.each(["github", "forgejo", "azure-devops"])("%s breaks a single newline", async (host) => {
+ await using screen = await renderBody(host);
+ await expect.element(screen.getByText(/first line/)).toBeInTheDocument();
+ expect(screen.container.querySelectorAll("p br")).toHaveLength(1);
+});
+
+it.each(["gitlab", "bitbucket", "gitcafe", "unknown"])(
+ "%s joins a single newline",
+ async (host) => {
+ await using screen = await renderBody(host);
+ await expect.element(screen.getByText(/first line/)).toBeInTheDocument();
+ expect(screen.container.querySelectorAll("p br")).toHaveLength(0);
+ },
+);
+
+it("lets text the host did not write opt out of the host's breaks", async () => {
+ await using screen = await renderBody("github", { lineBreaks: false });
+ await expect.element(screen.getByText(/first line/)).toBeInTheDocument();
+ expect(screen.container.querySelectorAll("p br")).toHaveLength(0);
+});
diff --git a/apps/web/src/components/pullRequest/PullRequestMarkdown.tsx b/apps/web/src/components/pullRequest/PullRequestMarkdown.tsx
index 7086d5441bc..b0e2c8de64c 100644
--- a/apps/web/src/components/pullRequest/PullRequestMarkdown.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestMarkdown.tsx
@@ -23,6 +23,7 @@ import { remarkPullRequestAutolinks, splitPullRequestBody } from "./pullRequestM
export const PullRequestMarkdownContext = createContext<{
autolinks: ((text: string) => ReadonlyArray<ReferenceAutolink>) | null;
+ lineBreaks: boolean;
threadRef: ScopedThreadRef | null;
} | null>(null);
@@ -59,12 +60,14 @@ export function PullRequestMarkdown({
cwd,
environmentId,
threadRef,
+ lineBreaks,
className,
}: {
text: string;
cwd: string;
environmentId: EnvironmentId;
threadRef?: ScopedThreadRef | null;
+ lineBreaks?: boolean;
className?: string;
}) {
const segments = splitPullRequestBody(text);
@@ -93,6 +96,7 @@ export function PullRequestMarkdown({
pullRequestPanelRef={resolvedThreadRef ?? PULL_REQUESTS_PANEL_REF}
environmentId={environmentId}
extraRemarkPlugins={extraRemarkPlugins}
+ lineBreaks={lineBreaks ?? context?.lineBreaks ?? false}
sourceControlMedia
/>
);
diff --git a/apps/web/src/components/pullRequest/guide/GuideProse.tsx b/apps/web/src/components/pullRequest/guide/GuideProse.tsx
index e117191fb97..adcdb7cd0a8 100644
--- a/apps/web/src/components/pullRequest/guide/GuideProse.tsx
+++ b/apps/web/src/components/pullRequest/guide/GuideProse.tsx
@@ -26,6 +26,7 @@ export function GuideProse({
text={text}
cwd={context.cwd}
environmentId={context.environmentId}
+ lineBreaks={false}
className={cn("space-y-0 [&_.chat-markdown]:text-inherit", className)}
/>
);
diff --git a/packages/source-control-azure-devops/src/shared/definition.ts b/packages/source-control-azure-devops/src/shared/definition.ts
index 2dd03667474..e0491368d72 100644
--- a/packages/source-control-azure-devops/src/shared/definition.ts
+++ b/packages/source-control-azure-devops/src/shared/definition.ts
@@ -117,6 +117,7 @@ export const definition = defineSourceControl({
changeRequestCloseCommand:
/\baz\s+repos\s+pr\s+update\b[^;&|]*--status[\s=]+["']?(?:completed|abandoned)\b/u,
authorProfileUrl: () => null,
+ hardLineBreaks: true,
reviewSummaryRequired: () => false,
checkoutCommandArgument: (input) => {
const args = CHECKOUT_COMMAND.exec(input)?.[1];
diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts
index e9e68325b74..c766a92a5a5 100644
--- a/packages/source-control-core/src/shared/definition.ts
+++ b/packages/source-control-core/src/shared/definition.ts
@@ -286,6 +286,8 @@ export interface SourceControlDefinition {
text: string,
repositoryUrl: string,
) => ReadonlyArray<ReferenceAutolink>;
+ /** True when the host renders a single newline in change request text as a line break. */
+ readonly hardLineBreaks?: boolean;
/** Where the host lists a change request's checks. Absent means the change request's own page. */
readonly changeRequestChecksUrl?: (changeRequestUrl: string) => string;
/**
diff --git a/packages/source-control-forgejo/src/shared/definition.ts b/packages/source-control-forgejo/src/shared/definition.ts
index 2cf8ac5bb58..c75b071c2ab 100644
--- a/packages/source-control-forgejo/src/shared/definition.ts
+++ b/packages/source-control-forgejo/src/shared/definition.ts
@@ -95,6 +95,7 @@ export const definition = defineSourceControl({
: null,
changeRequestCloseCommand: /\b(?:tea\s+(?:pr|pulls)|fj\s+pr)\s+(?:merge|close)\b/u,
authorProfileUrl: () => null,
+ hardLineBreaks: true,
// Forgejo refuses a change request review without a summary, even with inline comments.
reviewSummaryRequired: (verdict) => verdict === "request-changes",
checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
diff --git a/packages/source-control-github/src/shared/definition.ts b/packages/source-control-github/src/shared/definition.ts
index 6ac8aa305f3..8244e10d9e7 100644
--- a/packages/source-control-github/src/shared/definition.ts
+++ b/packages/source-control-github/src/shared/definition.ts
@@ -132,6 +132,7 @@ export const definition = defineSourceControl({
changeRequestActionsWhenUnreported: new Set(["close", "merge", "ready", "reopen"] as const),
// One search reads every repository on the host.
changeRequestListScope: "host",
+ hardLineBreaks: true,
reviewSummaryRequired: () => false,
checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),