-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
promote local media assets to file attachments #250
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
848bb4e
promote local media assets to file attachments
cinxmo f05a582
dedup files and update tests
cinxmo eb7af1d
Merge branch 'main' into cindy/file-attachments
cinxmo a9f60dd
Update src/markdown.ts
Fil 7934c22
Merge branch 'main' into cindy/file-attachments
Fil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
import assert from "node:assert"; | ||
import {normalizePieceHtml} from "../src/markdown.js"; | ||
|
||
const html = (strings, ...values) => String.raw({raw: strings}, ...values); | ||
const mockContext = () => ({files: [], imports: [], pieces: [], startLine: 0, currentLine: 0}); | ||
|
||
describe("file attachments", () => { | ||
describe("added", () => { | ||
const sourcePath = "/attachments.md"; | ||
|
||
it("img[src]", () => { | ||
const htmlStr = html`<img src="./test.png">`; | ||
const expected = html`<img src="./_file/test.png">`; | ||
const context = mockContext(); | ||
const actual = normalizePieceHtml(htmlStr, sourcePath, context); | ||
|
||
assert.equal(actual, expected); | ||
assert.deepEqual(context.files, [ | ||
{ | ||
mimeType: "image/png", | ||
name: "./test.png", | ||
path: "./_file/test.png" | ||
} | ||
]); | ||
}); | ||
|
||
it("img[srcset]", () => { | ||
const htmlStr = html` | ||
<img | ||
srcset="small.jpg 480w, large.jpg 800w" | ||
sizes="(max-width: 600px) 480px, | ||
800px" | ||
src="large.jpg" | ||
alt="Image for testing" | ||
/> | ||
`; | ||
const expected = html` | ||
<img srcset="./_file/small.jpg 480w, ./_file/large.jpg 800w" sizes="(max-width: 600px) 480px, | ||
800px" src="./_file/large.jpg" alt="Image for testing"> | ||
`; | ||
const context = mockContext(); | ||
const actual = normalizePieceHtml(htmlStr, sourcePath, context); | ||
|
||
assert.equal(actual, expected); | ||
assert.deepEqual(context.files, [ | ||
{ | ||
mimeType: "image/jpeg", | ||
name: "large.jpg", | ||
path: "./_file/large.jpg" | ||
}, | ||
{ | ||
mimeType: "image/jpeg", | ||
name: "small.jpg", | ||
path: "./_file/small.jpg" | ||
} | ||
]); | ||
}); | ||
|
||
it("video[src]", () => { | ||
const htmlStr = html`<video src="observable.mov" controls> | ||
Your browser doesn't support HTML video. | ||
</video>`; | ||
const expected = html`<video src="./_file/observable.mov" controls> | ||
Your browser doesn't support HTML video. | ||
</video>`; | ||
const context = mockContext(); | ||
const actual = normalizePieceHtml(htmlStr, sourcePath, context); | ||
|
||
assert.equal(actual, expected); | ||
assert.deepEqual(context.files, [ | ||
{ | ||
mimeType: "video/quicktime", | ||
name: "observable.mov", | ||
path: "./_file/observable.mov" | ||
} | ||
]); | ||
}); | ||
|
||
it("video source[src]", () => { | ||
const htmlStr = html`<video width="320" height="240" controls> | ||
<source src="observable.mp4" type="video/mp4"> | ||
<source src="observable.mov" type="video/mov"> | ||
Your browser doesn't support HTML video. | ||
</video>`; | ||
|
||
const expected = html`<video width="320" height="240" controls> | ||
<source src="./_file/observable.mp4" type="video/mp4"> | ||
<source src="./_file/observable.mov" type="video/mov"> | ||
Your browser doesn't support HTML video. | ||
</video>`; | ||
|
||
const context = mockContext(); | ||
const actual = normalizePieceHtml(htmlStr, sourcePath, context); | ||
|
||
assert.equal(actual, expected); | ||
assert.deepEqual(context.files, [ | ||
{ | ||
mimeType: "video/mp4", | ||
name: "observable.mp4", | ||
path: "./_file/observable.mp4" | ||
}, | ||
{ | ||
mimeType: "video/quicktime", | ||
name: "observable.mov", | ||
path: "./_file/observable.mov" | ||
} | ||
]); | ||
}); | ||
|
||
it("picture source[srcset]", () => { | ||
const htmlStr = html`<picture> | ||
<source srcset="observable-logo-wide.png" media="(min-width: 600px)"/> | ||
<img src="observable-logo-narrow.png" /> | ||
</picture>`; | ||
|
||
const expected = html`<picture> | ||
<source srcset="./_file/observable-logo-wide.png" media="(min-width: 600px)"> | ||
<img src="./_file/observable-logo-narrow.png"> | ||
</picture>`; | ||
|
||
const context = mockContext(); | ||
const actual = normalizePieceHtml(htmlStr, sourcePath, context); | ||
|
||
assert.equal(actual, expected); | ||
assert.deepEqual(context.files, [ | ||
{ | ||
mimeType: "image/png", | ||
name: "observable-logo-narrow.png", | ||
path: "./_file/observable-logo-narrow.png" | ||
}, | ||
{ | ||
mimeType: "image/png", | ||
name: "observable-logo-wide.png", | ||
path: "./_file/observable-logo-wide.png" | ||
} | ||
]); | ||
}); | ||
}); | ||
|
||
describe("not added", () => { | ||
const sourcePath = "/attachments.md"; | ||
|
||
it("img[src] only adds local files", () => { | ||
const htmlStr = html`<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/American_Shorthair.jpg/900px-American_Shorthair.jpg">`; | ||
const expected = html`<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/American_Shorthair.jpg/900px-American_Shorthair.jpg">`; | ||
const context = mockContext(); | ||
const actual = normalizePieceHtml(htmlStr, sourcePath, context); | ||
|
||
assert.equal(actual, expected); | ||
assert.deepEqual(context.files, []); | ||
}); | ||
|
||
it("img[srcset] only adds local files", () => { | ||
const htmlStr = html` | ||
<img | ||
srcset="small.jpg 480w, https://upload.wikimedia.org/900px-American_Shorthair.jpg 900w" | ||
sizes="(max-width: 600px) 480px, 900px" | ||
src="https://upload.wikimedia.org/900px-American_Shorthair.jpg" | ||
alt="Cat image for testing" | ||
/> | ||
`; | ||
const expected = html` | ||
<img srcset="./_file/small.jpg 480w, https://upload.wikimedia.org/900px-American_Shorthair.jpg 900w" sizes="(max-width: 600px) 480px, 900px" src="https://upload.wikimedia.org/900px-American_Shorthair.jpg" alt="Cat image for testing"> | ||
`; | ||
const context = mockContext(); | ||
const actual = normalizePieceHtml(htmlStr, sourcePath, context); | ||
|
||
assert.equal(actual, expected); | ||
assert.deepEqual(context.files, [ | ||
{ | ||
mimeType: "image/jpeg", | ||
name: "small.jpg", | ||
path: "./_file/small.jpg" | ||
} | ||
]); | ||
}); | ||
|
||
it("video source[src] only adds local files", () => { | ||
const htmlStr = html`<video width="320" height="240" controls> | ||
<source src="https://www.youtube.com/watch?v=SsFyayu5csc" type="video/youtube"/> | ||
<source src="observable.mov" type="video/mov"> | ||
Your browser doesn't support HTML video. | ||
</video>`; | ||
|
||
const expected = html`<video width="320" height="240" controls> | ||
<source src="https://www.youtube.com/watch?v=SsFyayu5csc" type="video/youtube"> | ||
<source src="./_file/observable.mov" type="video/mov"> | ||
Your browser doesn't support HTML video. | ||
</video>`; | ||
|
||
const context = mockContext(); | ||
const actual = normalizePieceHtml(htmlStr, sourcePath, context); | ||
|
||
assert.equal(actual, expected); | ||
assert.deepEqual(context.files, [ | ||
{ | ||
mimeType: "video/quicktime", | ||
name: "observable.mov", | ||
path: "./_file/observable.mov" | ||
} | ||
]); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I purposely didn't add
a[href]
because we already link to local.md
files. See test case:https://github.com/observablehq/cli/blob/67ebf20b3c1b993f9fc664fd8cd827353d371b65/test/input/build/config/index.md?plain=1#L3-L4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question of
a[href]
highlights a small flaw in our logic.Here are three cases:
[another page](page)
— the page is served from page.md, it's not a file attachment.[view source](file.md)
— file.md should be a file attachment<a href="earthquakes.csv" download>download dataset</a>
— data.csv should be a file attachmentCurrently we base our choice on the tag: in the current approach
audio
,video
,link
… are never pointing to pages, and thus indicate a file attachment, and converselya
always indicates a page and never a file attachment.But shouldn't this be associated to the URL routing instead? If the URL is routed to a md source (or to a static asset #169), it's not a file attachment; otherwise it's a (potential) file attachment.
That new logic could be applied to all the tags. Of course in practice an
audio[src]
will never be a page (since all pages generate HTML, which are not suitable for audio); but alink[href]
can be a page — e.g.,<link rel="next">
[ref].There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Note that this issue will be solved with #257.)