Skip to content

Instantly share code, notes, and snippets.

@albertBarsegyan
Created October 3, 2021 16:43
Show Gist options
  • Save albertBarsegyan/6c2c13d41d2b42b204fae71ed6abe036 to your computer and use it in GitHub Desktop.
Save albertBarsegyan/6c2c13d41d2b42b204fae71ed6abe036 to your computer and use it in GitHub Desktop.
get markdown from file
import matter from 'gray-matter';
import path from 'path';
import fs from 'fs';
export default function getDocBySlug(slug, locale = 'us') {
const docsDirectory = path.join(process.cwd(), '/src/content');
const realSlug = slug.replace(/\.mdx$/, '');
const fullPath = path.join(docsDirectory, slug, `${realSlug}.${locale}.mdx`);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { content, data } = matter(fileContents);
return { slug: realSlug, meta: data, content };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment