From 2a55890764a9227fd89bcdc7d6b8e9bb435c7665 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 20 Nov 2024 16:49:02 +0100 Subject: [PATCH] feat: Use nunjucks for rendering --- Dockerfile | 3 -- bun.lockb | Bin 4426 -> 6252 bytes package.json | 2 + src/index.ts | 5 +++ src/utils/renderer.ts | 45 +++++++---------------- templates/{index.html => index.html.njk} | 1 + templates/{index.txt => index.txt.njk} | 0 upload_to_server.sh | 3 +- 8 files changed, 22 insertions(+), 37 deletions(-) rename templates/{index.html => index.html.njk} (98%) rename templates/{index.txt => index.txt.njk} (100%) diff --git a/Dockerfile b/Dockerfile index d0a9adb..a81df0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,6 @@ WORKDIR /app COPY . /app -# Absolutely no idea why, but piping the output directly to the file makes it empty. -RUN mkdir -p /tmp/templates && mv /app/templates/*.html /tmp/templates/ -RUN bunx html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true /tmp/templates/index.html > /app/templates/index.html RUN bun install --production EXPOSE 3000 diff --git a/bun.lockb b/bun.lockb index 5b268d4cfdeeb9165ef7f0bf1b9ffb610d23c95e..75248b3870b2fd1b3b65328d34337939df903f11 100755 GIT binary patch delta 1960 zcmc&!drXs86#u@jEp4GKRA9D(@|fsQzrH{OHYpm2Ak$f2^)<-{6e!Y%JciUqYsMCa zf|R?d7+rL6TjnDfI1Hlk5fa@nlgE(F%)~GsY@->@xtH;=b4ovE{B!^8B=`H>bIv{I z-0$3be?9R&U9%=}eg6aBbhDPbVv;#>)jpE7*BLudJ=Z>HH{~b3{7K}^$&_Ehyylqn zWntqAMR7K(!)dWoE=Q5yWUscDRTplm>;g?nQ68?6bE4xI3<&agn^o>`n1{kV7!cR& zVkjyEun$lM2xba=o)v;||GogemtQ&Beq#PoHs$u{qAd5N_CAkS)b~O6kRr`KDKam2 zYIka_BhJyDyWdlCf2`o@v@PtqqZ#a1HMWEci6PjL#LC2xitW#KmMVdl}LNaFZdA0Hz8^x*&tu zARsP)!vfMJNOw!1C=L`vAq?o7U;Ycit|>VTM1?=gIHC^cAwZ7_gc>wUv1i46U&8~ zPmrdOS*Mc^JWMRAH#Ne)(A3k=r+|3oya;YKS?l1)^^$FW z%DJS)9}Z_dw>uHPH9%o@-L>eN)eYX2d#7jXrg!ES&%EK5{np`5fB)EeLqlq5D31;yXM)VMj3~usT28F+Rgg>Y4JB$x4Ll`b zm4z>LWssfHbL30SRWY7pBhVGO4M<52GZ^hCob7K6;FOV2T1}nLIz~ZR6Kj~ z%*N0glQafRGKrN(mmPy-*bkoGz^T{hG&&&R0Lk(M2p$7?k|>I&VV)wvGgkvq6jfO2 z*kmo@DoQRkycuQGc12*RB=qcJ$VIs-g=b@_C5Z{uB~J5h_^-y~tvqt%{|@pEOVlwt@}x zq6HywK*WR8gP>G`w0IFccoAC^6nf}QOF@e1MGvAE>&#|zD%64Do7ws1+nM*?MtsRA zn+Nw_?JMWi_t}TRxq+ToZhG^4=SV_Yw!V~qO@FE`$6ke)V@k!|6CG0O31b(o2MnUD-0995AY;laS!|m z*s$)`;H+eEFZ_{gco5KFK(@Fap2;?xku=a0i%W1$vEesnZfY9C1#+R@82h_fHyVxZYTd1eRM_2Yz)C0*rHyGT+L(q;Xehux)LiI+-uH(FPw!!INH{DB$ck*-Hw;ZlY$>YGT9&pRgbha zUdzpmk&m~-HAC=(nJ|wWWvpF5T92f*D%|Rhn~84o#MVkf+X&YGVFcm6o|gOFbUp-M m^}Rc(mcBkw93JbsI9_n0d=VbE#vs|!0^_ak7AAN6, ) { - const presentation = request.get("presentation"); - let content = await getTemplate(templateName, presentation); - - for (const [key, value] of Object.entries(ctx)) { - content = content.replaceAll(`{{${key}}}`, value); - } - - switch (presentation) { - case "browser": { - return request.html(content); - } - case "terminal": { - return request.text(content); - } - } -} - -const _templateCache: Record = {}; - -async function getTemplate( - templateName: "index", - presentation: PresentationType, -): Promise { + const presentation = context.get("presentation") as PresentationType; const extension = { browser: ".html", terminal: ".txt", }[presentation]; - const key = templateName + extension; + const key = templateName + extension + ".njk"; - if (_templateCache[key]) { - return _templateCache[key]; + const content = nunjucks.render(key, ctx); + + switch (presentation) { + case "browser": + return context.html(content); + break; + case "terminal": + return context.text(content); + break; } - - const currentPath = await Bun.resolve(`./templates/${key}`, process.cwd()); - _templateCache[key] = await Bun.file(currentPath).text(); - - return _templateCache[key]; } diff --git a/templates/index.html b/templates/index.html.njk similarity index 98% rename from templates/index.html rename to templates/index.html.njk index a7bbe4a..847db48 100644 --- a/templates/index.html +++ b/templates/index.html.njk @@ -4,6 +4,7 @@ + amiopen.now