Skip to content

Commit

Permalink
add front matter support to compileStatic method
Browse files Browse the repository at this point in the history
  • Loading branch information
soumak77 committed Aug 29, 2016
1 parent 82a7496 commit 33312eb
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 120 deletions.
60 changes: 31 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ const DEFAULT_DESTINATION_FN = path => {
};
const DEFAULT_FRONT_MATTER_SEPARATOR = '---';

const performMinify = (htmlPages, data) => {
if (htmlPages.disabled) {
if (htmlPages.forceRemoveFrontMatter) {
const frontmatter = fm(data);
return frontmatter.body;
} else {
return data;
}
} else {
if (!htmlPages.preserveFrontMatter && !htmlPages.removeFrontMatter) {
return minify(data, htmlPages.htmlMinOptions);
} else {
if (htmlPages.removeFrontMatter) {
// strip out front matter
const frontmatter = fm(data);
return minify(frontmatter.body, htmlPages.htmlMinOptions);
} else {
// minify and add back front matter
const frontmatter = fm(data);
return htmlPages.frontMatterSeparator + '\n' +
frontmatter.frontmatter + '\n' +
htmlPages.frontMatterSeparator + '\n' +
minify(frontmatter.body, htmlPages.htmlMinOptions);
}
}
}
};

class HtmlPages {
constructor(config) {
if (config === undefined) config = {};
Expand All @@ -57,35 +85,9 @@ class HtmlPages {
}

compile(file, path, callback) {
let err, error, result;
let err, error;
try {
const frontmatter = fm(file);

if (this.disabled) {
if (this.forceRemoveFrontMatter) {
result = frontmatter.body;
} else {
result = file;
}
} else {
if (!this.preserveFrontMatter && !this.removeFrontMatter) {
result = minify(file, this.htmlMinOptions);
} else {
const frontmatter = fm(file);

if (this.removeFrontMatter) {
// strip out front matter
result = minify(frontmatter.body, this.htmlMinOptions);
} else {
// minify and add back front matter
result = this.frontMatterSeparator + '\n' +
frontmatter.frontmatter + '\n' +
this.frontMatterSeparator + '\n' +
minify(frontmatter.body, this.htmlMinOptions);
}
}
}

const result = performMinify(this, file);
const destinationPath = sysPath.join(this.publicPath, this.destinationFn(path));
const destinationDir = sysPath.dirname(destinationPath);
mkdirp.sync(destinationDir);
Expand All @@ -105,7 +107,7 @@ class HtmlPages {

return new Promise(resolve => {
resolve({
data: this.compileAssets && !this.disabled ? minify(data, this.htmlMinOptions) : data,
data: this.compileAssets ? performMinify(this, data) : data,
path: sysPath.join(this.publicPath, this.destinationFn(path))
});
});
Expand Down
Loading

0 comments on commit 33312eb

Please sign in to comment.