From b0dac309f7006d6632c4976843d1157bf5df5f92 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Sun, 30 Jul 2023 17:26:08 +0900 Subject: [PATCH] :sparkles: Send project links to scrapbox.io when updating External links in the page --- browser/websocket/makeChanges.ts | 21 ++++++++++++++++++--- browser/websocket/pull.ts | 5 ++++- deps/socket.ts | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/browser/websocket/makeChanges.ts b/browser/websocket/makeChanges.ts index fa07cd2..79b762b 100644 --- a/browser/websocket/makeChanges.ts +++ b/browser/websocket/makeChanges.ts @@ -42,20 +42,28 @@ export function* makeChanges( } // リンクと画像の差分を入れる - const [links, image] = findLinksAndImage(right_.join("\n")); + const [links, projectLinks, image] = findLinksAndImage(right_.join("\n")); if ( head.links.length !== links.length || !head.links.every((link) => links.includes(link)) ) { yield { links }; } + if ( + head.projectLinks.length !== projectLinks.length || + !head.projectLinks.every((link) => projectLinks.includes(link)) + ) { + yield { projectLinks }; + } if (head.image !== image) { yield { image }; } } /** テキストに含まれる全てのリンクと最初の画像を探す */ -const findLinksAndImage = (text: string): [string[], string | null] => { +const findLinksAndImage = ( + text: string, +): [string[], string[], string | null] => { const rows = parseToRows(text); const blocks = packRows(rows, { hasTitle: true }).flatMap((pack) => { switch (pack.type) { @@ -77,6 +85,8 @@ const findLinksAndImage = (text: string): [string[], string | null] => { */ const linksLc = new Map(); const links = [] as string[]; + const projectLinksLc = new Set(); + const projectLinks = [] as string[]; let image: string | null = null; const lookup = (node: Node) => { @@ -93,6 +103,11 @@ const findLinksAndImage = (text: string): [string[], string | null] => { linksLc.set(toTitleLc(node.href), true); links.push(node.href); return; + case "root": + if (projectLinksLc.has(toTitleLc(node.href))) return; + projectLinksLc.add(toTitleLc(node.href)); + projectLinks.push(node.href); + return; case "absolute": { const props = parseYoutube(node.href); if (!props || props.pathType === "list") return; @@ -125,7 +140,7 @@ const findLinksAndImage = (text: string): [string[], string | null] => { lookup(node); } - return [links, image]; + return [links, projectLinks, image]; }; function* blocksToNodes(blocks: Iterable) { diff --git a/browser/websocket/pull.ts b/browser/websocket/pull.ts index 9417a83..d3996bc 100644 --- a/browser/websocket/pull.ts +++ b/browser/websocket/pull.ts @@ -8,6 +8,7 @@ export interface HeadData { image: string | null; pin: number; links: string[]; + projectLinks: string[]; lines: Line[]; } export const pull = async ( @@ -20,7 +21,8 @@ export const pull = async ( if (!result.ok) { throw new Error(`You have no privilege of editing "/${project}/${title}".`); } - const { commitId, persistent, image, links, lines, id, pin } = result.value; + const { commitId, persistent, image, links, projectLinks, lines, id, pin } = + result.value; return { commitId, @@ -28,6 +30,7 @@ export const pull = async ( persistent, image, links, + projectLinks, pin, lines, }; diff --git a/deps/socket.ts b/deps/socket.ts index 2bd0b14..3091245 100644 --- a/deps/socket.ts +++ b/deps/socket.ts @@ -10,8 +10,8 @@ export type { ProjectUpdatesStreamEvent, Socket, UpdateCommit, -} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.4/mod.ts"; +} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.5/mod.ts"; export { socketIO, wrap, -} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.4/mod.ts"; +} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.5/mod.ts";