Skip to content

Commit

Permalink
Merge pull request #1794 from rokoucha/support-webp-in-image-node
Browse files Browse the repository at this point in the history
Handle .webp URL as an Image
  • Loading branch information
progfay authored Dec 3, 2024
2 parents 7fd7482 + 8c95ef1 commit 3775cfa
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
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

0 comments on commit 3775cfa

Please sign in to comment.