generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreplacements.test.ts
84 lines (80 loc) · 2.87 KB
/
replacements.test.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { SmartLinksPattern, parseNextLink } from './replacements';
import {describe, expect, test} from '@jest/globals';
test('SmartLinkPattern creation', () => {
const pattern = new SmartLinksPattern("T(\\d+)", "https://phabricator.wikimedia.org/T$1");
expect(pattern.match("unrelated")).toBeFalsy();
expect(pattern.match("T1234")).toBeTruthy();
expect(pattern.match(" T1234 ")).toBeTruthy();
expect(pattern.match("Foo T1234 Foo")).toBeTruthy();
expect(pattern.match("FooT1234 Foo")).toBeFalsy();
});
test('parseNextLink basic', () => {
const pattern = new SmartLinksPattern("T(\\d+)", "https://phabricator.wikimedia.org/T$1");
expect(parseNextLink("Unrelated text", pattern)).toStrictEqual({found:false, remaining:"Unrelated text"});
expect(parseNextLink("T1234", pattern)).toStrictEqual({
found: true,
preText: "",
link: "T1234",
href: "https://phabricator.wikimedia.org/T1234",
remaining: "",
});
expect(parseNextLink(" T1234 ", pattern)).toStrictEqual({
found: true,
preText: " ",
link: "T1234",
href: "https://phabricator.wikimedia.org/T1234",
remaining: " ",
});
expect(parseNextLink("T1234.", pattern)).toStrictEqual({
found: true,
preText: "",
link: "T1234",
href: "https://phabricator.wikimedia.org/T1234",
remaining: ".",
});
expect(parseNextLink("Text\nT1234.", pattern)).toStrictEqual({
found: true,
preText: "Text\n",
link: "T1234",
href: "https://phabricator.wikimedia.org/T1234",
remaining: ".",
});
expect(parseNextLink("TICKET1234", pattern)).toStrictEqual({
found: false,
remaining: "TICKET1234",
});
});
test('parseNextLink non-word', () => {
const pattern = new SmartLinksPattern("\\$([A-Z]+)", "https://finance.yahoo.com/quote/$1");
expect(parseNextLink("$GOOG", pattern)).toStrictEqual({
found: true,
preText: "",
link: "$GOOG",
href: "https://finance.yahoo.com/quote/GOOG",
remaining: "",
});
expect(parseNextLink(" $GOOG ", pattern)).toStrictEqual({
found: true,
preText: " ",
link: "$GOOG",
href: "https://finance.yahoo.com/quote/GOOG",
remaining: " ",
});
});
test('parseNextLink hash', () => {
const pattern = new SmartLinksPattern("#(\\d+)", "https://github.com/kemayo/obsidian-smart-links/issues/$1");
expect(parseNextLink("#2", pattern)).toStrictEqual({
found: true,
preText: "",
link: "#2",
href: "https://github.com/kemayo/obsidian-smart-links/issues/2",
remaining: "",
});
expect(parseNextLink(" #2 ", pattern)).toStrictEqual({
found: true,
preText: " ",
link: "#2",
href: "https://github.com/kemayo/obsidian-smart-links/issues/2",
remaining: " ",
});
});