@riebeckite/plugin-map 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +176 -0
- package/README_ja.md +142 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +164 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +628 -0
- package/dist/src/hast.d.ts +7 -0
- package/dist/src/init.d.ts +11 -0
- package/dist/src/options.d.ts +30 -0
- package/dist/src/parse.d.ts +35 -0
- package/dist/src/rehype.d.ts +11 -0
- package/dist/src/types.d.ts +159 -0
- package/package.json +62 -0
- package/style.css +107 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// src/init.ts
|
|
2
|
+
var DEFAULT_LEAFLET_SCRIPT_URL = "https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.js";
|
|
3
|
+
var DEFAULT_LEAFLET_STYLE_URL = "https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css";
|
|
4
|
+
var DEFAULT_TILE_URL = "https://tile.openstreetmap.org/{z}/{x}/{y}.png";
|
|
5
|
+
var DEFAULT_ATTRIBUTION = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
|
|
6
|
+
var leafletPromise = null;
|
|
7
|
+
async function initMap(options = {}) {
|
|
8
|
+
const figures = Array.from(
|
|
9
|
+
document.querySelectorAll('[data-rr-map="pending"]')
|
|
10
|
+
);
|
|
11
|
+
if (figures.length === 0) return;
|
|
12
|
+
const runtime = options.runtime ?? await loadLeaflet(options);
|
|
13
|
+
if (!runtime) {
|
|
14
|
+
for (const figure of figures) setError(figure);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
for (const figure of figures) {
|
|
18
|
+
renderMap(runtime, figure, options);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function renderMap(runtime, figure, options) {
|
|
22
|
+
const canvas = figure.querySelector("[data-rr-map-canvas]");
|
|
23
|
+
const payload = readPayload(figure);
|
|
24
|
+
if (!canvas || !payload) {
|
|
25
|
+
setError(figure);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
canvas.style.display = "block";
|
|
30
|
+
const map = runtime.map(canvas, {
|
|
31
|
+
scrollWheelZoom: false,
|
|
32
|
+
minZoom: options.minZoom,
|
|
33
|
+
maxZoom: options.maxZoom
|
|
34
|
+
});
|
|
35
|
+
runtime.tileLayer(payload.tiles || options.tileUrl || DEFAULT_TILE_URL, {
|
|
36
|
+
attribution: payload.attribution || options.attribution || DEFAULT_ATTRIBUTION,
|
|
37
|
+
minZoom: options.minZoom,
|
|
38
|
+
maxZoom: options.maxZoom
|
|
39
|
+
}).addTo(map);
|
|
40
|
+
for (const marker of payload.markers) {
|
|
41
|
+
const layer = runtime.marker([marker.lat, marker.lng]);
|
|
42
|
+
const popup = formatPopup(marker);
|
|
43
|
+
if (popup && layer.bindPopup) layer.bindPopup(popup);
|
|
44
|
+
layer.addTo(map);
|
|
45
|
+
}
|
|
46
|
+
if (payload.markers.length > 1) {
|
|
47
|
+
map.fitBounds(
|
|
48
|
+
runtime.latLngBounds(
|
|
49
|
+
payload.markers.map((marker) => [
|
|
50
|
+
marker.lat,
|
|
51
|
+
marker.lng
|
|
52
|
+
])
|
|
53
|
+
),
|
|
54
|
+
{ padding: [24, 24] }
|
|
55
|
+
);
|
|
56
|
+
} else {
|
|
57
|
+
map.setView([payload.center.lat, payload.center.lng], payload.zoom);
|
|
58
|
+
}
|
|
59
|
+
figure.dataset.rrMap = "rendered";
|
|
60
|
+
} catch {
|
|
61
|
+
setError(figure);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function readPayload(figure) {
|
|
65
|
+
const raw = figure.getAttribute("data-rr-map-payload");
|
|
66
|
+
if (!raw) return null;
|
|
67
|
+
try {
|
|
68
|
+
const parsed = JSON.parse(raw);
|
|
69
|
+
if (!parsed || !Array.isArray(parsed.markers) || !parsed.center) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
return parsed;
|
|
73
|
+
} catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function formatPopup(marker) {
|
|
78
|
+
const label = marker.label?.trim() || `${marker.lat}, ${marker.lng}`;
|
|
79
|
+
const parts = [`<strong>${escapeHtml(label)}</strong>`];
|
|
80
|
+
if (marker.description) {
|
|
81
|
+
parts.push(`<span>${escapeHtml(marker.description)}</span>`);
|
|
82
|
+
}
|
|
83
|
+
return parts.join("<br>");
|
|
84
|
+
}
|
|
85
|
+
function setError(figure) {
|
|
86
|
+
figure.dataset.rrMap = "error";
|
|
87
|
+
const canvas = figure.querySelector("[data-rr-map-canvas]");
|
|
88
|
+
if (canvas) canvas.style.display = "none";
|
|
89
|
+
const fallback = figure.querySelector("details");
|
|
90
|
+
if (fallback) fallback.open = true;
|
|
91
|
+
}
|
|
92
|
+
async function loadLeaflet(options) {
|
|
93
|
+
const existing = readLeafletGlobal();
|
|
94
|
+
if (existing) return existing;
|
|
95
|
+
if (!leafletPromise) {
|
|
96
|
+
const scriptUrl = options.leafletScriptUrl ?? DEFAULT_LEAFLET_SCRIPT_URL;
|
|
97
|
+
const styleUrl = options.leafletStyleUrl ?? DEFAULT_LEAFLET_STYLE_URL;
|
|
98
|
+
leafletPromise = Promise.all([loadScript(scriptUrl), loadStyle(styleUrl)]).then(() => readLeafletGlobal()).catch(() => null);
|
|
99
|
+
}
|
|
100
|
+
return leafletPromise;
|
|
101
|
+
}
|
|
102
|
+
function readLeafletGlobal() {
|
|
103
|
+
const value = globalThis.L;
|
|
104
|
+
return value ?? null;
|
|
105
|
+
}
|
|
106
|
+
function loadScript(src) {
|
|
107
|
+
const existing = document.querySelector(
|
|
108
|
+
'script[data-rr-map-script="true"]'
|
|
109
|
+
);
|
|
110
|
+
if (existing) {
|
|
111
|
+
return existing.dataset.rrMapScriptLoaded === "true" ? Promise.resolve() : waitForLoad(existing);
|
|
112
|
+
}
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
const script = document.createElement("script");
|
|
115
|
+
script.src = src;
|
|
116
|
+
script.async = true;
|
|
117
|
+
script.dataset.rrMapScript = "true";
|
|
118
|
+
script.addEventListener(
|
|
119
|
+
"load",
|
|
120
|
+
() => {
|
|
121
|
+
script.dataset.rrMapScriptLoaded = "true";
|
|
122
|
+
resolve();
|
|
123
|
+
},
|
|
124
|
+
{ once: true }
|
|
125
|
+
);
|
|
126
|
+
script.addEventListener(
|
|
127
|
+
"error",
|
|
128
|
+
() => reject(new Error("Failed to load Leaflet")),
|
|
129
|
+
{ once: true }
|
|
130
|
+
);
|
|
131
|
+
document.head.appendChild(script);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
function loadStyle(href) {
|
|
135
|
+
const existing = document.querySelector(
|
|
136
|
+
'link[data-rr-map-style="true"]'
|
|
137
|
+
);
|
|
138
|
+
if (existing) return Promise.resolve();
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
const link = document.createElement("link");
|
|
141
|
+
link.rel = "stylesheet";
|
|
142
|
+
link.href = href;
|
|
143
|
+
link.dataset.rrMapStyle = "true";
|
|
144
|
+
link.addEventListener("load", () => resolve(), { once: true });
|
|
145
|
+
link.addEventListener(
|
|
146
|
+
"error",
|
|
147
|
+
() => reject(new Error("Failed to load Leaflet styles")),
|
|
148
|
+
{ once: true }
|
|
149
|
+
);
|
|
150
|
+
document.head.appendChild(link);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
function waitForLoad(element) {
|
|
154
|
+
return new Promise((resolve) => {
|
|
155
|
+
element.addEventListener("load", () => resolve(), { once: true });
|
|
156
|
+
element.addEventListener("error", () => resolve(), { once: true });
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function escapeHtml(value) {
|
|
160
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
161
|
+
}
|
|
162
|
+
export {
|
|
163
|
+
initMap
|
|
164
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { MapOptions } from "./src/types.js";
|
|
2
|
+
export { resolveMapOptions } from "./src/options.js";
|
|
3
|
+
export { describeMap, formatCoordinates, normalizeMapInput, openStreetMapUrl, parseMapSource, } from "./src/parse.js";
|
|
4
|
+
export { rehypeMap } from "./src/rehype.js";
|
|
5
|
+
export type { MapClientOptions, MapCoordinates, MapData, MapMarker, MapOptions, MapPayload, MapRuntime, ResolvedMapOptions, } from "./src/types.js";
|
|
6
|
+
export declare const MAP_PLUGIN_NAME = "map";
|
|
7
|
+
/**
|
|
8
|
+
* Turns a fenced `map` code block and/or frontmatter coordinates into an
|
|
9
|
+
* embedded OpenStreetMap.
|
|
10
|
+
*
|
|
11
|
+
* The build emits a static fallback (coordinates, place, OpenStreetMap links)
|
|
12
|
+
* and a JSON payload; `initMap` lazily loads Leaflet from the CDN only when a
|
|
13
|
+
* map is present and upgrades each figure to an interactive map.
|
|
14
|
+
*/
|
|
15
|
+
export declare function map(options?: MapOptions): import("@riebeckite/core").RiebeckitePlugin<MapOptions>;
|
|
16
|
+
/** Alias matching the `*Plugin` suffix used by other plugin factories. */
|
|
17
|
+
export declare const mapPlugin: typeof map;
|