diff --git a/browser/websocket/makeChanges.ts b/browser/websocket/makeChanges.ts index 2dd0758..ea1b8fd 100644 --- a/browser/websocket/makeChanges.ts +++ b/browser/websocket/makeChanges.ts @@ -39,12 +39,12 @@ export function makeChanges( } // リンクと画像の差分を入れる - const [linksLc, image] = findLinksAndImage(right_.join("\n")); + const [links, image] = findLinksAndImage(right_.join("\n")); if ( - head.linksLc.length !== linksLc.length || - !head.linksLc.every((link) => linksLc.includes(link)) + head.links.length !== links.length || + !head.links.every((link) => links.includes(link)) ) { - changes.push({ links: linksLc }); + changes.push({ links }); } if (head.image !== image) { changes.push({ image }); @@ -67,19 +67,30 @@ function findLinksAndImage(text: string): [string[], string | null] { } }); - const linksLc = [] as string[]; + /** 重複判定用map + * + * bracket link とhashtagを区別できるようにしている + * - bracket linkならtrue + * + * linkの形状はbracket linkを優先している + */ + const linksLc = new Map(); + const links = [] as string[]; let image: string | null = null; const lookup = (node: Node) => { switch (node.type) { case "hashTag": - linksLc.push(toTitleLc(node.href)); + if (linksLc.has(toTitleLc(node.href))) return; + linksLc.set(toTitleLc(node.href), false); + links.push(node.href); return; - case "link": { + case "link": if (node.pathType !== "relative") return; - linksLc.push(toTitleLc(node.href)); + if (linksLc.get(toTitleLc(node.href))) return; + linksLc.set(toTitleLc(node.href), true); + links.push(node.href); return; - } case "image": case "strongImage": { image ??= node.src.endsWith("/thumb/1000") @@ -103,7 +114,7 @@ function findLinksAndImage(text: string): [string[], string | null] { lookup(node); } - return [linksLc, image]; + return [links, image]; } function* blocksToNodes(blocks: Iterable) { diff --git a/browser/websocket/pull.ts b/browser/websocket/pull.ts index bd1dcfb..e974137 100644 --- a/browser/websocket/pull.ts +++ b/browser/websocket/pull.ts @@ -1,5 +1,4 @@ import type { Line } from "../../deps/scrapbox.ts"; -import { toTitleLc } from "../../title.ts"; import { getPage } from "../../rest/pages.ts"; export interface HeadData { @@ -8,7 +7,7 @@ export interface HeadData { persistent: boolean; image: string | null; pin: number; - linksLc: string[]; + links: string[]; lines: Line[]; } export async function pull(project: string, title: string): Promise { @@ -25,7 +24,7 @@ export async function pull(project: string, title: string): Promise { pageId: id, persistent, image, - linksLc: links.map((link) => toTitleLc(link)), + links, pin, lines, };