Skip to content

Extentional parser #95

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 5 commits into from
Dec 23, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- name: Run type check
run: deno check --remote ./**/*.ts
- name: Run test
run: deno test
run: deno test --allow-read --allow-write
2 changes: 1 addition & 1 deletion .github/workflows/udd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
run: >
deno run --allow-net --allow-read --allow-write=deps/
--allow-run=deno https://deno.land/x/[email protected]/main.ts deps/*.ts
--test="deno test"
--test="deno test --allow-read --allow-write"
- name: Create Pull Request
uses: peter-evans/create-pull-request@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions browser/websocket/makeChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import type { Change } from "../../deps/socket.ts";
import type { HeadData } from "./pull.ts";
import { toTitleLc } from "../../title.ts";
import { parseYoutube } from "../../parseYoutube.ts";
import { parseYoutube } from "../../parser/youtube.ts";

export interface Init {
userId: string;
Expand Down Expand Up @@ -95,7 +95,7 @@ const findLinksAndImage = (text: string): [string[], string | null] => {
return;
case "absolute": {
const props = parseYoutube(node.href);
if (!props) return;
if (!props || props.pathType === "list") return;
image ??= `https://i.ytimg.com/vi/${props.videoId}/mqdefault.jpg`;
return;
}
Expand Down
1 change: 1 addition & 0 deletions deps/testing.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "https://deno.land/[email protected]/testing/asserts.ts";
export * from "https://deno.land/[email protected]/testing/snapshot.ts";
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./rest/mod.ts";
export * from "./browser/mod.ts";
export * from "./title.ts";
export * from "./parseAbsoluteLink.ts";
121 changes: 121 additions & 0 deletions parseAbsoluteLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { LinkNode } from "./deps/scrapbox.ts";
import { parseYoutube } from "./parser/youtube.ts";
import { parseVimeo } from "./parser/vimeo.ts";
import { parseSpotify } from "./parser/spotify.ts";
import { parseAnchorFM } from "./parser/anchor-fm.ts";

export type { LinkNode };

export interface AbsoluteLinkNode {
type: "absoluteLink";
content: string;
href: string;
raw: string;
}

/** Youtube埋め込み */
export interface YoutubeNode {
type: "youtube";
videoId: string;
pathType: "com" | "dotbe" | "short";
params: URLSearchParams;
href: string;
raw: string;
}

/** Youtube List埋め込み */
export interface YoutubeListNode {
type: "youtube";
listId: string;
pathType: "list";
params: URLSearchParams;
href: string;
raw: string;
}

/** Vimeo埋め込み */
export interface VimeoNode {
type: "vimeo";
videoId: string;
href: string;
raw: string;
}

/** Spotify埋め込み */
export interface SpotifyNode {
type: "spotify";
videoId: string;
pathType: "track" | "artist" | "playlist" | "album" | "episode" | "show";
href: string;
raw: string;
}

/** Anchor FM埋め込み */
export interface AnchorFMNode {
type: "anchor-fm";
videoId: string;
href: string;
raw: string;
}

/** 動画埋め込み */
export interface VideoNode {
type: "video";
href: VideoURL;
raw: string;
}

/** 音声埋め込み */
export interface AudioNode {
type: "audio";
content: string;
href: AudioURL;
raw: string;
}

/** scrapbox-parserで解析した外部リンク記法を、埋め込み形式ごとに細かく解析する
*
* @param link scrapbox-parserで解析した外部リンク記法のobject
* @return 解析した記法のobject
*/
export const parseAbsoluteLink = (
link: LinkNode & { pathType: "absolute" },
):
| AbsoluteLinkNode
| VideoNode
| AudioNode
| YoutubeNode
| YoutubeListNode
| VimeoNode
| SpotifyNode
| AnchorFMNode => {
const { type: _, pathType: __, content, href, ...baseLink } = link;
if (content === "") {
const youtube = parseYoutube(href);
if (youtube) return { type: "youtube", href, ...youtube, ...baseLink };

const vimeoId = parseVimeo(href);
if (vimeoId) return { type: "vimeo", videoId: vimeoId, href, ...baseLink };

const spotify = parseSpotify(href);
if (spotify) return { type: "spotify", href, ...spotify, ...baseLink };

const anchorFMId = parseAnchorFM(href);
if (anchorFMId) {
return { type: "anchor-fm", videoId: anchorFMId, href, ...baseLink };
}

if (isVideoURL(href)) return { type: "video", href, ...baseLink };
}
if (isAudioURL(href)) return { type: "audio", content, href, ...baseLink };

return { type: "absoluteLink", content, href, ...baseLink };
};

type AudioURL = `${string}.${"mp3" | "ogg" | "wav" | "acc"}`;
const isAudioURL = (url: string): url is AudioURL =>
/\.(?:mp3|ogg|wav|aac)$/.test(url);

type VideoURL = `${string}.${"mp4" | "webm"}`;
const isVideoURL = (url: string): url is VideoURL =>
/\.(?:mp4|webm)$/.test(url);
11 changes: 11 additions & 0 deletions parser/__snapshots__/anchor-fm.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const snapshot = {};

snapshot[`spotify links > is 1`] = `"1-FM-e1gh6a7/a-a7m2veg"`;

snapshot[`spotify links > is not 1`] = `undefined`;

snapshot[`spotify links > is not 2`] = `undefined`;

snapshot[`spotify links > is not 3`] = `undefined`;

snapshot[`spotify links > is not 4`] = `undefined`;
37 changes: 37 additions & 0 deletions parser/__snapshots__/spotify.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const snapshot = {};

snapshot[`spotify links > is 1`] = `
{
pathType: "track",
videoId: "0rlYL6IQIwLZwYIguyy3l0",
}
`;

snapshot[`spotify links > is 2`] = `
{
pathType: "album",
videoId: "1bgUOjg3V0a7tvEfF1N6Kk",
}
`;

snapshot[`spotify links > is 3`] = `
{
pathType: "episode",
videoId: "0JtPGoprZK2WlYMjhFF2xD",
}
`;

snapshot[`spotify links > is 4`] = `
{
pathType: "playlist",
videoId: "2uOyQytSjDq9GF5z1RJj5w",
}
`;

snapshot[`spotify links > is not 1`] = `undefined`;

snapshot[`spotify links > is not 2`] = `undefined`;

snapshot[`spotify links > is not 3`] = `undefined`;

snapshot[`spotify links > is not 4`] = `undefined`;
11 changes: 11 additions & 0 deletions parser/__snapshots__/vimeo.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const snapshot = {};

snapshot[`vimeo links > is 1`] = `"121284607"`;

snapshot[`vimeo links > is not 1`] = `undefined`;

snapshot[`vimeo links > is not 2`] = `undefined`;

snapshot[`vimeo links > is not 3`] = `undefined`;

snapshot[`vimeo links > is not 4`] = `undefined`;
130 changes: 130 additions & 0 deletions parser/__snapshots__/youtube.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
export const snapshot = {};

snapshot[`youtube links > is 1`] = `
{
params: URLSearchParams {
[Symbol("[[webidl.brand]]")]: Symbol("[[webidl.brand]]"),
[Symbol(list)]: [
[
"v",
"LSvaOcaUQ3Y",
],
],
[Symbol("url object")]: URL {
hash: "",
host: "www.youtube.com",
hostname: "www.youtube.com",
href: "https://www.youtube.com/watch?v=LSvaOcaUQ3Y",
origin: "https://www.youtube.com",
password: "",
pathname: "/watch",
port: "",
protocol: "https:",
search: "?v=LSvaOcaUQ3Y",
username: "",
},
},
pathType: "com",
videoId: "LSvaOcaUQ3Y",
}
`;

snapshot[`youtube links > is 2`] = `
{
listId: "PLmoRDY8IgE2Okxy4WWdP95RHXOTGzJfQs",
params: URLSearchParams {
[Symbol("[[webidl.brand]]")]: Symbol("[[webidl.brand]]"),
[Symbol(list)]: [
[
"list",
"PLmoRDY8IgE2Okxy4WWdP95RHXOTGzJfQs",
],
],
[Symbol("url object")]: null,
},
pathType: "list",
}
`;

snapshot[`youtube links > is 3`] = `
{
params: URLSearchParams {
[Symbol("[[webidl.brand]]")]: Symbol("[[webidl.brand]]"),
[Symbol(list)]: [
[
"v",
"57rdbK4vmKE",
],
[
"list",
"PLmoRDY8IgE2Okxy4WWdP95RHXOTGzJfQs",
],
],
[Symbol("url object")]: URL {
hash: "",
host: "www.youtube.com",
hostname: "www.youtube.com",
href: "https://www.youtube.com/watch?v=57rdbK4vmKE&list=PLmoRDY8IgE2Okxy4WWdP95RHXOTGzJfQs",
origin: "https://www.youtube.com",
password: "",
pathname: "/watch",
port: "",
protocol: "https:",
search: "?v=57rdbK4vmKE&list=PLmoRDY8IgE2Okxy4WWdP95RHXOTGzJfQs",
username: "",
},
},
pathType: "com",
videoId: "57rdbK4vmKE",
}
`;

snapshot[`youtube links > is 4`] = `
{
params: URLSearchParams {
[Symbol("[[webidl.brand]]")]: Symbol("[[webidl.brand]]"),
[Symbol(list)]: [
[
"v",
"nj1cre2e6t0",
],
],
[Symbol("url object")]: URL {
hash: "",
host: "music.youtube.com",
hostname: "music.youtube.com",
href: "https://music.youtube.com/watch?v=nj1cre2e6t0",
origin: "https://music.youtube.com",
password: "",
pathname: "/watch",
port: "",
protocol: "https:",
search: "?v=nj1cre2e6t0",
username: "",
},
},
pathType: "com",
videoId: "nj1cre2e6t0",
}
`;

snapshot[`youtube links > is 5`] = `
{
params: URLSearchParams {
[Symbol("[[webidl.brand]]")]: Symbol("[[webidl.brand]]"),
[Symbol(list)]: [
],
[Symbol("url object")]: null,
},
pathType: "dotbe",
videoId: "nj1cre2e6t0",
}
`;

snapshot[`youtube links > is not 1`] = `undefined`;

snapshot[`youtube links > is not 2`] = `undefined`;

snapshot[`youtube links > is not 3`] = `undefined`;

snapshot[`youtube links > is not 4`] = `undefined`;
40 changes: 40 additions & 0 deletions parser/anchor-fm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { parseAnchorFM } from "./anchor-fm.ts";
import { assertSnapshot } from "../deps/testing.ts";

Deno.test("spotify links", async (t) => {
await t.step("is", async (t) => {
await assertSnapshot(
t,
parseAnchorFM(
"https://anchor.fm/notainc/episodes/1-FM-e1gh6a7/a-a7m2veg",
),
);
});

await t.step("is not", async (t) => {
await assertSnapshot(
t,
parseAnchorFM(
"https://gyazo.com/da78df293f9e83a74b5402411e2f2e01",
),
);
await assertSnapshot(
t,
parseAnchorFM(
"ほげほげ",
),
);
await assertSnapshot(
t,
parseAnchorFM(
"https://yourtube.com/watch?v=rafere",
),
);
await assertSnapshot(
t,
parseAnchorFM(
"https://example.com",
),
);
});
});
Loading