Skip to content

Commit 583dafc

Browse files
committed
Fix tests, lint repo
1 parent 5a06a01 commit 583dafc

File tree

13 files changed

+177
-88
lines changed

13 files changed

+177
-88
lines changed

packages/remend/__tests__/strikethrough.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ describe("strikethrough formatting (~~)", () => {
2727
expect(remend("~~xxx~")).toBe("~~xxx~~");
2828
expect(remend("~~strike text~")).toBe("~~strike text~~");
2929
expect(remend("Text with ~~strike~")).toBe("Text with ~~strike~~");
30-
expect(remend("This is ~~strikethrough~")).toBe("This is ~~strikethrough~~");
30+
expect(remend("This is ~~strikethrough~")).toBe(
31+
"This is ~~strikethrough~~"
32+
);
3133
});
3234
});

packages/remend/src/emphasis-handlers.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
boldItalicPattern,
33
boldPattern,
4+
doubleUnderscoreGlobalPattern,
45
fourOrMoreAsterisksPattern,
6+
halfCompleteUnderscorePattern,
57
italicPattern,
68
listItemPattern,
79
singleAsteriskPattern,
@@ -303,11 +305,13 @@ export const handleIncompleteDoubleUnderscoreItalic = (
303305
// Check for half-complete closing marker: __content_ should become __content__
304306
// The pattern /(__)([^_]*?)$/ won't match __content_ because it ends with _
305307
// So we need a separate check for this case
306-
const halfCompleteMatch = text.match(/(__)([^_]+)_$/);
308+
const halfCompleteMatch = text.match(halfCompleteUnderscorePattern);
307309
if (halfCompleteMatch) {
308310
const markerIndex = text.lastIndexOf(halfCompleteMatch[1]);
309311
if (!isWithinCodeBlock(text, markerIndex)) {
310-
const underscorePairs = (text.match(/__/g) || []).length;
312+
const underscorePairs = (
313+
text.match(doubleUnderscoreGlobalPattern) || []
314+
).length;
311315
if (underscorePairs % 2 === 1) {
312316
return `${text}_`;
313317
}

packages/remend/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { handleIncompleteSetextHeading } from "./setext-heading-handler";
1515
import { handleIncompleteStrikethrough } from "./strikethrough-handler";
1616

17+
export type { LinkMode } from "./link-image-handler";
1718
// biome-ignore lint/performance/noBarrelFile: "Re-exports utility functions for public API convenience"
1819
export {
1920
isWithinCodeBlock,
@@ -22,8 +23,6 @@ export {
2223
isWordChar,
2324
} from "./utils";
2425

25-
export type { LinkMode } from "./link-image-handler";
26-
2726
/**
2827
* Handler function that transforms text during streaming.
2928
*/
@@ -200,7 +199,8 @@ const getEnabledBuiltInHandlers = (
200199
return {
201200
handler: {
202201
...handler,
203-
handle: (text: string) => handleIncompleteLinksAndImages(text, linkMode),
202+
handle: (text: string) =>
203+
handleIncompleteLinksAndImages(text, linkMode),
204204
},
205205
// Only use early return for protocol mode (text-only won't end with the marker)
206206
earlyReturn: linkMode === "protocol" ? earlyReturn : undefined,

packages/remend/src/patterns.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ export const inlineTripleBacktickPattern = /^```[^`\n]*```?$/;
1212
export const fourOrMoreAsterisksPattern = /^\*{4,}$/;
1313
export const linkImagePattern = /(!?\[)([^\]]*?)$/;
1414
export const incompleteLinkUrlPattern = /(!?)\[([^\]]+)\](\([^)]+)$/;
15+
export const halfCompleteUnderscorePattern = /(__)([^_]+)_$/;
16+
export const halfCompleteTildePattern = /(~~)([^~]+)~$/;
17+
export const doubleUnderscoreGlobalPattern = /__/g;
18+
export const doubleTildeGlobalPattern = /~~/g;

packages/remend/src/strikethrough-handler.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { strikethroughPattern, whitespaceOrMarkersPattern } from "./patterns";
1+
import {
2+
doubleTildeGlobalPattern,
3+
halfCompleteTildePattern,
4+
strikethroughPattern,
5+
whitespaceOrMarkersPattern,
6+
} from "./patterns";
27

38
// Completes incomplete strikethrough formatting (~~)
49
export const handleIncompleteStrikethrough = (text: string): string => {
@@ -16,16 +21,16 @@ export const handleIncompleteStrikethrough = (text: string): string => {
1621
return text;
1722
}
1823

19-
const tildePairs = (text.match(/~~/g) || []).length;
24+
const tildePairs = (text.match(doubleTildeGlobalPattern) || []).length;
2025
if (tildePairs % 2 === 1) {
2126
return `${text}~~`;
2227
}
2328
} else {
2429
// Check for half-complete closing marker: ~~content~ should become ~~content~~
2530
// The pattern /(~~)([^~]*?)$/ won't match ~~content~ because it ends with ~
26-
const halfCompleteMatch = text.match(/(~~)([^~]+)~$/);
31+
const halfCompleteMatch = text.match(halfCompleteTildePattern);
2732
if (halfCompleteMatch) {
28-
const tildePairs = (text.match(/~~/g) || []).length;
33+
const tildePairs = (text.match(doubleTildeGlobalPattern) || []).length;
2934
if (tildePairs % 2 === 1) {
3035
return `${text}~`;
3136
}

packages/streamdown-code/__tests__/index.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ describe("highlight error handling", () => {
124124
{
125125
code: "const x = 1;",
126126
language: "javascript",
127-
themes: [
128-
"invalid-theme-that-does-not-exist",
129-
"another-invalid-theme",
130-
],
127+
themes: ["invalid-theme-that-does-not-exist", "another-invalid-theme"],
131128
},
132129
callback
133130
);

packages/streamdown/__tests__/cjk-friendly.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ describe("CJK (Chinese, Japanese, Korean) Friendly Support (#185)", () => {
280280
it("splits autolinks at CJK punctuation", () => {
281281
const autolinkContent = "请访问 https://example.com。谢谢";
282282
const { container } = render(
283-
<Streamdown plugins={{ cjk }}>{autolinkContent}</Streamdown>
283+
<Streamdown linkSafety={{ enabled: false }} plugins={{ cjk }}>
284+
{autolinkContent}
285+
</Streamdown>
284286
);
285287

286288
const link = container.querySelector('[data-streamdown="link"]');

packages/streamdown/__tests__/components.test.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import { render, waitFor } from "@testing-library/react";
22
import React from "react";
33
import { describe, expect, it } from "vitest";
4+
import { StreamdownContext, type StreamdownContextType } from "../index";
45
import { components as importedComponents } from "../lib/components";
56
import type { Options } from "../lib/markdown";
67

8+
const createContextValue = (
9+
linkSafety?: StreamdownContextType["linkSafety"]
10+
): StreamdownContextType => ({
11+
shikiTheme: ["github-light", "github-dark"],
12+
controls: true,
13+
isAnimating: false,
14+
mode: "streaming",
15+
mermaid: undefined,
16+
linkSafety,
17+
});
18+
719
// Type assertion: we know all components are defined in our implementation
820
type RequiredComponents = Required<NonNullable<Options["components"]>>;
921
const components = importedComponents as RequiredComponents;
@@ -151,9 +163,11 @@ describe("Markdown Components", () => {
151163
throw new Error("A component not found");
152164
}
153165
const { container } = render(
154-
<A href="https://example.com" node={null as any}>
155-
Link text
156-
</A>
166+
<StreamdownContext.Provider value={createContextValue()}>
167+
<A href="https://example.com" node={null as any}>
168+
Link text
169+
</A>
170+
</StreamdownContext.Provider>
157171
);
158172
const link = container.querySelector("a");
159173
expect(link).toBeTruthy();
@@ -171,9 +185,11 @@ describe("Markdown Components", () => {
171185
throw new Error("A component not found");
172186
}
173187
const { container } = render(
174-
<A href="streamdown:incomplete-link" node={null as any}>
175-
Incomplete link text
176-
</A>
188+
<StreamdownContext.Provider value={createContextValue()}>
189+
<A href="streamdown:incomplete-link" node={null as any}>
190+
Incomplete link text
191+
</A>
192+
</StreamdownContext.Provider>
177193
);
178194
// Should render a normal anchor with data-incomplete attribute
179195
const link = container.querySelector('a[data-streamdown="link"]');

0 commit comments

Comments
 (0)