From 33312ebeff0190e3f903450a968a34ddadf5af27 Mon Sep 17 00:00:00 2001 From: Brian Soumakian Date: Mon, 29 Aug 2016 02:21:02 -0700 Subject: [PATCH] add front matter support to compileStatic method --- index.js | 60 ++++++----- test.js | 316 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 256 insertions(+), 120 deletions(-) diff --git a/index.js b/index.js index 18cc6b1..52097bf 100644 --- a/index.js +++ b/index.js @@ -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 = {}; @@ -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); @@ -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)) }); }); diff --git a/test.js b/test.js index 7978e94..dbc67fd 100644 --- a/test.js +++ b/test.js @@ -7,7 +7,6 @@ describe('Plugin', () => { const plugin = new Plugin({ paths: {public: 'build'} }); - const path = "./build"; it('should be an object', () => { expect(plugin).to.be.an('object'); @@ -21,122 +20,257 @@ describe('Plugin', () => { expect(plugin.compile).to.be.a('function'); }); - it('should compile and produce a build file', function (done) { - plugin.compile('

blah

', '', function (error) { - expect(error).not.to.be.ok; - expect(fs.existsSync(path)).to.be.ok; - fs.unlinkSync(path); - done(); + describe('Plugin.compile', () => { + const path = "./build"; + + afterEach(done => { + fs.unlink(path, done); }); - }); - it('should minify file', function (done) { - plugin.disabled = false; - plugin.htmlMin = { - removeComments: true - }; + it('should compile and produce a build file', function (done) { + plugin.disabled = false; - var content = '

blah

'; - var minified = '

blah'; + var content = '

blah

'; + var minified = '

blah'; - testCompile(content, minified, done); - }); + testCompile(content, minified, done); + }); - it('should minify front matter', function (done) { - plugin.htmlMin = { - preserveLineBreaks: false, - collapseWhitespace: true - }; - plugin.preserveFrontMatter = false; + it('should minify file', function (done) { + plugin.disabled = false; + plugin.htmlMin = { + removeComments: true + }; - var content = '---\ntitle: test\nname: test\n---\n

blah

'; - var minified = '--- title: test name: test ---

blah'; + var content = '

blah

'; + var minified = '

blah'; - testCompile(content, minified, done); - }); + testCompile(content, minified, done); + }); - it('should preserve front matter', function (done) { - plugin.disabled = false; - plugin.preserveFrontMatter = true; - plugin.removeFrontMatter = false; - plugin.htmlMin = { - preserveLineBreaks: false, - collapseWhitespace: true - }; + it('should minify front matter', function (done) { + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + plugin.preserveFrontMatter = false; - var content = '---\ntitle: test\nname: test\n---\n

blah

'; - var minified = '---\ntitle: test\nname: test\n---\n

blah'; + var content = '---\ntitle: test\nname: test\n---\n

blah

'; + var minified = '--- title: test name: test ---

blah'; - testCompile(content, minified, done); - }); + testCompile(content, minified, done); + }); - it('should preserve front matter with custom separator', function (done) { - plugin.disabled = false; - plugin.preserveFrontMatter = true; - plugin.removeFrontMatter = false; - plugin.frontMatterSeparator = '= yaml ='; - plugin.htmlMin = { - preserveLineBreaks: false, - collapseWhitespace: true - }; + it('should preserve front matter', function (done) { + plugin.disabled = false; + plugin.preserveFrontMatter = true; + plugin.removeFrontMatter = false; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; - var content = '= yaml =\ntitle: test\nname: test\n= yaml =\n

blah

'; - var minified = '= yaml =\ntitle: test\nname: test\n= yaml =\n

blah'; + var content = '---\ntitle: test\nname: test\n---\n

blah

'; + var minified = '---\ntitle: test\nname: test\n---\n

blah'; - testCompile(content, minified, done); - }); + testCompile(content, minified, done); + }); - it('should remove front matter', function (done) { - plugin.disabled = false; - plugin.htmlMin = { - preserveLineBreaks: false, - collapseWhitespace: true - }; - plugin.removeFrontMatter = true; + it('should preserve front matter with custom separator', function (done) { + plugin.disabled = false; + plugin.preserveFrontMatter = true; + plugin.removeFrontMatter = false; + plugin.frontMatterSeparator = '= yaml ='; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; - var content = '---\ntitle: test\nname: test\n---\n

blah

'; - var minified = '

blah'; + var content = '= yaml =\ntitle: test\nname: test\n= yaml =\n

blah

'; + var minified = '= yaml =\ntitle: test\nname: test\n= yaml =\n

blah'; - testCompile(content, minified, done); - }); + testCompile(content, minified, done); + }); - it('should not remove front matter when disabled', function (done) { - plugin.disabled = true; - plugin.htmlMin = { - preserveLineBreaks: false, - collapseWhitespace: true - }; - plugin.removeFrontMatter = true; + it('should remove front matter', function (done) { + plugin.disabled = false; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + plugin.removeFrontMatter = true; + + var content = '---\ntitle: test\nname: test\n---\n

blah

'; + var minified = '

blah'; + + testCompile(content, minified, done); + }); + + it('should not remove front matter when disabled', function (done) { + plugin.disabled = true; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + plugin.removeFrontMatter = true; + + var content = '---\ntitle: test\nname: test\n---\n

blah

'; + + testCompile(content, content, done); + }); + + it('should force remove front matter when disabled', function (done) { + plugin.disabled = true; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + plugin.forceRemoveFrontMatter = true; + + var content = '---\ntitle: test\nname: test\n---\n

blah

'; + var minified = '

blah

'; + + testCompile(content, minified, done); + }); + + function testCompile(content, expected, done) { + plugin.compile(content, '', function (error) { + expect(error).not.to.be.ok; + expect(fs.existsSync(path)).to.be.ok; - var content = '---\ntitle: test\nname: test\n---\n

blah

'; + const filecontents = fs.readFileSync(path); + expect(filecontents.toString()).to.equal(expected); - testCompile(content, content, done); + done(); + }); + } }); - it('should force remove front matter when disabled', function (done) { - plugin.disabled = true; - plugin.htmlMin = { - preserveLineBreaks: false, - collapseWhitespace: true + describe('Plugin.compileStatic', () => { + const testfile = { + data: "", + path: "./build" }; - plugin.forceRemoveFrontMatter = true; - var content = '---\ntitle: test\nname: test\n---\n

blah

'; - var minified = '

blah

'; + it('should compile and produce a build file', function (done) { + plugin.disabled = false; + plugin.compileAssets = true; - testCompile(content, minified, done); - }); + testfile.data = '

blah

'; + var minified = '

blah'; + + testCompileStatic(testfile, minified, done); + }); + + it('should minify file', function (done) { + plugin.disabled = false; + plugin.htmlMin = { + removeComments: true + }; + plugin.preserveFrontMatter = false; + plugin.removeFrontMatter = false; + plugin.forceRemoveFrontMatter = false; + + testfile.data = '

blah

'; + var minified = '

blah'; + + testCompileStatic(testfile, minified, done); + }); + + it('should minify front matter', function (done) { + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + plugin.preserveFrontMatter = false; + plugin.removeFrontMatter = false; + plugin.forceRemoveFrontMatter = false; + + testfile.data = '---\ntitle: test\nname: test\n---\n

blah

'; + var minified = '--- title: test name: test ---

blah'; + + testCompileStatic(testfile, minified, done); + }); + + it('should preserve front matter', function (done) { + plugin.disabled = false; + plugin.preserveFrontMatter = true; + plugin.removeFrontMatter = false; + plugin.forceRemoveFrontMatter = false; + plugin.frontMatterSeparator = '---'; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + + testfile.data = '---\ntitle: test\nname: test\n---\n

blah

'; + var minified = '---\ntitle: test\nname: test\n---\n

blah'; + + testCompileStatic(testfile, minified, done); + }); - function testCompile(content, expected, done) { - plugin.compile(content, '', function (error) { - expect(error).not.to.be.ok; - expect(fs.existsSync(path)).to.be.ok; + it('should preserve front matter with custom separator', function (done) { + plugin.disabled = false; + plugin.preserveFrontMatter = true; + plugin.removeFrontMatter = false; + plugin.frontMatterSeparator = '= yaml ='; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; - const filecontents = fs.readFileSync(path); - expect(filecontents.toString()).to.equal(expected); + testfile.data = '= yaml =\ntitle: test\nname: test\n= yaml =\n

blah

'; + var minified = '= yaml =\ntitle: test\nname: test\n= yaml =\n

blah'; - fs.unlinkSync(path); - done(); + testCompileStatic(testfile, minified, done); }); - } + + it('should remove front matter', function (done) { + plugin.disabled = false; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + plugin.removeFrontMatter = true; + + testfile.data = '---\ntitle: test\nname: test\n---\n

blah

'; + var minified = '

blah'; + + testCompileStatic(testfile, minified, done); + }); + + it('should not remove front matter when disabled', function (done) { + plugin.disabled = true; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + plugin.removeFrontMatter = true; + + testfile.data = '---\ntitle: test\nname: test\n---\n

blah

'; + + testCompileStatic(testfile, testfile.data, done); + }); + + it('should force remove front matter when disabled', function (done) { + plugin.disabled = true; + plugin.htmlMin = { + preserveLineBreaks: false, + collapseWhitespace: true + }; + plugin.forceRemoveFrontMatter = true; + + testfile.data = '---\ntitle: test\nname: test\n---\n

blah

'; + var minified = '

blah

'; + + testCompileStatic(testfile, minified, done); + }); + + function testCompileStatic(testfile, expected, done) { + plugin.compileStatic(testfile).then((file) => { + expect(file.data).to.equal(expected); + done(); + }).catch(done); + } + }); });