|
| 1 | +import assert from "node:assert"; |
| 2 | +import {convertNode, convertNodes, inferFileName, resolveInput} from "../src/convert.js"; |
| 3 | + |
| 4 | +describe("convertNodes", () => { |
| 5 | + it("converts multiple nodes", () => { |
| 6 | + assert.strictEqual( |
| 7 | + convertNodes([ |
| 8 | + {mode: "md", value: "Hello, world!"}, |
| 9 | + {mode: "js", value: "1 + 2"} |
| 10 | + ]), |
| 11 | + "Hello, world!\n\n```js\n1 + 2\n```\n" |
| 12 | + ); |
| 13 | + }); |
| 14 | +}); |
| 15 | + |
| 16 | +describe("convertNode", () => { |
| 17 | + it("passes through Markdown, adding a newline", () => { |
| 18 | + assert.strictEqual(convertNode({mode: "md", value: "Hello, world!"}), "Hello, world!\n"); |
| 19 | + assert.strictEqual(convertNode({mode: "md", value: "# Hello, world!"}), "# Hello, world!\n"); |
| 20 | + assert.strictEqual(convertNode({mode: "md", value: "# Hello, ${'world'}!"}), "# Hello, ${'world'}!\n"); |
| 21 | + }); |
| 22 | + it("wraps JavaScript in a fenced code block", () => { |
| 23 | + assert.strictEqual(convertNode({mode: "js", value: "1 + 2"}), "```js\n1 + 2\n```\n"); |
| 24 | + }); |
| 25 | + it("converts pinned to echo", () => { |
| 26 | + assert.strictEqual(convertNode({mode: "js", pinned: true, value: "1 + 2"}), "```js echo\n1 + 2\n```\n"); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("inferFileName", () => { |
| 31 | + it("infers a suitable file name based on identifier", () => { |
| 32 | + assert.strictEqual(inferFileName("https://api.observablehq.com/document/1111111111111111"), "1111111111111111.md"); |
| 33 | + }); |
| 34 | + it("infers a suitable file name based on slug", () => { |
| 35 | + assert.strictEqual(inferFileName("https://api.observablehq.com/document/@d3/bar-chart"), "bar-chart.md"); |
| 36 | + }); |
| 37 | + it("handles a slug with a suffix", () => { |
| 38 | + assert.strictEqual(inferFileName("https://api.observablehq.com/document/@d3/bar-chart/2"), "bar-chart,2.md"); |
| 39 | + }); |
| 40 | + it("handles a different origin", () => { |
| 41 | + assert.strictEqual(inferFileName("https://api.example.com/document/@d3/bar-chart"), "bar-chart.md"); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +/* prettier-ignore */ |
| 46 | +describe("resolveInput", () => { |
| 47 | + it("resolves document identifiers", () => { |
| 48 | + assert.strictEqual(resolveInput("1111111111111111"), "https://api.observablehq.com/document/1111111111111111"); |
| 49 | + assert.strictEqual(resolveInput("1234567890abcdef"), "https://api.observablehq.com/document/1234567890abcdef"); |
| 50 | + }); |
| 51 | + it("resolves document slugs", () => { |
| 52 | + assert.strictEqual(resolveInput("@d3/bar-chart"), "https://api.observablehq.com/document/@d3/bar-chart"); |
| 53 | + assert.strictEqual(resolveInput("@d3/bar-chart/2"), "https://api.observablehq.com/document/@d3/bar-chart/2"); |
| 54 | + }); |
| 55 | + it("resolves document versions", () => { |
| 56 | + assert.strictEqual(resolveInput("1234567890abcdef@123"), "https://api.observablehq.com/document/1234567890abcdef@123"); |
| 57 | + assert.strictEqual(resolveInput("1234567890abcdef@latest"), "https://api.observablehq.com/document/1234567890abcdef@latest"); |
| 58 | + assert.strictEqual(resolveInput("1234567890abcdef~0"), "https://api.observablehq.com/document/1234567890abcdef~0"); |
| 59 | + assert.strictEqual(resolveInput("@d3/bar-chart@123"), "https://api.observablehq.com/document/@d3/bar-chart@123"); |
| 60 | + assert.strictEqual(resolveInput("@d3/bar-chart@latest"), "https://api.observablehq.com/document/@d3/bar-chart@latest"); |
| 61 | + assert.strictEqual(resolveInput("@d3/bar-chart~0"), "https://api.observablehq.com/document/@d3/bar-chart~0"); |
| 62 | + assert.strictEqual(resolveInput("@d3/bar-chart/2@123"), "https://api.observablehq.com/document/@d3/bar-chart/2@123"); |
| 63 | + assert.strictEqual(resolveInput("@d3/bar-chart/2@latest"), "https://api.observablehq.com/document/@d3/bar-chart/2@latest"); |
| 64 | + assert.strictEqual(resolveInput("@d3/bar-chart/2~0"), "https://api.observablehq.com/document/@d3/bar-chart/2~0"); |
| 65 | + }); |
| 66 | + it("resolves urls", () => { |
| 67 | + assert.strictEqual(resolveInput("https://observablehq.com/1234567890abcdef"), "https://api.observablehq.com/document/1234567890abcdef"); |
| 68 | + assert.strictEqual(resolveInput("https://observablehq.com/1234567890abcdef@123"), "https://api.observablehq.com/document/1234567890abcdef@123"); |
| 69 | + assert.strictEqual(resolveInput("https://observablehq.com/1234567890abcdef@latest"), "https://api.observablehq.com/document/1234567890abcdef@latest"); |
| 70 | + assert.strictEqual(resolveInput("https://observablehq.com/1234567890abcdef~0"), "https://api.observablehq.com/document/1234567890abcdef~0"); |
| 71 | + assert.strictEqual(resolveInput("https://observablehq.com/@d3/bar-chart"), "https://api.observablehq.com/document/@d3/bar-chart"); |
| 72 | + assert.strictEqual(resolveInput("https://observablehq.com/@d3/bar-chart@123"), "https://api.observablehq.com/document/@d3/bar-chart@123"); |
| 73 | + assert.strictEqual(resolveInput("https://observablehq.com/@d3/bar-chart@latest"), "https://api.observablehq.com/document/@d3/bar-chart@latest"); |
| 74 | + assert.strictEqual(resolveInput("https://observablehq.com/@d3/bar-chart~0"), "https://api.observablehq.com/document/@d3/bar-chart~0"); |
| 75 | + assert.strictEqual(resolveInput("https://observablehq.com/@d3/bar-chart/2"), "https://api.observablehq.com/document/@d3/bar-chart/2"); |
| 76 | + assert.strictEqual(resolveInput("https://observablehq.com/@d3/bar-chart/2@123"), "https://api.observablehq.com/document/@d3/bar-chart/2@123"); |
| 77 | + assert.strictEqual(resolveInput("https://observablehq.com/@d3/bar-chart/2@latest"), "https://api.observablehq.com/document/@d3/bar-chart/2@latest"); |
| 78 | + assert.strictEqual(resolveInput("https://observablehq.com/@d3/bar-chart/2~0"), "https://api.observablehq.com/document/@d3/bar-chart/2~0"); |
| 79 | + }); |
| 80 | + it("preserves the specified host", () => { |
| 81 | + assert.strictEqual(resolveInput("https://example.com/1234567890abcdef"), "https://api.example.com/document/1234567890abcdef"); |
| 82 | + assert.strictEqual(resolveInput("https://example.com/1234567890abcdef@123"), "https://api.example.com/document/1234567890abcdef@123"); |
| 83 | + assert.strictEqual(resolveInput("https://example.com/1234567890abcdef@latest"), "https://api.example.com/document/1234567890abcdef@latest"); |
| 84 | + assert.strictEqual(resolveInput("https://example.com/1234567890abcdef~0"), "https://api.example.com/document/1234567890abcdef~0"); |
| 85 | + assert.strictEqual(resolveInput("https://example.com/@d3/bar-chart"), "https://api.example.com/document/@d3/bar-chart"); |
| 86 | + assert.strictEqual(resolveInput("https://example.com/@d3/bar-chart@123"), "https://api.example.com/document/@d3/bar-chart@123"); |
| 87 | + assert.strictEqual(resolveInput("https://example.com/@d3/bar-chart@latest"), "https://api.example.com/document/@d3/bar-chart@latest"); |
| 88 | + assert.strictEqual(resolveInput("https://example.com/@d3/bar-chart~0"), "https://api.example.com/document/@d3/bar-chart~0"); |
| 89 | + assert.strictEqual(resolveInput("https://example.com/@d3/bar-chart/2"), "https://api.example.com/document/@d3/bar-chart/2"); |
| 90 | + assert.strictEqual(resolveInput("https://example.com/@d3/bar-chart/2@123"), "https://api.example.com/document/@d3/bar-chart/2@123"); |
| 91 | + assert.strictEqual(resolveInput("https://example.com/@d3/bar-chart/2@latest"), "https://api.example.com/document/@d3/bar-chart/2@latest"); |
| 92 | + assert.strictEqual(resolveInput("https://example.com/@d3/bar-chart/2~0"), "https://api.example.com/document/@d3/bar-chart/2~0"); |
| 93 | + }); |
| 94 | +}); |
0 commit comments