Skip to content

Instantly share code, notes, and snippets.

@twilson63
Last active October 22, 2020 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twilson63/5fc02c0118a96562c0eb7c4c5a35f6bd to your computer and use it in GitHub Desktop.
Save twilson63/5fc02c0118a96562c0eb7c4c5a35f6bd to your computer and use it in GitHub Desktop.
Quick steps to add MdSvex to Sapper

Markdown ⬇️ with Sapper πŸ”₯ Setup

This is a short post on how to setup a sapper project with markdown. Markdown is a great way to write posts and documentation. Sapper gives you a SSR or SSG svelte app out of the box. This post documents the process to setup a sapper project with markdown support. Specifically Mdsvex, which allows you to not only write markdown, but to use svelte within your markdown, giving you the best of both worlds. Yay!

Checkout Mdsvex - https://mdsvex.pngwn.io/

πŸ’™πŸ’™πŸ’™πŸ’™ I love markdown!!!

πŸ’™πŸ’™πŸ’™πŸ’™ I love svelte!!!

Getting started

The first thing you do is create a sapper project

npx degit sveltejs/sapper-template#rollup my_project
cd my_project

Next we need to initialize the project and install mdsvex

yarn
yarn add -D mdsvex

In order to configure sapper to compile markdown files we need to modify the package.json and the rollup.config.js files

In the package.json scripts section we need to tell the sapper cli to look for .svx files

"scripts": {
  "dev": "sapper dev --ext '.svelte .svx'",
  "build": "sapper build --legacy --ext '.svelte .svx'",
  "export": "sapper export --legacy --ext '.svelte .svx'"
}

In the rollup.config.js file we need to import mdsvex and add it as a svelte preprocessor and modify the svelte extensions for both the client and server nodes of the rollup config.

import { mdsvex } from 'mdsvex'

...

const extensions = ['.svelte', '.svx']

...

client: {
  ...
  plugins: [
    svelte({
      extensions,
      preprocess: mdsvex()
      ...
    }),
    ...
  ]
},
server: {
  ...
  plugins: [
    svelte({
      extensions,
      preprocess: mdsvex()
      ...
    })
  ]
 }

πŸŽ‰πŸŽ‰πŸŽ‰ Thats it! πŸŽ‰πŸŽ‰πŸŽ‰

Now create a svx file in the routes folder and it should render:

hello.svx

# Hello From Mdsvex
yarn dev

open http://localhost:3000/hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment