Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { rehypeTextDirection } from "./markdownTextDirection";
4
5const text = (value: string) => ({ type: "text", value });
6const element = (
7 tagName: string,
8 children: object[],
9 properties: Record<string, unknown> = {},
10) => ({
11 type: "element",
12 tagName,
13 properties,
14 children,
15});
16
17function directed(...blocks: ReturnType<typeof element>[]) {
18 rehypeTextDirection()({ type: "root", children: blocks });
19 return blocks;
20}
21
22describe("rehypeTextDirection", () => {
23 it("keeps raw HTML list items apart when counting words", () => {
24 const [list] = directed(
25 element("ul", [element("li", [text("مرحبا")]), element("li", [text("hello world")])]),
26 );
27 expect(list!.properties.dir).toBe("ltr");
28 });
29
30 it("leaves inline code out of the count and isolates it", () => {
31 const code = element("code", [text("useEffect useLayoutEffect React")]);
32 const [paragraph] = directed(element("p", [text("استخدم "), code, text(" هنا")]));
33 expect(paragraph!.properties.dir).toBe("rtl");
34 expect(code.properties.dir).toBe("ltr");
35 });
36
37 it("leaves code blocks to their own left-to-right frame", () => {
38 const code = element("code", [text("// احسب المجموع هنا")]);
39 const [pre] = directed(element("pre", [code]));
40 expect(pre!.properties.dir).toBeUndefined();
41 expect(code.properties.dir).toBeUndefined();
42 });
43
44 it("keeps authored ltr/rtl dir and replaces other dir values with detected direction", () => {
45 const cases = [
46 ["rtl", "Hello world", "rtl"],
47 ["ltr", "مرحبا بالعالم", "ltr"],
48 ["RTL", "Hello world", "RTL"],
49 ["auto", "مرحبا بالعالم", "rtl"],
50 [" rtl", "مرحبا بالعالم", "rtl"],
51 ] as const;
52 for (const [authored, sample, expected] of cases) {
53 const [block] = directed(element("p", [text(sample)], { dir: authored }));
54 expect(block!.properties.dir, `dir=${JSON.stringify(authored)}`).toBe(expected);
55 }
56 });
57});