Skip to content

Commit 8a6df46

Browse files
authored
Merge pull request #133 from takker99/external-links
✨ Send project links to scrapbox.io when updating External links in the page
2 parents 54dc7ef + b0dac30 commit 8a6df46

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

browser/websocket/makeChanges.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,28 @@ export function* makeChanges(
4242
}
4343

4444
// リンクと画像の差分を入れる
45-
const [links, image] = findLinksAndImage(right_.join("\n"));
45+
const [links, projectLinks, image] = findLinksAndImage(right_.join("\n"));
4646
if (
4747
head.links.length !== links.length ||
4848
!head.links.every((link) => links.includes(link))
4949
) {
5050
yield { links };
5151
}
52+
if (
53+
head.projectLinks.length !== projectLinks.length ||
54+
!head.projectLinks.every((link) => projectLinks.includes(link))
55+
) {
56+
yield { projectLinks };
57+
}
5258
if (head.image !== image) {
5359
yield { image };
5460
}
5561
}
5662

5763
/** テキストに含まれる全てのリンクと最初の画像を探す */
58-
const findLinksAndImage = (text: string): [string[], string | null] => {
64+
const findLinksAndImage = (
65+
text: string,
66+
): [string[], string[], string | null] => {
5967
const rows = parseToRows(text);
6068
const blocks = packRows(rows, { hasTitle: true }).flatMap((pack) => {
6169
switch (pack.type) {
@@ -77,6 +85,8 @@ const findLinksAndImage = (text: string): [string[], string | null] => {
7785
*/
7886
const linksLc = new Map<string, boolean>();
7987
const links = [] as string[];
88+
const projectLinksLc = new Set<string>();
89+
const projectLinks = [] as string[];
8090
let image: string | null = null;
8191

8292
const lookup = (node: Node) => {
@@ -93,6 +103,11 @@ const findLinksAndImage = (text: string): [string[], string | null] => {
93103
linksLc.set(toTitleLc(node.href), true);
94104
links.push(node.href);
95105
return;
106+
case "root":
107+
if (projectLinksLc.has(toTitleLc(node.href))) return;
108+
projectLinksLc.add(toTitleLc(node.href));
109+
projectLinks.push(node.href);
110+
return;
96111
case "absolute": {
97112
const props = parseYoutube(node.href);
98113
if (!props || props.pathType === "list") return;
@@ -125,7 +140,7 @@ const findLinksAndImage = (text: string): [string[], string | null] => {
125140
lookup(node);
126141
}
127142

128-
return [links, image];
143+
return [links, projectLinks, image];
129144
};
130145

131146
function* blocksToNodes(blocks: Iterable<Block>) {

browser/websocket/pull.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface HeadData {
88
image: string | null;
99
pin: number;
1010
links: string[];
11+
projectLinks: string[];
1112
lines: Line[];
1213
}
1314
export const pull = async (
@@ -20,14 +21,16 @@ export const pull = async (
2021
if (!result.ok) {
2122
throw new Error(`You have no privilege of editing "/${project}/${title}".`);
2223
}
23-
const { commitId, persistent, image, links, lines, id, pin } = result.value;
24+
const { commitId, persistent, image, links, projectLinks, lines, id, pin } =
25+
result.value;
2426

2527
return {
2628
commitId,
2729
pageId: id,
2830
persistent,
2931
image,
3032
links,
33+
projectLinks,
3134
pin,
3235
lines,
3336
};

deps/socket.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export type {
1010
ProjectUpdatesStreamEvent,
1111
Socket,
1212
UpdateCommit,
13-
} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.4/mod.ts";
13+
} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.5/mod.ts";
1414
export {
1515
socketIO,
1616
wrap,
17-
} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.4/mod.ts";
17+
} from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-websocket/0.1.5/mod.ts";

0 commit comments

Comments
 (0)