Skip to content

Commit

Permalink
Bug/issue 456 top level pages not building correctly (#476)
Browse files Browse the repository at this point in the history
* better support for top level pages

* minor refactor
  • Loading branch information
thescientist13 committed Apr 3, 2021
1 parent 285aa2f commit 08b710b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/cli/src/plugins/resource/plugin-standard-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ const unified = require('unified');
// general refactoring
const getPageTemplate = (barePath, workspace, template) => {
const templatesDir = path.join(workspace, 'templates');
const pageIsHtmlPath = `${barePath.substring(0, barePath.lastIndexOf('/index'))}.html`;

if (template && fs.existsSync(`${templatesDir}/${template}.html`)) {
// use a predefined template, usually from markdown frontmatter
contents = fs.readFileSync(`${templatesDir}/${template}.html`, 'utf-8');
} else if (fs.existsSync(`${barePath}.html`)) {
} else if (fs.existsSync(`${barePath}.html`) || fs.existsSync(pageIsHtmlPath)) {
// if the page is already HTML, use that as the template
contents = fs.readFileSync(`${barePath}.html`, 'utf-8');
const indexPath = fs.existsSync(pageIsHtmlPath)
? pageIsHtmlPath
: `${barePath}.html`;

contents = fs.readFileSync(indexPath, 'utf-8');
} else if (fs.existsSync(`${templatesDir}/page.html`)) {
// else look for default page template
contents = fs.readFileSync(`${templatesDir}/page.html`, 'utf-8');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Use Case
* Run Greenwood with default config and mixed HTML and markdown top level pages.
*
* Result
* Test for correct page output and layout.
*
* Command
* greenwood build
*
* User Config
* None (Greenwood default)
*
* User Workspace
* src/
* pages/
* about.html
* contact.md
* index.html
*/
const expect = require('chai').expect;
const fs = require('fs');
const { JSDOM } = require('jsdom');
const path = require('path');
const TestBed = require('../../../../../test/test-bed');

describe('Build Greenwood With: ', function() {
const LABEL = 'Default Greenwood Configuration and Default Workspace w/ Top Level Pages';
let setup;

before(async function() {
setup = new TestBed();
this.context = await setup.setupTestBed(__dirname);
});

describe(LABEL, function() {
before(async function() {
await setup.runGreenwoodCommand('build');
});

// TODO runSmokeTest(['public', 'not-found', 'index'], LABEL);
runSmokeTest(['public'], LABEL);

describe('Home (index) Page', function() {
let dom;

before(async function() {
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html'));
});

it('should create a top level home (index) page with just an index.html', function() {
expect(fs.existsSync(path.join(this.context.publicDir, './index.html'))).to.be.true;
});

it('should have the correct content for the home page', function() {
const h1Tags = dom.window.document.querySelectorAll('h1');

expect(h1Tags.length).to.equal(1);
expect(h1Tags[0].textContent).to.equal('Hello from the home page!!!!');
});
});

describe('About Page', function() {
let dom;

before(async function() {
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'about', 'index.html'));
});

it('should create a top level about page with a directory and index.html', function() {
expect(fs.existsSync(path.join(this.context.publicDir, 'about', 'index.html'))).to.be.true;
});

it('should have the correct content for the about page', function() {
const h1Tags = dom.window.document.querySelectorAll('h1');
const pTags = dom.window.document.querySelectorAll('p');

expect(h1Tags.length).to.equal(1);
expect(h1Tags[0].textContent).to.equal('Hello from about.html');

expect(pTags.length).to.equal(1);
expect(pTags[0].textContent).to.equal('Lorum Ipsum');
});
});

describe('Contact Page', function() {
let dom;

before(async function() {
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'contact', 'index.html'));
});

it('should create a top level contact page with a directory and index.html', function() {
expect(fs.existsSync(path.join(this.context.publicDir, 'contact', 'index.html'))).to.be.true;
});

it('should have the correct content for the contact page', function() {
const h3Tags = dom.window.document.querySelectorAll('h3');
const pTags = dom.window.document.querySelectorAll('p');

expect(h3Tags.length).to.equal(1);
expect(h3Tags[0].textContent).to.equal('Contact Page');

expect(pTags.length).to.equal(1);
expect(pTags[0].textContent).to.equal('Thanks for contacting us.');
});
});
});

after(function() {
setup.teardownTestBed();
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en" prefix="og:http://ogp.me/ns#">

<body>
<h1>Hello from about.html</h1>
<p>Lorum Ipsum</p>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Contact Page

Thanks for contacting us.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en" prefix="og:http://ogp.me/ns#">

<body>
<h1>Hello from the home page!!!!</h1>
</body>

</html>

0 comments on commit 08b710b

Please sign in to comment.