我正在使用 Sapper 使用默认创建博客工兵模板汇总 https://github.com/sveltejs/sapper-template-rollup.
在博客文件夹中,它确实提到了从 Markdown 文件生成数据。但我找不到该怎么做?
我发表了https://github.com/mikenikles/sapper-template-with-markdown https://github.com/mikenikles/sapper-template-with-markdown它显示了如何使用默认的 Sapper 模板,但使用*.md
博客文章内容的文件。
主要变化在于src/routes/blog/_posts.js
我将内容替换为:
const fs = require('fs');
const frontMatter = require('front-matter');
const marked = require('marked');
const posts = fs.readdirSync('./src/posts').map(postFilename => {
const postContent = fs.readFileSync(`./src/posts/${postFilename}`, {
encoding: 'utf8'
});
const postFrontMatter = frontMatter(postContent);
return {
title: postFrontMatter.attributes.title,
slug: postFrontMatter.attributes.slug,
html: marked(postFrontMatter.body)
}
});
posts.forEach(post => {
post.html = post.html.replace(/^\t{3}/gm, '');
});
export default posts;
然后,每篇博客文章都存储在src/posts
作为具有以下格式的 Markdown 文件:
---
title: 'What is Sapper?'
slug: 'what-is-sapper'
---
Your markdown content.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)