Skip to content

Commit 7d7bde4

Browse files
committed
Merge branch 'fix-frontmatter-pr'
2 parents 0f35eae + 378de31 commit 7d7bde4

File tree

6 files changed

+36
-13
lines changed

6 files changed

+36
-13
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
"statements": 100
4848
},
4949
"dependencies": {
50+
"camelize": "^1.0.0",
51+
"except": "^0.1.3",
5052
"front-matter": "^2.1.0",
51-
"prismjs": "^1.5.1",
53+
"node-prismjs": "^0.1.0",
5254
"remarkable": "^1.6.2"
5355
},
5456
"devDependencies": {

src/build.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
'use strict';
2+
const camelize = require('camelize');
3+
const except = require('except');
24

35
/**
46
* @typedef HTMLObject
@@ -17,9 +19,11 @@ module.exports = function build(markdown) {
1719

1820
let doImports = 'import React from \'react\';\n';
1921
const
20-
imports = markdown.imports || {},
22+
imports = markdown.attributes.imports || {},
2123
jsx = markdown.html.replace(/class=/g, 'className=');
2224

25+
const frontMatterAttributes = except(markdown.attributes, 'imports');
26+
2327
for (const variable in imports) {
2428
// eslint-disable-next-line no-prototype-builtins
2529
if (imports.hasOwnProperty(variable)) {
@@ -30,7 +34,8 @@ module.exports = function build(markdown) {
3034
return `
3135
${doImports}
3236
33-
module.exports = function() {
37+
export const attributes = ${JSON.stringify(camelize(frontMatterAttributes))};
38+
export default function() {
3439
return (
3540
<div>
3641
${jsx}

src/parser.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
const
44
frontMatter = require('front-matter'),
5-
Prism = require('prismjs'),
5+
Prism = require('node-prismjs'),
66
Remarkable = require('remarkable'),
77
escapeHtml = require('remarkable/lib/common/utils').escapeHtml,
88
md = new Remarkable();
99

10-
require('prismjs/components/prism-jsx');
11-
1210
/**
1311
* Wraps the code and jsx in an html component
1412
* for styling it later
@@ -83,7 +81,8 @@ function parseMarkdown(markdown) {
8381

8482
const options = {
8583
highlight(code, lang) {
86-
return Prism.highlight(code, Prism.languages[lang]);
84+
const language = Prism.languages[lang] || Prism.languages.autoit;
85+
return Prism.highlight(code, language);
8786
},
8887
xhtmlOut: true
8988
};
@@ -103,7 +102,7 @@ function parseMarkdown(markdown) {
103102

104103
try {
105104
html = md.render(markdown.body);
106-
resolve({ html, imports: markdown.attributes.imports });
105+
resolve({ html, attributes: markdown.attributes });
107106
} catch (err) {
108107
return reject(err);
109108
}

test/build.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ describe('Build Component', () => {
3636
component.should.contain('import HelloWorld from \'./hello-world.js\';\n');
3737
});
3838

39+
it('exports the front-matter attributes', () => {
40+
component.should.contain('export const attributes = {"testFrontMatter":"hello world"}');
41+
});
42+
3943
});

test/examples/hello-world.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
test-front-matter: 'hello world'
23
imports:
34
Button: './button.js'
45
HelloWorld: './hello-world.js'

test/parser.spec.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,32 @@ describe('Parse Markdown', () => {
5151
</div>`);
5252
});
5353

54-
it('parses markdown with live code blocks', () =>
54+
it('parses markdown with live code blocks', done => {
5555
parser.parse(mdExample).then(result => {
5656
result.html.should.contain(`<div class="run"><HelloWorld />
5757
<Button label="Hello World" />
5858
</div>`);
5959
})
60-
);
60+
.then(done)
61+
.catch(done);
62+
});
6163

62-
it('parses markdown and created valid html for JSX', () => {
64+
it('parses markdown and created valid html for JSX', done => {
6365
const
6466
exampleCode = '![](myImage.png)';
6567
parser.parse(exampleCode).then(result => {
66-
result.html.should.ewual('<p><img src="myImage.png" alt="" /></p>\n');
67-
});
68+
result.html.should.equal('<p><img src="myImage.png" alt="" /></p>\n');
69+
})
70+
.then(done)
71+
.catch(done);
72+
});
73+
74+
it('provides the front-matter attributes', done => {
75+
parser.parse(mdExample).then(result => {
76+
result.attributes['test-front-matter'].should.equal('hello world');
77+
})
78+
.then(done)
79+
.catch(done);
6880
});
6981

7082
});

0 commit comments

Comments
 (0)