Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Markdown support — v2 #2882

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/integration-tests/package.json
Expand Up @@ -18,13 +18,15 @@
"@parcel/test-utils": "^1.12.0",
"codecov": "^3.0.0",
"command-exists": "^1.2.6",
"front-matter": "^3.0.1",
"graphql-tag": "^2.6.0",
"json5": "^1.0.1",
"kotlin": "^1.3.11",
"marked": "^0.6.1",
"mocha": "^5.1.1",
"mocha-junit-reporter": "^1.18.0",
"mocha-multi-reporters": "^1.1.7",
"mustache": "^3.0.1",
"ncp": "^2.0.0",
"nyc": "^11.1.0",
"parcel-bundler": "^1.12.3",
Expand Down
@@ -0,0 +1,2 @@
<h1 id="myPrefix_heading1">heading1</h1>
<p>content content content</p>
@@ -0,0 +1,3 @@
# heading1

content content content
@@ -0,0 +1,2 @@
<h1 id="myPrefixFromFrontMatter_heading1">heading1</h1>
<p>content content content</p>
@@ -0,0 +1,7 @@
---
markedConfig:
headerPrefix: myPrefixFromFrontMatter_
---
# heading1

content content content
@@ -0,0 +1,3 @@
module.exports = {
headerPrefix: 'myPrefix_'
}
@@ -0,0 +1,4 @@
<section class="val1">
<h1 id="heading1">heading1</h1>
<p>content content content</p>
</section>
@@ -0,0 +1,7 @@
---
mustacheTemplate: ./template.html
attr1: val1
---
# heading1

content content content
@@ -0,0 +1,3 @@
<section class="{{ attr1 }}">
{{{ contents }}}
</section>
@@ -0,0 +1 @@
<p>content content content</p>
@@ -0,0 +1,4 @@
---
key1: val1
---
content content content
@@ -0,0 +1,3 @@
<h1 id="heading1">heading1</h1>
<p>content content content</p>
<p><a href="/100x100.png">image</a></p>
157 changes: 149 additions & 8 deletions packages/core/integration-tests/test/markdown.js
Expand Up @@ -17,17 +17,158 @@ describe('markdown', function() {
type: 'png',
assets: ['100x100.png'],
childBundles: []
},
{
type: 'js',
assets: ['index.md'],
childBundles: []
}
]
});

let fixture = (await fs.readFile(
path.join(__dirname, '/integration/markdown/index.fixture.html')
))
.toString()
.trim();
let html = (await fs.readFile(path.join(__dirname, '/dist/index.html')))
.toString()
.trim();
let js = (await fs.readFile(path.join(__dirname, '/dist/index.js')))
.toString()
.trim();

assert.equal(html, fixture);
assert(js.includes(JSON.stringify(fixture)));
});

it('should support front matter', async function() {
let b = await bundle(
path.join(__dirname, '/integration/markdown/frontmatter.md')
);

await assertBundleTree(b, {
name: 'frontmatter.html',
assets: ['frontmatter.md'],
childBundles: [
{
type: 'js',
assets: ['frontmatter.md'],
childBundles: []
}
]
});

let fixture = (await fs.readFile(
path.join(__dirname, '/integration/markdown/frontmatter.fixture.html')
))
.toString()
.trim();
let html = (await fs.readFile(
path.join(__dirname, '/dist/frontmatter.html')
))
.toString()
.trim();
let js = (await fs.readFile(path.join(__dirname, '/dist/frontmatter.js')))
.toString()
.trim();

assert.equal(html, fixture);
assert(js.includes(JSON.stringify({key1: 'val1'})));
});

it('should support marked config', async function() {
let b = await bundle(
path.join(__dirname, '/integration/markdown-config/index.md')
);

await assertBundleTree(b, {
name: 'index.html',
assets: ['index.md'],
childBundles: [
{
type: 'js',
assets: ['index.md'],
childBundles: []
}
]
});

let files = await fs.readdir(path.join(__dirname, '/dist'));
let html = await fs.readFile(path.join(__dirname, '/dist/index.html'));
for (let file of files) {
let ext = file.match(/\.([0-9a-z]+)(?:[?#]|$)/i)[0];
if (file !== 'index.html' && ext !== '.map') {
assert(html.includes(file));
}
}
let fixture = (await fs.readFile(
path.join(__dirname, '/integration/markdown-config/index.fixture.html')
))
.toString()
.trim();
let html = (await fs.readFile(path.join(__dirname, '/dist/index.html')))
.toString()
.trim();

assert.equal(html, fixture);
});

it('should support marked config from frontmatter', async function() {
let b = await bundle(
path.join(__dirname, '/integration/markdown-config/local-config.md')
);

await assertBundleTree(b, {
name: 'local-config.html',
assets: ['local-config.md'],
childBundles: [
{
type: 'js',
assets: ['local-config.md'],
childBundles: []
}
]
});

let fixture = (await fs.readFile(
path.join(
__dirname,
'/integration/markdown-config/local-config.fixture.html'
)
))
.toString()
.trim();
let html = (await fs.readFile(
path.join(__dirname, '/dist/local-config.html')
))
.toString()
.trim();

assert.equal(html, fixture);
});

it('should support mustache templating when the template exists', async function() {
let b = await bundle(
path.join(__dirname, '/integration/markdown-templating/index.md')
);

await assertBundleTree(b, {
name: 'index.html',
assets: ['index.md'],
childBundles: [
{
type: 'js',
assets: ['index.md'],
childBundles: []
}
]
});

let fixture = (await fs.readFile(
path.join(
__dirname,
'/integration/markdown-templating/index.fixture.html'
)
))
.toString()
.trim();
let html = (await fs.readFile(path.join(__dirname, '/dist/index.html')))
.toString()
.trim();

assert.equal(html, fixture);
});
});
79 changes: 77 additions & 2 deletions packages/core/parcel-bundler/src/assets/MarkdownAsset.js
@@ -1,5 +1,7 @@
const localRequire = require('../utils/localRequire');
const Asset = require('../Asset');
const path = require('path');
const fs = require('@parcel/fs');

class MarkdownAsset extends Asset {
constructor(name, options) {
Expand All @@ -8,8 +10,81 @@ class MarkdownAsset extends Asset {
this.hmrPageReload = true;
}
async generate() {
let marked = await localRequire('marked', this.name);
return marked(this.contents);
const fm = await localRequire('front-matter', this.name);
const marked = await localRequire('marked', this.name);

let markedOptions = {};

const markedConfig = await this.getConfig(['marked.config.js']);
if (markedConfig) {
markedOptions = {
markedOptions,
...markedConfig
};
}

const doc = fm(this.contents);

this.frontMatterAttributes = doc.attributes;

const perFileConfig = doc.attributes['markedConfig'];
if (perFileConfig && typeof perFileConfig === 'object') {
markedOptions = {
markedOptions,
...perFileConfig
};
}

// .trim() is important here – test asserts rely on trimmed strings for easy comparison
let contents = marked(doc.body, markedOptions).trim();

contents = await this.applyMustacheTemplate(contents, doc.attributes);

return [
{
type: 'html',
value: contents
}
];
}

async postProcess(generated) {
const compiledMarkdown = generated.find(t => t.type === 'html').value;

const jsModule = {
contents: compiledMarkdown,
attributes: this.frontMatterAttributes
};

generated.push({
type: 'js',
value: `module.exports=${JSON.stringify(jsModule)}`
});
return generated;
}

async applyMustacheTemplate(contents, attributes) {
const templatePath = attributes['mustacheTemplate'];
if (!templatePath) {
return contents;
}

if (typeof templatePath !== 'string') {
throw new Error(`${this.name}: mustacheTemplate should be a string`);
}

const templatePathNormalized = path.join(
path.dirname(this.name),
templatePath
);

const template = (await fs.readFile(templatePathNormalized)).toString();

const mustache = await localRequire('mustache', this.name);
return mustache.render(template, {
contents,
...attributes
});
}
}
module.exports = MarkdownAsset;
7 changes: 7 additions & 0 deletions yarn.lock
Expand Up @@ -4675,6 +4675,13 @@ from2@^2.1.0:
inherits "^2.0.1"
readable-stream "^2.0.0"

front-matter@3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/front-matter/-/front-matter-3.0.1.tgz#9ed0a8e2771b1367c4d8809b2f91c528b9b04d0c"
integrity sha512-iCHZ7RZGE36uG58iIWp8zrhDi9BZjlDiRj7aRcGm45EIqrbK+u4KTAmRKLG3FOaVkFhZI5/29SUo7sMLzlQkcA==
dependencies:
js-yaml "^3.10.0"

fs-extra@^7.0.0:
version "7.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
Expand Down