Skip to content

Commit 1a4fe1b

Browse files
authored
Merge pull request #936 from live-codes/fix-meta-tags
fix meta tags
2 parents 7e230d2 + b347082 commit 1a4fe1b

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

functions/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getProjectInfo, type Context, type PgFunction } from './utils.ts';
1+
import { encodeHTML, getProjectInfo, type Context, type PgFunction } from './utils.ts';
22

33
export const onRequest: PgFunction = async function (context) {
44
const { request, env } = context;
@@ -48,15 +48,15 @@ export const onRequest: PgFunction = async function (context) {
4848
.replace(
4949
/title" content="LiveCodes"/g,
5050
`title" content="${
51-
!title || title === 'Untitled Project' ? 'LiveCodes' : title + ' - LiveCodes'
51+
!title || title === 'Untitled Project' ? 'LiveCodes' : encodeHTML(title) + ' - LiveCodes'
5252
}"`,
5353
)
5454
.replace(
5555
/content="A Code Playground That Just Works!"/g,
5656
`content="${
5757
!title && !description
5858
? 'A Code Playground That Just Works!'
59-
: description || 'A project on LiveCodes.'
59+
: encodeHTML(description || 'A project on LiveCodes.')
6060
}"`,
6161
)
6262
.replace(/content="https:\/\/livecodes.io\/"/g, `content="${request.url}"`)
@@ -77,7 +77,7 @@ export const onRequest: PgFunction = async function (context) {
7777

7878
context.waitUntil(logToAPI(context));
7979
return response;
80-
} catch (err) {
80+
} catch (err: any) {
8181
context.data = {
8282
...data,
8383
ok: false,

functions/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,11 @@ export const getProjectInfo = async (url: URL): Promise<ProjectInfo> => {
8080
description: '',
8181
};
8282
};
83+
84+
export const encodeHTML = (html: string) =>
85+
html
86+
.replace(/&/g, '&amp;')
87+
.replace(/</g, '&lt;')
88+
.replace(/>/g, '&gt;')
89+
.replace(/'/g, '&#39;')
90+
.replace(/"/g, '&#34;');

0 commit comments

Comments
 (0)