Skip to content

Commit 7e9d615

Browse files
authored
Merge pull request #18 from takker99/preserve-letters
Preserve letters
2 parents bd9769a + b20054e commit 7e9d615

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

browser/websocket/makeChanges.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ export function makeChanges(
3939
}
4040

4141
// リンクと画像の差分を入れる
42-
const [linksLc, image] = findLinksAndImage(right_.join("\n"));
42+
const [links, image] = findLinksAndImage(right_.join("\n"));
4343
if (
44-
head.linksLc.length !== linksLc.length ||
45-
!head.linksLc.every((link) => linksLc.includes(link))
44+
head.links.length !== links.length ||
45+
!head.links.every((link) => links.includes(link))
4646
) {
47-
changes.push({ links: linksLc });
47+
changes.push({ links });
4848
}
4949
if (head.image !== image) {
5050
changes.push({ image });
@@ -67,19 +67,30 @@ function findLinksAndImage(text: string): [string[], string | null] {
6767
}
6868
});
6969

70-
const linksLc = [] as string[];
70+
/** 重複判定用map
71+
*
72+
* bracket link とhashtagを区別できるようにしている
73+
* - bracket linkならtrue
74+
*
75+
* linkの形状はbracket linkを優先している
76+
*/
77+
const linksLc = new Map<string, boolean>();
78+
const links = [] as string[];
7179
let image: string | null = null;
7280

7381
const lookup = (node: Node) => {
7482
switch (node.type) {
7583
case "hashTag":
76-
linksLc.push(toTitleLc(node.href));
84+
if (linksLc.has(toTitleLc(node.href))) return;
85+
linksLc.set(toTitleLc(node.href), false);
86+
links.push(node.href);
7787
return;
78-
case "link": {
88+
case "link":
7989
if (node.pathType !== "relative") return;
80-
linksLc.push(toTitleLc(node.href));
90+
if (linksLc.get(toTitleLc(node.href))) return;
91+
linksLc.set(toTitleLc(node.href), true);
92+
links.push(node.href);
8193
return;
82-
}
8394
case "image":
8495
case "strongImage": {
8596
image ??= node.src.endsWith("/thumb/1000")
@@ -103,7 +114,7 @@ function findLinksAndImage(text: string): [string[], string | null] {
103114
lookup(node);
104115
}
105116

106-
return [linksLc, image];
117+
return [links, image];
107118
}
108119

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

browser/websocket/pull.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Line } from "../../deps/scrapbox.ts";
2-
import { toTitleLc } from "../../title.ts";
32
import { getPage } from "../../rest/pages.ts";
43

54
export interface HeadData {
@@ -8,7 +7,7 @@ export interface HeadData {
87
persistent: boolean;
98
image: string | null;
109
pin: number;
11-
linksLc: string[];
10+
links: string[];
1211
lines: Line[];
1312
}
1413
export async function pull(project: string, title: string): Promise<HeadData> {
@@ -25,7 +24,7 @@ export async function pull(project: string, title: string): Promise<HeadData> {
2524
pageId: id,
2625
persistent,
2726
image,
28-
linksLc: links.map((link) => toTitleLc(link)),
27+
links,
2928
pin,
3029
lines,
3130
};

0 commit comments

Comments
 (0)