Properly encode om:// links

This commit is contained in:
Alexander Borsuk 2021-06-12 23:57:13 +02:00
parent 4e78d3b6dd
commit 1fd4e34d7f
2 changed files with 13 additions and 6 deletions

View file

@ -10,7 +10,12 @@ function replaceInTemplate(template: string, data: Record<string, any>) {
}
// Throws on decode error.
export async function onGe0Decode(template: string, encodedLatLonZoom: string, name?: string): Promise<Response> {
export async function onGe0Decode(template: string, url: string): Promise<Response> {
const { pathname, search, hash } = new URL(url);
// Filter empty pathname elements.
const params = pathname.split('/').filter(Boolean);
const encodedLatLonZoom = params[0];
let name = params.length > 1 ? params[1] : undefined;
const llz = decodeLatLonZoom(encodedLatLonZoom);
let title = 'Organic Maps';
if (name) {
@ -21,7 +26,12 @@ export async function onGe0Decode(template: string, encodedLatLonZoom: string, n
name = 'Shared via <a href="https://organicmaps.app">Organic Maps app</a>';
}
template = replaceInTemplate(template, { ...llz, title, name });
template = replaceInTemplate(template, {
...llz,
title,
name,
path: pathname + search + hash, // Starts with a slash
});
return new Response(template, { headers: { 'content-type': 'text/html' } });
}

View file

@ -77,9 +77,6 @@ async function handleFetchEvent(event: FetchEvent) {
return await getAssetFromKV(event, getAssetOptions);
} catch (_) { }
// No static resource were found, try to handle a specific dynamic request.
// Filter empty pathname elements.
const params = pathname.split('/').filter(Boolean);
getAssetOptions.mapRequestToAsset = (request: Request) => {
const url = new URL(request.url);
url.pathname = GE0_TEMPLATE_PATH;
@ -87,5 +84,5 @@ async function handleFetchEvent(event: FetchEvent) {
};
const resp = await getAssetFromKV(event, getAssetOptions);
const ge0HtmlTemplate = await resp.text();
return onGe0Decode(ge0HtmlTemplate, params[0], params.length >= 2 ? params[1] : undefined);
return onGe0Decode(ge0HtmlTemplate, event.request.url);
}