|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { rehypeTextDirection } from "./markdownTextDirection"; |
| 4 | + |
| 5 | +const text = (value: string) => ({ type: "text", value }); |
| 6 | +const element = (tagName: string, children: object[]) => ({ |
| 7 | + type: "element", |
| 8 | + tagName, |
| 9 | + properties: {} as Record<string, unknown>, |
| 10 | + children, |
| 11 | +}); |
| 12 | + |
| 13 | +function directed(...blocks: ReturnType<typeof element>[]) { |
| 14 | + rehypeTextDirection()({ type: "root", children: blocks }); |
| 15 | + return blocks; |
| 16 | +} |
| 17 | + |
| 18 | +describe("rehypeTextDirection", () => { |
| 19 | + it("keeps raw HTML list items apart when counting words", () => { |
| 20 | + const [list] = directed( |
| 21 | + element("ul", [element("li", [text("مرحبا")]), element("li", [text("hello world")])]), |
| 22 | + ); |
| 23 | + expect(list!.properties.dir).toBe("ltr"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("leaves inline code out of the count and isolates it", () => { |
| 27 | + const code = element("code", [text("useEffect useLayoutEffect React")]); |
| 28 | + const [paragraph] = directed(element("p", [text("استخدم "), code, text(" هنا")])); |
| 29 | + expect(paragraph!.properties.dir).toBe("rtl"); |
| 30 | + expect(code.properties.dir).toBe("ltr"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("leaves code blocks to their own left-to-right frame", () => { |
| 34 | + const code = element("code", [text("// احسب المجموع هنا")]); |
| 35 | + const [pre] = directed(element("pre", [code])); |
| 36 | + expect(pre!.properties.dir).toBeUndefined(); |
| 37 | + expect(code.properties.dir).toBeUndefined(); |
| 38 | + }); |
| 39 | +}); |
0 commit comments