Skip to content

✨ Send project links to scrapbox.io when updating External links in the page #133

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
Jul 30, 2023
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
21 changes: 18 additions & 3 deletions browser/websocket/makeChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -77,6 +85,8 @@ const findLinksAndImage = (text: string): [string[], string | null] => {
*/
const linksLc = new Map<string, boolean>();
const links = [] as string[];
const projectLinksLc = new Set<string>();
const projectLinks = [] as string[];
let image: string | null = null;

const lookup = (node: Node) => {
Expand All @@ -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;
Expand Down Expand Up @@ -125,7 +140,7 @@ const findLinksAndImage = (text: string): [string[], string | null] => {
lookup(node);
}

return [links, image];
return [links, projectLinks, image];
};

function* blocksToNodes(blocks: Iterable<Block>) {
Expand Down
5 changes: 4 additions & 1 deletion browser/websocket/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface HeadData {
image: string | null;
pin: number;
links: string[];
projectLinks: string[];
lines: Line[];
}
export const pull = async (
Expand All @@ -20,14 +21,16 @@ 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,
pageId: id,
persistent,
image,
links,
projectLinks,
pin,
lines,
};
Expand Down
4 changes: 2 additions & 2 deletions deps/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";