-
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
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
/** @jsx h */
import { extname } from "../../../deps/deno.land/std/path/mod.ts";
import type * as Hast from "../../../deps/esm.sh/hast/types.ts";
import type * as Mdast from "../../../deps/esm.sh/mdast/types.ts";
import { h } from "../../../deps/esm.sh/hastscript/mod.ts";
import { type State } from "../../../deps/esm.sh/mdast-util-to-hast/mod.ts";
import { type OfmWikilinkEmbed } from "../../../content_parser/obsidian_markdown/mdast_util_ofm_wikilink.ts";
import type { Document } from "../../../types.ts";
import type { BuildContext } from "../context.ts";
import { hasAssetToken, hasDocumentToken } from "./utils.ts";
function sizeProperties(node: Mdast.Node): { width?: number; height?: number } {
return {
width:
node.data && "width" in node.data && typeof node.data.width === "number"
? node.data.width
: undefined,
height:
node.data && "height" in node.data && typeof node.data.height === "number"
? node.data.height
: undefined,
};
}
function getUrl(url: string, node: Mdast.Node, context: BuildContext): string {
if (!hasAssetToken(node)) {
return url;
}
const file = context.documentTree.exchangeToken(node.data.macanaAssetToken);
context.copyFile(file);
return context.resolvePath(file.path).join("/");
}
export interface EmbedHandlersParameters {
context: BuildContext;
buildDocumentContent(
document: Document,
fragments?: readonly string[],
): Hast.Nodes;
}
export function embedHandlers(
{ context, buildDocumentContent }: EmbedHandlersParameters,
) {
return {
image(_state: State, node: Mdast.Image) {
return h("img", {
...sizeProperties(node),
src: getUrl(node.url, node, context),
alt: node.alt,
}, []);
},
imageReference(state: State, node: Mdast.ImageReference) {
const def = state.definitionById.get(node.identifier.toUpperCase());
if (!def) {
throw new Error(`Orphaned image reference: id=${node.identifier}`);
}
return h("img", {
...sizeProperties(node),
src: getUrl(def.url, node, context),
alt: node.alt,
}, []);
},
ofmWikilinkEmbed(_state: State, node: OfmWikilinkEmbed) {
// Document embed
if (hasDocumentToken(node)) {
const { document, fragments } = context.documentTree.exchangeToken(
node.data.macanaDocumentToken,
context.document,
);
const hast = buildDocumentContent(document, fragments);
return h("div", {}, [hast]);
}
const path = getUrl(node.target, node, context);
switch (extname(node.target).toLowerCase()) {
case ".jpg":
case ".jpeg":
case ".avif":
case ".bmp":
case ".png":
case ".svg":
case ".webp": {
return h("img", {
...sizeProperties(node),
src: path,
alt: node.label ?? undefined,
}, []);
}
case ".flac":
case ".m4a":
case ".mp3":
case ".ogg":
case ".wav":
case ".3gp": {
return h("audio", {
src: path,
title: node.label ?? undefined,
}, []);
}
case ".mkv":
case ".mov":
case ".mp4":
case ".ogv":
case ".webm": {
return h("video", {
...sizeProperties(node),
src: path,
title: node.label ?? undefined,
}, []);
}
default: {
return h("iframe", {
...sizeProperties(node),
src: path,
title: node.label ?? undefined,
}, []);
}
}
},
};
}