diff options
| author | Tomas Bankauskas <tbdizainas@gmail.com> | 2024-04-09 12:40:31 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-09 12:40:31 +0300 |
| commit | 3bebbf00e6f4d33bfacbf38eef73a9ad7f0d9d58 (patch) | |
| tree | 2fc74dae2e0d554325ec634891f5726d063e7529 | |
| parent | 4df0e4a754ea5e1fb0d05a4e08a53cd49e9fce16 (diff) | |
Added Image CDN page (#4)
* added image-cdn
* updated image-cdn page, colors, etc
* added Markdown.astro and updated styles
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | .prettierrc | 14 | ||||
| -rw-r--r-- | astro.config.mjs | 11 | ||||
| -rw-r--r-- | package-lock.json | 22 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | src/components/Alert.astro | 16 | ||||
| -rw-r--r-- | src/components/Markdown.astro | 35 | ||||
| -rw-r--r-- | src/layouts/Layout.astro | 40 | ||||
| -rw-r--r-- | src/pages/_assets/corgi.jpg | bin | 0 -> 587671 bytes | |||
| -rw-r--r-- | src/pages/image-cdn.astro | 105 | ||||
| -rw-r--r-- | src/styles/globals.css | 43 | ||||
| -rw-r--r-- | src/utils.js | 4 | ||||
| -rw-r--r-- | tailwind.config.mjs | 6 |
13 files changed, 254 insertions, 47 deletions
@@ -19,3 +19,6 @@ pnpm-debug.log* # macOS-specific files .DS_Store + +# Local Netlify folder +.netlify diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..ae04475 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,14 @@ +{ + "printWidth": 160, + "singleQuote": true, + "trailingComma": "none", + "tabWidth": 4, + "overrides": [ + { + "files": ["*.md", "*.mdx", "*.yaml"], + "options": { + "tabWidth": 2 + } + } + ] +}
\ No newline at end of file diff --git a/astro.config.mjs b/astro.config.mjs index 461bd30..0798920 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,8 +1,11 @@ import { defineConfig } from 'astro/config'; - -import tailwind from "@astrojs/tailwind"; +import tailwind from '@astrojs/tailwind'; // https://astro.build/config export default defineConfig({ - integrations: [tailwind()] -});
\ No newline at end of file + integrations: [ + tailwind({ + applyBaseStyles: false + }) + ] +}); diff --git a/package-lock.json b/package-lock.json index 2b65a28..00d3e2d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,8 @@ "dependencies": { "@astrojs/tailwind": "^5.1.0", "astro": "^4.5.14", + "marked": "^12.0.1", + "marked-shiki": "^1.0.1", "tailwindcss": "^3.4.3" }, "devDependencies": { @@ -3190,6 +3192,26 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/marked": { + "version": "12.0.1", + "resolved": "https://registry.npmjs.org/marked/-/marked-12.0.1.tgz", + "integrity": "sha512-Y1/V2yafOcOdWQCX0XpAKXzDakPOpn6U0YLxTJs3cww6VxOzZV1BTOOYWLvH3gX38cq+iLwljHHTnMtlDfg01Q==", + "bin": { + "marked": "bin/marked.js" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/marked-shiki": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/marked-shiki/-/marked-shiki-1.0.1.tgz", + "integrity": "sha512-fVz5Gq4ZBEptQH4kZTpNfILLxnBONxrXdpjko7rfXm68psltGaZL5VcR1xNNbF/ZN7W4u7ba5fxM+bzzEZoPlQ==", + "peerDependencies": { + "marked": ">=7.0.0", + "shiki": ">=1.0.0" + } + }, "node_modules/mdast-util-definitions": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/mdast-util-definitions/-/mdast-util-definitions-6.0.0.tgz", diff --git a/package.json b/package.json index 6db7e80..a089f6e 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "dependencies": { "@astrojs/tailwind": "^5.1.0", "astro": "^4.5.14", + "marked": "^12.0.1", + "marked-shiki": "^1.0.1", "tailwindcss": "^3.4.3" }, "devDependencies": { diff --git a/src/components/Alert.astro b/src/components/Alert.astro new file mode 100644 index 0000000..1a468fb --- /dev/null +++ b/src/components/Alert.astro @@ -0,0 +1,16 @@ +--- +interface Props { + class?: string; +} + +const { class: className } = Astro.props; +--- + +<div class:list={['alert alert-info', className].join(' ')}> + <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 fill-current shrink-0" viewBox="0 0 24 24"> + <path + d="M12 20.016q3.281 0 5.648-2.367t2.367-5.648-2.367-5.648-5.648-2.367-5.648 2.367-2.367 5.648 2.367 5.648 5.648 2.367zM12 2.016q4.125 0 7.055 2.93t2.93 7.055-2.93 7.055-7.055 2.93-7.055-2.93-2.93-7.055 2.93-7.055 7.055-2.93zM11.016 6.984h1.969v6h-1.969v-6zM11.016 15h1.969v2.016h-1.969v-2.016z" + ></path> + </svg> + <slot /> +</div> diff --git a/src/components/Markdown.astro b/src/components/Markdown.astro new file mode 100644 index 0000000..d9881a8 --- /dev/null +++ b/src/components/Markdown.astro @@ -0,0 +1,35 @@ +--- +import { Marked } from 'marked'; +import markedShiki from 'marked-shiki'; +import { getHighlighter } from 'shiki'; + +interface Props { + content: string; + class?: string; +} + +const { content, class: className } = Astro.props; + +// `getHighlighter` is async, it initializes the internal and +// loads the languages and themes specified. +// Then later you can use `highlighter.codeToHtml` synchronously +// with the loaded themes and languages. +const highlighter = await getHighlighter({ + langs: ['jsx', 'js'], + themes: ['poimandres'] +}); +const html = await new Marked() + .use( + markedShiki({ + highlight(code, lang, props) { + return highlighter.codeToHtml(code, { + lang, + theme: 'poimandres' + }); + } + }) + ) + .parse(content); +--- + +<div class:list={['markdown', className]} set:html={html} /> diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index 4ecbd8b..1d5bea1 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -1,10 +1,10 @@ --- import '../../src/styles/globals.css'; -import Header from "../components/Header.astro"; -import Footer from "../components/Footer.astro"; +import Header from '../components/Header.astro'; +import Footer from '../components/Footer.astro'; interface Props { - title: string; + title: string; } const { title } = Astro.props; @@ -12,20 +12,20 @@ const { title } = Astro.props; <!doctype html> <html lang="en"> - <head> - <meta charset="UTF-8" /> - <title>{title}</title> - <meta name="viewport" content="width=device-width" /> - <meta name="generator" content={Astro.generator} /> - <link rel="icon" type="image/svg+xml" href="/favicon.svg" /> - </head> - <body class="antialiased text-white bg-blue-900"> - <div class="flex flex-col min-h-screen px-6 bg-grid-pattern sm:px-12"> - <div class="flex flex-col w-full max-w-5xl mx-auto grow"> - <Header /> - <div class="grow"><slot /></div> - <Footer /> - </div> - </div> - </body> -</html>
\ No newline at end of file + <head> + <meta charset="UTF-8" /> + <title>{title}</title> + <meta name="viewport" content="width=device-width" /> + <meta name="generator" content={Astro.generator} /> + <link rel="icon" type="image/svg+xml" href="/favicon.svg" /> + </head> + <body class="antialiased text-white bg-teal-600"> + <div class="flex flex-col min-h-screen px-6 bg-grid-pattern sm:px-12"> + <div class="flex flex-col w-full max-w-5xl mx-auto grow"> + <Header /> + <main class="grow"><slot /></main> + <Footer /> + </div> + </div> + </body> +</html> diff --git a/src/pages/_assets/corgi.jpg b/src/pages/_assets/corgi.jpg Binary files differnew file mode 100644 index 0000000..402232f --- /dev/null +++ b/src/pages/_assets/corgi.jpg diff --git a/src/pages/image-cdn.astro b/src/pages/image-cdn.astro new file mode 100644 index 0000000..aa1b4e6 --- /dev/null +++ b/src/pages/image-cdn.astro @@ -0,0 +1,105 @@ +--- +import { Image } from 'astro:assets'; +import Layout from '../layouts/Layout.astro'; +import Markdown from '../components/Markdown.astro'; +import corgi from './_assets/corgi.jpg'; + +const sampleImageSrc = '/images/corgi.jpg'; + +const astroImageSnippet = ` +When running on Netlify, \`next/image\` is automatically set-up to use Netlify Image CDN for optimized images. + +~~~jsx +import Image from 'next/image'; + +// In your component +<Image src="/images/corgi.jpg" alt="Corgi" /* ... additional props */ /> +~~~ +`; + +const originalVsCdnSnippet = ` +In the code below, a regular \`<img>\` tag is used in both cases for a framework-agnostic example. +Other than using \`next/image\` or rolling your own \`<img>\` tags, you can also use the excellent [unpic-img](https://unpic.pics/). + +~~~jsx +// <== On the left, the original image +<img src="/images/corgi.jpg" alt="Corgi" /> + +// ==> On the right, explicitly using Netlify Image CDN endpoint for a responsive image +<img + srcSet="/.netlify/images?url=images/corgi.jpg&w=640 640w, /.netlify/images?url=images/corgi.jpg&w=1280 1280w, /.netlify/images?url=images/corgi.jpg&w=2048 2048w" + sizes="(max-width: 1024px) 100vw, 1024px" + alt="Corgi" +/> +~~~ +`; +--- + +<Layout title="Image CDN"> + <div class="flex flex-col gap-8 sm:gap-16"> + <section> + <h1 class="mb-0">Image CDN</h1> + </section> + <section> + <h2 class="mb-6 text-2xl font-bold sm:text-3xl">Using built-in Astro <Image /> component</h2> + <Markdown content={astroImageSnippet} class="mb-8" /> + <figure> + <Image src={corgi} alt="Corgi" class="border-2 border-white rounded-lg" /> + <figcaption class="mt-2 text-sm italic"> + Credit: photo by <a href="https://unsplash.com/@alvannee?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Alvan Nee</a> on + <a + href="https://unsplash.com/photos/long-coated-white-and-brown-dog-lvFlpqEvuRM?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash" + > + Unsplash + </a> + </figcaption> + </figure> + </section> + <section> + <h2 class="mb-6 text-2xl font-bold sm:text-3xl">Original vs. optimized image: can you tell the difference?</h2> + <Markdown content={originalVsCdnSnippet} class="mb-8" /> + <div class="diff aspect-[3/2] rounded-lg border-2 border-white"> + <div class="diff-item-1"> + <div> + <div class="relative"> + <span class="hidden absolute py-1.5 px-2.5 text-sm rounded-lg bg-neutral-900/70 top-2.5 right-2.5"></span> + <img src={`/.netlify/images?url=${sampleImageSrc}`} alt="Corgi" /> + </div> + </div> + </div> + <div class="diff-item-2"> + <div> + <div class="relative"> + <span class="hidden absolute py-1.5 px-2.5 text-sm rounded-lg bg-neutral-900/70 top-2.5 left-2.5"></span> + <img src={sampleImageSrc} alt="Corgi" /> + </div> + </div> + </div> + <div class="diff-resizer"></div> + </div> + </section> + </div> +</Layout> + +<script> + const images = document.querySelectorAll<HTMLImageElement>('.diff img'); + + images.forEach((image) => { + if (image.complete) { + showImageSize(image); + } else { + image.addEventListener('load', () => { + showImageSize(image); + }); + } + }); + + function showImageSize(image: HTMLImageElement) { + const imageEntry = window?.performance?.getEntriesByName(image.currentSrc)?.[0] as PerformanceResourceTiming; + if (imageEntry && imageEntry.encodedBodySize) { + const imageOverlay = image.previousElementSibling; + imageOverlay.classList.remove('hidden'); + imageOverlay.textContent = `Size: ${Math.ceil(imageEntry.encodedBodySize / 1024)}KB`; + } + } +</script> diff --git a/src/styles/globals.css b/src/styles/globals.css index 8846f57..b36e856 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -5,33 +5,36 @@ @tailwind utilities; @layer base { - + h1 { + @apply mb-6 text-4xl font-bold tracking-tight sm:text-5xl; + } + + a { + @apply underline; + } + + pre { + @apply overflow-x-auto p-6 rounded-lg; + } + :not(pre) > code { @apply px-1 py-0.5 font-mono rounded bg-neutral-900 text-yellow-200; font-size: 0.9em; } - h1 { - @apply mb-6 text-4xl font-bold tracking-tight sm:text-5xl; + .markdown > * { + @apply my-6; } -} - -a { - @apply underline -} -.markdown { - @apply mb-1; -} + .markdown > :first-child { + @apply mt-0; + } -.alert .markdown { - @apply mb-0; -} + .markdown > :last-child { + @apply mb-0; + } -.markdown p { - @apply mb-3; + .btn:disabled { + @apply text-neutral-400 bg-neutral-700; + } } - -.btn:disabled { - @apply text-neutral-400 bg-neutral-700; -}
\ No newline at end of file diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..d2cc47b --- /dev/null +++ b/src/utils.js @@ -0,0 +1,4 @@ +// Note: this only works on the server side +export function getNetlifyContext() { + return process.env.CONTEXT; +}
\ No newline at end of file diff --git a/tailwind.config.mjs b/tailwind.config.mjs index 507c93e..87a222b 100644 --- a/tailwind.config.mjs +++ b/tailwind.config.mjs @@ -22,10 +22,10 @@ export default { { lofi: { ...require('daisyui/src/theming/themes')['lofi'], - primary: '#2bdcd2', + primary: '#fbb13d', 'primary-content': '#171717', - secondary: '#016968', - info: '#2bdcd2', + secondary: '#0f766e', + info: '#fbb13d', 'info-content': '#171717', } } |
