-
Notifications
You must be signed in to change notification settings - Fork 6
/
markdown_test.ts
165 lines (132 loc) · 4.78 KB
/
markdown_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { assertEquals } from "https://deno.land/std@0.202.0/assert/mod.ts";
import { parseMarkdown } from "./markdown.ts";
Deno.test("parseMarkdown should return html, links, and headings", () => {
const markdown = `## Hello World\nThis is a [link](some-file.md).`;
const expected = `<h2 id="hello-world"><a class="h-anchor" href="#hello-world">#</a>Hello World</h2><p>This is a <a href="/some-file">link</a>.</p>\n`;
const { html, links, headings } = parseMarkdown({
text: markdown,
currentPath: "/some-page",
baseUrl: new URL("https://example.com/"),
});
assertEquals(html, expected);
assertEquals(links, [new URL("https://example.com/some-file")]);
assertEquals(headings, [
{ text: "Hello World", level: 2, slug: "hello-world" },
]);
});
Deno.test("Markdown with external links", () => {
const markdown = `This is a [link](https://example.com).`;
const expected = `<p>This is a <a href="https://example.com" rel="external noopener noreferrer">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: ".",
baseUrl: new URL("https://example.com/"),
});
assertEquals(html, expected);
});
Deno.test("Markdown with internal links", () => {
const markdown = `This is a [link](/notes).`;
const expected = `<p>This is a <a href="/notes">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: ".",
baseUrl: new URL("https://example.com/"),
});
assertEquals(html, expected);
});
Deno.test("Markdown with a relative link on an index page", () => {
const markdown = `This is a [link](some-page).`;
const expected = `<p>This is a <a href="/pages/notes/some-page">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: "/pages/notes",
baseUrl: new URL("https://example.com/"),
isDirIndex: true,
});
assertEquals(html, expected);
});
Deno.test("Markdown with an absolute link on an index page", () => {
const markdown = `This is a [link](/top-level-page).`;
const expected = `<p>This is a <a href="/top-level-page">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: "/pages/notes",
baseUrl: new URL("https://example.com/"),
isDirIndex: true,
});
assertEquals(html, expected);
});
Deno.test("Markdown with a relative link on a non-index page", () => {
const markdown = `This is a [link](some-page).`;
const expected = `<p>This is a <a href="/pages/notes/some-page">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: "/pages/notes/another-page",
baseUrl: new URL("https://example.com/"),
isDirIndex: false,
});
assertEquals(html, expected);
});
Deno.test("Markdown with a relative link to a parent directory", () => {
const markdown = `This is a [link](../some-page.md).`;
const expected = `<p>This is a <a href="/pages/some-page">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: "/pages/notes/another-page",
baseUrl: new URL("https://example.com/"),
});
assertEquals(html, expected);
});
Deno.test(
"Markdown with a relative link to a parent directory on an index page",
() => {
const markdown = `This is a [link](../some-page.md).`;
const expected = `<p>This is a <a href="/pages/some-page">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: "/pages/notes",
baseUrl: new URL("https://example.com/"),
isDirIndex: true,
});
assertEquals(html, expected);
}
);
Deno.test(
"Links to non-markdown files should not have the extension removed",
() => {
const markdown = `This is a [link](some-file.pdf).`;
const expected = `<p>This is a <a href="/some-file.pdf">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: "/some-page",
baseUrl: new URL("https://example.com/"),
});
assertEquals(html, expected);
}
);
Deno.test(
"Relative links to markdown files with date prefixes should have the prefix removed",
() => {
const markdown = `This is a [link](./2020-01-01-some-file.md).`;
const expected = `<p>This is a <a href="/some-file">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: "/some-page",
baseUrl: new URL("https://example.com/"),
});
assertEquals(html, expected);
}
);
Deno.test(
"Absolute links to markdown files with date prefixes should have the prefix removed",
() => {
const markdown = `This is a [link](/2020-01-01-some-file.md).`;
const expected = `<p>This is a <a href="/some-file">link</a>.</p>\n`;
const { html } = parseMarkdown({
text: markdown,
currentPath: "/some-page",
baseUrl: new URL("https://example.com/"),
});
assertEquals(html, expected);
}
);