Skip to content

Commit 8efe924

Browse files
committed
init
1 parent 28aed8b commit 8efe924

17 files changed

+7638
-2
lines changed

.eslintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": [
3+
"react-app",
4+
"prettier"
5+
],
6+
"plugins": [
7+
"prettier"
8+
],
9+
"rules": {
10+
"jsx-a11y/anchor-has-content": "off",
11+
"prettier/prettier": ["error", {
12+
"singleQuote": true,
13+
"trailingComma": "all",
14+
"bracketSpacing": false,
15+
"jsxBracketSameLine": true,
16+
"parser": "flow"
17+
}]
18+
}
19+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

README.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,67 @@
1-
# react-static-markdown
2-
Markdown files utils for react-static
1+
# [react-static-markdown](https://github.com/tb/react-static-markdown)
2+
3+
Markdown files utils for [react-static](https://github.com/nozzle/react-static)
4+
5+
## Install
6+
7+
yarn add -D globby gray-matter mdxc mdx-loader
8+
yarn add react react-static-markdown
9+
10+
## Example usage with mdx-loader
11+
12+
static.config.js
13+
14+
webpack: (config, {defaultLoaders, stage}) => {
15+
config.resolve.extensions.push('.md');
16+
config.module.rules = [
17+
{
18+
oneOf: [
19+
{
20+
test: /\.md$/,
21+
use: ['mdx-loader'],
22+
},
23+
defaultLoaders.cssLoader,
24+
defaultLoaders.jsLoader,
25+
defaultLoaders.fileLoader,
26+
],
27+
},
28+
];
29+
return config;
30+
},
31+
32+
Usage
33+
34+
import About from 'content/pages/about.md';
35+
36+
...
37+
38+
<About />
39+
40+
## Example usage without mdx-loader
41+
42+
Markdown is transformed to mdx during `getRoutes` and then evaluated to component.
43+
44+
static.config.js
45+
46+
const getFiles = require('react-static-mdx/getFilesWithMdx');
47+
48+
...
49+
50+
const pages = await getFiles('content/pages/*.md');
51+
52+
Usage
53+
54+
import mdxToComponent from 'react-static-mdx/mdxToComponent';
55+
56+
...
57+
58+
const Page = mdxToComponent(page.contentmdx);
59+
60+
...
61+
62+
<Page {...props} />
63+
64+
## License
65+
66+
React Static uses the MIT license. For more information on this license, [click here](https://github.com/tb/react-static-markdown/blob/master/LICENSE).
67+

getFiles.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require('fs');
2+
const globby = require('globby');
3+
const matter = require('gray-matter');
4+
5+
module.exports = async patterns =>
6+
(await globby(patterns)).map(filepath => {
7+
const {data, content} = matter(fs.readFileSync(filepath, 'utf8'));
8+
return Object.assign({content}, data);
9+
});

getFilesWithMdx.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const getFiles = require('./getFiles');
2+
const renderMdx = require('./renderMdx');
3+
4+
module.exports = async patterns =>
5+
(await getFiles(patterns)).map(data =>
6+
Object.assign({contentmdx: renderMdx(data.content)}, data),
7+
);

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
getFiles: require('./getFiles'),
3+
getFilesWithMdx: require('./getFilesWithMdx'),
4+
mdxToComponent: require('./mdxToComponent'),
5+
renderMdx: require('./renderMdx'),
6+
};

mdxToComponent.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const React = require('react');
2+
3+
module.exports = (code, factories = {}) => {
4+
const module = {exports: null};
5+
const {createFactory, createElement} = React;
6+
eval(code); /* eslint no-eval: 0 */
7+
return module.exports;
8+
};

0 commit comments

Comments
 (0)