Skip to content

Instantly share code, notes, and snippets.

@kmelve
Last active October 18, 2019 10:23
Show Gist options
  • Save kmelve/f16bcf12c9189c8496b935d27460cb04 to your computer and use it in GitHub Desktop.
Save kmelve/f16bcf12c9189c8496b935d27460cb04 to your computer and use it in GitHub Desktop.
Adding images to svelte using https://github.com/sanity-io/image-url
<script context="module">
import blocksToHtml from '@sanity/block-content-to-html'
import client from '../../sanityClient'
import urlBuilder from '@sanity/image-url'
import serializers from '../../components/serializers'
const urlFor = source => urlBuilder(client).image(source)
export async function preload({ params }) {
// the `slug` parameter is available because
// this file is called [slug].html
const { slug } = params
const filter = '*[_type == "post" && slug.current == $slug][0]'
const projection = `{
...,
mainImage{
...,
asset->
},
body[]{
...,
children[]{
...,
_type == 'authorReference' => {
author->
}
}
}
}`
const query = filter + projection
const post = await client.fetch(query, { slug }).catch(err => this.error(500, err))
return { post: {
...post,
mainImage: {
...post.mainImage,
url: urlFor(post.mainImage).auto('format').url()
},
body: blocksToHtml({blocks: post.body, serializers, ...client.clientConfig })
} };
}
</script>
<script>
export let post;
</script>
<style>
/*
By default, CSS is locally scoped to the component,
and any unused styles are dead-code-eliminated.
In this page, Svelte can't know which elements are
going to appear inside the {{{post.html}}} block,
so we have to use the :global(...) modifier to target
all elements inside .content
*/
.content :global(h2) {
font-size: 1.4em;
font-weight: 500;
}
.content :global(pre) {
background-color: #f9f9f9;
box-shadow: inset 1px 1px 5px rgba(0,0,0,0.05);
padding: 0.5em;
border-radius: 2px;
overflow-x: auto;
position: relative;
}
.content :global(pre) :global(code) {
background-color: transparent;
padding: 0;
}
.content :global(pre::after) {
content: attr(data-language);
top: 0;
position: absolute;
right: 0;
background: #ff3e00;
color: #fff;
padding: 2px;
border-radius: 2px;
}
.content :global(img) {
display: block;
max-width:100%;
}
.content :global(figure) {
margin: 0;
}
.content :global(ul) {
line-height: 1.5;
}
.content :global(li) {
margin: 0 0 0.5em 0;
}
</style>
<svelte:head>
<title>{post.title}</title>
</svelte:head>
<h1>{post.title}</h1>
<div class='content'>
<img src={post.mainImage.url} alt={post.mainImage.alt} />
{@html post.body}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment