Skip to content

Commit

Permalink
qr
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Jan 21, 2025
1 parent e52be5d commit 8672181
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dangerFileContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ async function addDeployPreviewUrls() {

async function formatMdLinkWithQrCode(path: string) {
const url = String(formatFileToLink(path));
const qrDataUrl = await QRCode.toDataURL(url, {});
return `<details><summary>[${path}](${url})</summary>![${path}](${qrDataUrl})</details>`;
const qr = `${netlifyPreview}edge-functions/qr-code?text=${encodeURIComponent(url)}`;
return `<details><summary><a href="${url}">${path}</a></summary><img src="${qr}" alt="QR code" /></details>`;
}

const files = [...danger.git.created_files, ...danger.git.modified_files];
Expand Down
6 changes: 6 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# run `pnpm --package netlify-cli dlx netlify dev --cwd .` to run functions locally
[dev]
command = "pnpm docs:dev"
targetPort = 3000
port = 8888

[build]
# Directory (relative to root of your repo) that contains the deploy-ready
# HTML files and assets generated by the build. If a base directory has
Expand Down
33 changes: 33 additions & 0 deletions netlify/edge-functions/qr-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import qr from 'https://esm.sh/[email protected]';

export default async function handler(req: Request) {
const params = new URL(req.url).searchParams;
const text = params.get('text');

if (!text) {
return new Response('Missing text query parameter', { status: 400 });
}

if (text.length > 1000) {
return new Response('Text query parameter is too long', { status: 400 });
}

const data = qr.imageSync(text, { type: 'png' });

const v = params.get('v') || '';

const cacheKey = new URLSearchParams();
cacheKey.append('text', text);
cacheKey.append('v', v);

return new Response(data, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=31536000, immutable', // Cache for 1 year
},
});
}
export const config = {
cache: 'manual',
path: '/edge-functions/qr-code',
};

0 comments on commit 8672181

Please sign in to comment.