Skip to content

Commit

Permalink
feat: 🎸 pass vfile data to route data when present
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Reese committed Nov 10, 2020
1 parent cae0639 commit 2e7a776
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const remarkHtml = require('remark-html');
const prepareMarkdownParser = require('./utils/prepareMarkdownParser');

const extractFrontmatter = require('remark-extract-frontmatter');
const frontmatter = require('remark-frontmatter');
const remarkFrontmatter = require('remark-frontmatter');
const yaml = require('yaml').parse;

const plugin = {
Expand All @@ -24,7 +24,7 @@ const plugin = {

if (plugin.config.remarkPlugins.length === 0) {
plugin.config.remarkPlugins = [
frontmatter,
remarkFrontmatter,
[extractFrontmatter, { name: 'frontmatter', yaml: yaml }],
remarkHtml,
];
Expand All @@ -36,9 +36,7 @@ const plugin = {
if (typeof plugin.config.useSyntaxHighlighting !== 'boolean') {
rehypeShikiConfig = plugin.config.useSyntaxHighlighting;
}
plugin.config.remarkPlugins.push(
[rehypeShiki, rehypeShikiConfig]
);
plugin.config.remarkPlugins.push([rehypeShiki, rehypeShikiConfig]);
}

plugin.markdownParser = prepareMarkdownParser(plugin.config.remarkPlugins);
Expand All @@ -62,14 +60,17 @@ const plugin = {
}
}

const { data, contents: html, data: { frontmatter } } = await plugin.markdownParser.process(md);
const {
contents: html,
data: { frontmatter, ...data },
} = await plugin.markdownParser.process(md);
let slug;

if (plugin.config.slugFormatter && typeof(plugin.config.slugFormatter) === 'function') {
if (plugin.config.slugFormatter && typeof plugin.config.slugFormatter === 'function') {
let relativePath = file.replace(`${mdsInRoute}/`, '');
slug = plugin.config.slugFormatter(relativePath, frontmatter);
}
if (typeof(slug) !== 'string') {
if (typeof slug !== 'string') {
if (frontmatter && frontmatter.slug) {
slug = frontmatter.slug;
} else {
Expand All @@ -83,6 +84,7 @@ const plugin = {
slug,
frontmatter,
html,
data,
});
plugin.requests.push({ slug, route });
}
Expand Down Expand Up @@ -172,11 +174,12 @@ const plugin = {
if (data.markdown && data.markdown[request.route]) {
const markdown = data.markdown[request.route].find((m) => m.slug === request.slug);
if (markdown) {
let { html, frontmatter } = markdown;
let { html, frontmatter, data: addToData } = markdown;

return {
data: {
...data,
...addToData,
html,
frontmatter,
},
Expand Down

0 comments on commit 2e7a776

Please sign in to comment.