Skip to content

Handle .webp URL as an Image #1794

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

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/block/node/ImageNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import type { NodeCreator } from "./creator.ts";
import type { ImageNode, PlainNode } from "./type.ts";

const srcFirstStrongImageRegExp =
/\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?(?:\s+https?:\/\/[^\s\]]+)?\]/i;
/\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg|webp)(?:\?[^\]\s]+)?(?:\s+https?:\/\/[^\s\]]+)?\]/i;
const linkFirstStrongImageRegExp =
/\[https?:\/\/[^\s\]]+\s+https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?\]/i;
/\[https?:\/\/[^\s\]]+\s+https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg|webp)(?:\?[^\]\s]+)?\]/i;
const srcFirstStrongGyazoImageRegExp =
/\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?(?:\s+https?:\/\/[^\s\]]+)?\]/;
const linkFirstStrongGyazoImageRegExp =
/\[https?:\/\/[^\s\]]+\s+https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?\]/;

const isImageUrl = (text: string): boolean =>
/^https?:\/\/[^\s\]]+\.(png|jpe?g|gif|svg)(\?[^\]\s]+)?$/i.test(text) ||
/^https?:\/\/[^\s\]]+\.(png|jpe?g|gif|svg|webp)(\?[^\]\s]+)?$/i.test(text) ||
isGyazoImageUrl(text);

const isGyazoImageUrl = (text: string): boolean =>
Expand Down
2 changes: 1 addition & 1 deletion src/block/node/StrongImageNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createNodeParser } from "./creator.ts";
import type { NodeCreator } from "./creator.ts";
import type { PlainNode, StrongImageNode } from "./type.ts";

const strongImageRegExp = /\[\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)\]\]/i;
const strongImageRegExp = /\[\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg|webp)\]\]/i;
const strongGyazoImageRegExp =
/\[\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}\]\]/;

Expand Down
14 changes: 13 additions & 1 deletion tests/line/__snapshots__/image.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ exports[`image > HTTP jpeg image with special and japanese chars 1`] = `
]
`;

exports[`image > HTTPS svg and GIF image with link 1`] = `
exports[`image > HTTPS svg, GIF and WebP image with link 1`] = `
[
{
"indent": 0,
Expand All @@ -142,6 +142,18 @@ exports[`image > HTTPS svg and GIF image with link 1`] = `
],
"type": "line",
},
{
"indent": 0,
"nodes": [
{
"link": "https://example.com",
"raw": "[https://example.com/image.webp https://example.com]",
"src": "https://example.com/image.webp",
"type": "image",
},
],
"type": "line",
},
]
`;

Expand Down
11 changes: 11 additions & 0 deletions tests/line/__snapshots__/strongImage.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,16 @@ exports[`strongImage > Simple strong image 1`] = `
],
"type": "line",
},
{
"indent": 0,
"nodes": [
{
"raw": "[[https://example.com/image.webp]]",
"src": "https://example.com/image.webp",
"type": "strongImage",
},
],
"type": "line",
},
]
`;
3 changes: 2 additions & 1 deletion tests/line/image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ describe("image", () => {
).toMatchSnapshot();
});

it("HTTPS svg and GIF image with link", () => {
it("HTTPS svg, GIF and WebP image with link", () => {
expect(
parse(
`
[https://example.com/image.svg https://example.com/]
[https://example.com/ https://example.com/image.GIF]
[https://example.com/image.webp https://example.com]
`.trim(),
{
hasTitle: false,
Expand Down
1 change: 1 addition & 0 deletions tests/line/strongImage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe("strongImage", () => {
[[https://example.com/image.JPG]]
[[https://example.com/image.svg]]
[[https://example.com/image.GIF]]
[[https://example.com/image.webp]]
`.trim(),
{
hasTitle: false,
Expand Down
Loading