Skip to content

inline compilation of html #2345

@btakita

Description

@btakita

One advantage that react has over svelte is the ability to compile in a function. It would be nice if svelte could do the same.

Instead of manipulating a string in:

https://github.com/sveltejs/svelte.technology/blob/master/src/routes/blog/rss.xml.js

something like the following could be used:

import get_posts from '../api/blog/_posts.js';
import { interpret, deindent } from 'svelte/interpreter'

const months = ',Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split( ',' );

function formatPubdate ( str ) {
	const [ y, m, d ] = str.split( '-' );
	return `${d} ${months[+m]} ${y}`;
}

const rss = interpret({post, formatPubdate, get_posts}, deindent`
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
	<title>Svelte blog</title>
	<link>https://svelte.technology/blog</link>
	<description>News and information about the magical disappearing UI framework</description>
	<image>
		<url>https://svelte.technology/favicon.png</url>
		<title>Svelte</title>
		<link>https://svelte.technology/blog</link>
	</image>
	{#each get_posts() as post}
		<item>
			<title>{post.metadata.title}</title>
			<link>https://svelte.technology/blog/{post.slug}</link>
			<description>{post.metadata.description}</description>
			<pubDate>{formatPubdate(post.metadata.pubdate)}</pubDate>
		</item>
	{/each}
</channel>
</rss>
`)

export function get(req, res) {
	res.set({
		'Cache-Control': `max-age=${30 * 60 * 1e3}`,
		'Content-Type': 'application/rss+xml'
	});
	res.end(rss);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions