-
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
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import { extname } from "../../deps/deno.land/std/path/mod.ts";
import type { Extension } from "../../deps/esm.sh/mdast-util-from-markdown/mod.ts";
import type { State } from "../../deps/esm.sh/mdast-util-to-hast/mod.ts";
import type * as Mdast from "../../deps/esm.sh/mdast/types.ts";
import type * as Hast from "../../deps/esm.sh/hast/types.ts";
export interface OfmWikilink extends Mdast.Node {
type: "ofmWikilink";
target: string;
label: string | null;
}
export interface OfmWikilinkEmbed extends Mdast.Node {
type: "ofmWikilinkEmbed";
target: string;
label: string | null;
}
export function ofmWikilinkFromMarkdown(): Extension {
return {
enter: {
ofmWikilink(token) {
this.enter({
// @ts-expect-error: unist-related libraries heavily relies on ambiend module declarations,
// which Deno does not support. APIs also don't accept type parameters.
type: "ofmWikilink",
target: "",
label: null,
}, token);
},
ofmWikilinkEmbed(token) {
this.enter({
// @ts-expect-error: unist-related libraries heavily relies on ambiend module declarations,
// which Deno does not support. APIs also don't accept type parameters.
type: "ofmWikilinkEmbed",
target: "",
label: null,
data: {},
}, token);
},
ofmWikilinkTarget(token) {
const context = this.stack[this.stack.length - 1] as
| Mdast.Nodes
| OfmWikilink
| OfmWikilinkEmbed
| undefined;
switch (context?.type) {
case "ofmWikilink":
case "ofmWikilinkEmbed": {
context.target = this.sliceSerialize(token);
return;
}
default: {
throw new Error(`Unexpected wikilink target in ${context?.type}`);
}
}
},
ofmWikilinkLabel(token) {
const context = this.stack[this.stack.length - 1] as
| Mdast.Nodes
| OfmWikilink
| OfmWikilinkEmbed
| undefined;
switch (context?.type) {
case "ofmWikilink":
case "ofmWikilinkEmbed": {
context.label = this.sliceSerialize(token);
return;
}
default: {
throw new Error(`Unexpected wikilink target in ${context?.type}`);
}
}
},
},
exit: {
ofmWikilink(token) {
this.exit(token);
},
ofmWikilinkEmbed(token) {
this.exit(token);
},
},
};
}
export const ofmWikilinkToHastHandlers = {
ofmWikilink(_state: State, node: OfmWikilink): Hast.Nodes {
return {
type: "element",
tagName: "a",
properties: {
href: node.target,
},
children: [{ type: "text", value: node.label ?? node.target }],
};
},
ofmWikilinkEmbed(_state: State, node: OfmWikilinkEmbed): Hast.Nodes {
switch (extname(node.target).toLowerCase()) {
case ".jpg":
case ".jpeg":
case ".avif":
case ".bmp":
case ".png":
case ".svg":
case ".webp": {
return {
type: "element",
tagName: "img",
properties: {
src: node.target,
alt: node.label ?? undefined,
},
children: [],
};
}
case ".flac":
case ".m4a":
case ".mp3":
case ".ogg":
case ".wav":
case ".3gp": {
return {
type: "element",
tagName: "audio",
properties: {
src: node.target,
title: node.label ?? undefined,
},
children: [],
};
}
case ".mkv":
case ".mov":
case ".mp4":
case ".ogv":
case ".webm": {
return {
type: "element",
tagName: "video",
properties: {
src: node.target,
title: node.label ?? undefined,
},
children: [],
};
}
default: {
return {
type: "element",
tagName: "iframe",
properties: {
src: node.target,
title: node.label ?? undefined,
},
children: [],
};
}
}
},
};