Created
May 29, 2020 15:24
-
-
Save ryanditjia/28a5e2f605044647a3d657465fe980f4 to your computer and use it in GitHub Desktop.
Sapper rollup + markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fm from 'front-matter' | |
import path from 'path' | |
export function markdown() { | |
return { | |
name: 'rollup-plugin-markdown', | |
transform(code, id) { | |
// Check the file extension | |
const extension = path.extname(id) | |
// Only handle .md files | |
if (extension !== '.md') return | |
// Generate slug from filename | |
const slug = path.basename(id, extension) | |
// Use `front-matter` lib to extract frontmatter and body | |
const { attributes: frontmatter, body } = fm(code) | |
// The resulting object when you import markdown files | |
const exportFromModule = JSON.stringify({ | |
slug, | |
body, | |
frontmatter, | |
}) | |
return { | |
// Use like so: | |
// import post from '../posts/example-blog-post.md' | |
code: `export default ${exportFromModule}`, | |
map: { mappings: '' }, | |
} | |
}, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { markdown } from './rollup-plugin-markdown' | |
import glob from 'rollup-plugin-glob' | |
export default { | |
server: { | |
plugins: [ | |
glob(), | |
markdown(), | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment