Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,40 @@ describe('/@*', () => {
assert.equal(getBody(resB)?.toString().includes('<fake tag />'), true);
});

it('should replace a meta tag that spans multiple lines', async () => {
const ctx = new LambdaAlbRequest(baseRequest, {} as Context, LogConfig.get());
process.env[Env.StaticAssetLocation] = 'memory://assets/';

const indexHtml = [
'<html>',
' <head>',
' <meta name="twitter:title" property="og:title" content="LINZ Basemaps" />',
' <meta',
' name="twitter:description"',
' property="og:description"',
' content="NZ’s authoritative and open digital basemap service for LINZ and the public."',
' />',
' </head>',
'</html>',
].join('\n');

await fsa.write(new URL('memory://assets/index.html'), indexHtml);

const res = await loadAndServeIndexHtml(
ctx,
null,
new Map([['og:description', '<meta name="twitter:description" property="og:description" content="replaced" />']]),
);
const body = getBody(res)?.toString();

assert.equal(
body?.includes('<meta name="twitter:description" property="og:description" content="replaced" />'),
true,
);

assert.equal(body?.includes('<meta name="twitter:title" property="og:title" content="LINZ Basemaps" />'), true);
});

it('should include config url', async () => {
process.env[Env.StaticAssetLocation] = 'memory://assets/';

Expand Down
18 changes: 7 additions & 11 deletions packages/lambda-tiler/src/routes/preview.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,13 @@ export async function loadAndServeIndexHtml(
return res;
}

// Replace open graph tags
const output = String(indexHtml)
.split('\n')
.map((f) => {
for (const [key, value] of tags.entries()) {
if (f.includes(key)) return value;
}
return f;
});

const outHtml = output.join('\n');
// Replace open graph tags, matching the whole `<meta ... />` element as it
// may be formatted across multiple lines
let outHtml = String(indexHtml);
for (const [key, value] of tags.entries()) {
outHtml = outHtml.replace(new RegExp(`[ \\t]*<meta[^>]*property="${key}"[^>]*/>`), value);
}

res.header(HttpHeader.ETag, Etag.key(outHtml));
res.buffer(outHtml, `text/html; charset=utf-8`);
return res;
Expand Down
Loading