-
Notifications
You must be signed in to change notification settings - Fork 1
/
hasLink.ts
40 lines (38 loc) · 1.21 KB
/
hasLink.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Node } from "./deps/scrapbox-parser.ts";
import { parseLink } from "./parseLink.ts";
import { LinkTo } from "./types.ts";
import { toTitleLc } from "./deps/scrapbox-std.ts";
/** 指定したリンクがScrapboxのページ中に存在するか調べる
*
* @param link 調べたいリンクのタイトル
* @param nodes ページの構文解析結果
* @return 見つかったら`true`
*/
export const hasLink = (
link: LinkTo,
nodes: Node[],
): boolean =>
nodes.some((node) => {
const isRelative = !link.project;
switch (node.type) {
case "hashTag":
return isRelative &&
toTitleLc(node.href) === link.titleLc;
case "link": {
if (node.pathType == "absolute") return false;
if ((node.pathType === "relative") !== isRelative) return false;
const { project, title = "" } = parseLink({
pathType: node.pathType,
href: node.href,
});
return isRelative
? !project && toTitleLc(title) === link.titleLc
: project === link.project &&
toTitleLc(title) === link.titleLc;
}
case "quote":
case "strong":
case "decoration":
return hasLink(link, node.nodes);
}
});