|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { markdownToIR } from "./ir.js"; |
| 3 | + |
| 4 | +const DUNDER_OPTIONS = { |
| 5 | + autolink: false, |
| 6 | + linkify: false, |
| 7 | + preserveDunderIdentifiers: true, |
| 8 | +} as const; |
| 9 | + |
| 10 | +describe("markdownToIR preserveDunderIdentifiers", () => { |
| 11 | + it("leaves CommonMark emphasis unchanged by default", () => { |
| 12 | + expect(markdownToIR("obj.__class__", { autolink: false, linkify: false })).toEqual({ |
| 13 | + text: "obj.class", |
| 14 | + styles: [{ start: 4, end: 9, style: "bold" }], |
| 15 | + links: [], |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + it("keeps ordinary parenthesized bold unchanged when enabled", () => { |
| 20 | + expect(markdownToIR("(__warning__)", DUNDER_OPTIONS)).toEqual({ |
| 21 | + text: "(warning)", |
| 22 | + styles: [{ start: 1, end: 8, style: "bold" }], |
| 23 | + links: [], |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it.each([ |
| 28 | + ["member access", "obj.__class__"], |
| 29 | + ["call", "__init__()"], |
| 30 | + ["function argument", "print(__name__)"], |
| 31 | + ["index", '__dict__["key"]'], |
| 32 | + ])("preserves %s identifiers", (_name, source) => { |
| 33 | + expect(markdownToIR(source, DUNDER_OPTIONS)).toEqual({ |
| 34 | + text: source, |
| 35 | + styles: [], |
| 36 | + links: [], |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it.each([ |
| 41 | + { |
| 42 | + name: "full", |
| 43 | + source: "[Class][obj.__class__]\n\n[obj.__class__]: https://example.org/python", |
| 44 | + text: "Class", |
| 45 | + end: 5, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "collapsed", |
| 49 | + source: "[obj.__class__][]\n\n[obj.__class__]: https://example.org/python", |
| 50 | + text: "obj.__class__", |
| 51 | + end: 13, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "shortcut", |
| 55 | + source: "[obj.__class__]\n\n[obj.__class__]: https://example.org/python", |
| 56 | + text: "obj.__class__", |
| 57 | + end: 13, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "case-normalized", |
| 61 | + source: "[Class][OBJ.__CLASS__]\n\n[obj.__class__]: https://example.org/python", |
| 62 | + text: "Class", |
| 63 | + end: 5, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "whitespace-normalized", |
| 67 | + source: "[Class][obj. __class__]\n\n[obj.\t__class__]: https://example.org/python", |
| 68 | + text: "Class", |
| 69 | + end: 5, |
| 70 | + }, |
| 71 | + ])("preserves $name reference links", ({ source, text, end }) => { |
| 72 | + expect(markdownToIR(source, DUNDER_OPTIONS)).toEqual({ |
| 73 | + text, |
| 74 | + styles: [], |
| 75 | + links: [{ start: 0, end, href: "https://example.org/python" }], |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("preserves repeated reference destinations", () => { |
| 80 | + const href = "https://docs.python.org/3/library/stdtypes.html#instance.__class__"; |
| 81 | + expect( |
| 82 | + markdownToIR(`[Class][docs] and [Type][docs]\n\n[docs]: ${href}`, DUNDER_OPTIONS), |
| 83 | + ).toEqual({ |
| 84 | + text: "Class and Type", |
| 85 | + styles: [], |
| 86 | + links: [ |
| 87 | + { start: 0, end: 5, href }, |
| 88 | + { start: 10, end: 14, href }, |
| 89 | + ], |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it.each([ |
| 94 | + { |
| 95 | + name: "inline", |
| 96 | + source: "`obj.__class__`", |
| 97 | + text: "obj.__class__", |
| 98 | + styles: [{ start: 0, end: 13, style: "code" }], |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "fenced", |
| 102 | + source: "```python\nobj.__class__\n```", |
| 103 | + text: "obj.__class__\n", |
| 104 | + styles: [{ start: 0, end: 14, style: "code_block", language: "python" }], |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "indented", |
| 108 | + source: " obj.__class__", |
| 109 | + text: "obj.__class__\n", |
| 110 | + styles: [{ start: 0, end: 14, style: "code_block" }], |
| 111 | + }, |
| 112 | + ])("leaves $name code handling unchanged", ({ source, text, styles }) => { |
| 113 | + expect(markdownToIR(source, DUNDER_OPTIONS)).toEqual({ |
| 114 | + text, |
| 115 | + styles, |
| 116 | + links: [], |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments