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
8 changes: 4 additions & 4 deletions functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProjectInfo, type Context, type PgFunction } from './utils.ts';
import { encodeHTML, getProjectInfo, type Context, type PgFunction } from './utils.ts';

export const onRequest: PgFunction = async function (context) {
const { request, env } = context;
Expand Down Expand Up @@ -48,15 +48,15 @@ export const onRequest: PgFunction = async function (context) {
.replace(
/title" content="LiveCodes"/g,
`title" content="${
!title || title === 'Untitled Project' ? 'LiveCodes' : title + ' - LiveCodes'
!title || title === 'Untitled Project' ? 'LiveCodes' : encodeHTML(title) + ' - LiveCodes'
}"`,
)
.replace(
/content="A Code Playground That Just Works!"/g,
`content="${
!title && !description
? 'A Code Playground That Just Works!'
: description || 'A project on LiveCodes.'
: encodeHTML(description || 'A project on LiveCodes.')
}"`,
)
.replace(/content="https:\/\/livecodes.io\/"/g, `content="${request.url}"`)
Expand All @@ -77,7 +77,7 @@ export const onRequest: PgFunction = async function (context) {

context.waitUntil(logToAPI(context));
return response;
} catch (err) {
} catch (err: any) {
context.data = {
...data,
ok: false,
Expand Down
8 changes: 8 additions & 0 deletions functions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,11 @@ export const getProjectInfo = async (url: URL): Promise<ProjectInfo> => {
description: '',
};
};

export const encodeHTML = (html: string) =>
html
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/'/g, '&#39;')
.replace(/"/g, '&#34;');