Skip to content

Instantly share code, notes, and snippets.

@babichjacob
Last active August 1, 2021 03:28
Show Gist options
  • Save babichjacob/c6907dd261fb3776044f618372d59470 to your computer and use it in GitHub Desktop.
Save babichjacob/c6907dd261fb3776044f618372d59470 to your computer and use it in GitHub Desktop.
mdsvex compiled in an endpoint (proof of concept)
import { dev } from "$app/env";
import { createRequire } from "module";
import { compile as mdsvexCompile } from "mdsvex";
import { compile as svelteCompile } from "svelte/compiler";
import "svelte/internal";
import type { RequestHandler } from '@sveltejs/kit';
const BUILD_DESTINATION = "/Users/jacob/Repositories/test-university-website/build/app.js";
const mdsvexConfig = {
extensions: [".svelte.md"],
smartypants: {
dashes: "oldschool",
},
}
const testSource = `
# Hello
I'm **bold** text and I was rendered by [mdsvex](https://mdsvex.com/)!.
`;
export const get: RequestHandler = async () => {
const preprocessed = await mdsvexCompile(testSource, mdsvexConfig);
const compiled = svelteCompile(
preprocessed.code,
{
generate: 'ssr',
format: "cjs",
hydratable: false,
}
);
const require = createRequire(dev ? import.meta.url : BUILD_DESTINATION);
const module = { exports: {} };
const exports = module.exports;
// Force preserve
console.log({ require, module, exports });
eval(compiled.js.code);
const rendered = module.exports.default.render();
const renderedHTML = rendered.html;
const document = `<html>
<head>
<meta charset="UTF-8">
</head>
</html>
<body>
${renderedHTML}
</body>
`
return {
body: document,
headers: {
"Content-Type": "text/html",
},
status: 200,
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment