|
| 1 | +import assert from "node:assert"; |
| 2 | +import {normalizePieceHtml} from "../src/markdown.js"; |
| 3 | +import {resolvePath} from "../src/url.js"; |
| 4 | + |
| 5 | +const html = (strings, ...values) => String.raw({raw: strings}, ...values); |
| 6 | +const mockContext = () => ({files: [], imports: [], pieces: [], startLine: 0, currentLine: 0}); |
| 7 | + |
| 8 | +describe("file attachments", () => { |
| 9 | + describe("success", () => { |
| 10 | + const sourcePath = "/attachments.md"; |
| 11 | + |
| 12 | + it("img[src]", () => { |
| 13 | + const htmlStr = html`<img src="./test.png">`; |
| 14 | + const expected = html`<img src="./_file/test.png">`; |
| 15 | + const context = mockContext(); |
| 16 | + const actual = normalizePieceHtml(htmlStr, sourcePath, context); |
| 17 | + |
| 18 | + assert.equal(expected, actual); |
| 19 | + assert.deepEqual(context.files, [ |
| 20 | + { |
| 21 | + mimeType: "image/png", |
| 22 | + name: "./test.png", |
| 23 | + path: "./_file/test.png" |
| 24 | + } |
| 25 | + ]); |
| 26 | + }); |
| 27 | + |
| 28 | + it("video[src]", () => { |
| 29 | + const htmlStr = html`<video src="observable.mov" controls> |
| 30 | + Learn more about Observable CLI |
| 31 | + </video>`; |
| 32 | + const expected = html`<video src="./_file/observable.mov" controls> |
| 33 | + Learn more about Observable CLI |
| 34 | + </video>`; |
| 35 | + const context = mockContext(); |
| 36 | + const actual = normalizePieceHtml(htmlStr, sourcePath, context); |
| 37 | + |
| 38 | + assert.equal(expected, actual); |
| 39 | + assert.deepEqual(context.files, [ |
| 40 | + { |
| 41 | + mimeType: "video/quicktime", |
| 42 | + name: "observable.mov", |
| 43 | + path: "./_file/observable.mov" |
| 44 | + } |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("video source[src]", () => { |
| 49 | + const htmlStr = html`<video width="320" height="240" controls> |
| 50 | + <source src="observable.mp4" type="video/mp4"> |
| 51 | + <source src="observable.mov" type="video/mov"> |
| 52 | + Learn more about Observable CLI |
| 53 | + </video>`; |
| 54 | + |
| 55 | + const expected = html`<video width="320" height="240" controls> |
| 56 | + <source src="./_file/observable.mp4" type="video/mp4"> |
| 57 | + <source src="./_file/observable.mov" type="video/mov"> |
| 58 | + Learn more about Observable CLI |
| 59 | + </video>`; |
| 60 | + |
| 61 | + const context = mockContext(); |
| 62 | + const actual = normalizePieceHtml(htmlStr, sourcePath, context); |
| 63 | + |
| 64 | + assert.equal(expected, actual); |
| 65 | + assert.deepEqual(context.files, [ |
| 66 | + { |
| 67 | + mimeType: "video/mp4", |
| 68 | + name: "observable.mp4", |
| 69 | + path: "./_file/observable.mp4" |
| 70 | + }, |
| 71 | + { |
| 72 | + mimeType: "video/quicktime", |
| 73 | + name: "observable.mov", |
| 74 | + path: "./_file/observable.mov" |
| 75 | + } |
| 76 | + ]); |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments