Skip to content

Commit

Permalink
Move layouts to assets folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jamos-tay committed Sep 30, 2018
1 parent 750baae commit b3b4329
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 14 deletions.
7 changes: 4 additions & 3 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ function Page(pageConfig) {
this.baseUrlMap = pageConfig.baseUrlMap;
this.content = pageConfig.content || '';
this.faviconUrl = pageConfig.faviconUrl;
this.layout = pageConfig.layout;
this.layoutsAssetPath = pageConfig.layoutsAssetPath;
this.rootPath = pageConfig.rootPath;
this.searchable = pageConfig.searchable;
this.src = pageConfig.src;
this.template = pageConfig.pageTemplate;
this.layout = pageConfig.layout;
this.title = pageConfig.title || '';
this.titlePrefix = pageConfig.titlePrefix;
this.userDefinedVariablesMap = pageConfig.userDefinedVariablesMap;
Expand Down Expand Up @@ -571,8 +572,8 @@ Page.prototype.generate = function (builtFiles) {
* Adds linked layout files to page assets
*/
Page.prototype.addLayoutFiles = function () {
this.asset.layoutScript = path.join(LAYOUT_FOLDER_PATH, this.frontMatter.layout, 'scripts.js');
this.asset.layoutStyle = path.join(LAYOUT_FOLDER_PATH, this.frontMatter.layout, 'styles.css');
this.asset.layoutScript = path.join(this.layoutsAssetPath, this.frontMatter.layout, 'scripts.js');
this.asset.layoutStyle = path.join(this.layoutsAssetPath, this.frontMatter.layout, 'styles.css');
};

/**
Expand Down
17 changes: 17 additions & 0 deletions lib/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ const SITE_NAV_PATH = '_markbind/navigation/site-nav.md';
const LAYOUT_DEFAULT_NAME = 'default';
const LAYOUT_FILES = ['navigation.md', 'head.md', 'footer.md', 'styles.css', 'scripts.js'];
const LAYOUT_FOLDER_PATH = '_markbind/layouts';
const LAYOUT_SITE_FOLDER_NAME = 'layouts';
const USER_VARIABLES_PATH = '_markbind/variables.md';

const SITE_CONFIG_DEFAULT = {
baseUrl: '',
titlePrefix: '',
ignore: [
'_markbind/layouts/*',
'_markbind/logs/*',
'_site/*',
'site.json',
Expand Down Expand Up @@ -304,6 +306,7 @@ Site.prototype.createPage = function (config) {
rootPath: this.rootPath,
searchable: config.searchable,
src: config.pageSrc,
layoutsAssetPath: path.join(TEMPLATE_SITE_ASSET_FOLDER_NAME, LAYOUT_SITE_FOLDER_NAME),
layout: config.layout,
title: config.title || '',
titlePrefix: this.siteConfig.titlePrefix,
Expand Down Expand Up @@ -447,6 +450,7 @@ Site.prototype.generate = function (baseUrl) {
.then(() => this.buildAssets())
.then(() => this.buildSourceFiles())
.then(() => this.copyMarkBindAsset())
.then(() => this.copyLayouts())
.then(() => this.writeSiteData())
.then(() => {
const endTime = new Date();
Expand Down Expand Up @@ -655,6 +659,19 @@ Site.prototype.copyMarkBindAsset = function () {
});
};

/**
* Copies layouts to the assets folder
*/
Site.prototype.copyLayouts = function () {
const siteLayoutPath = path.join(this.rootPath, LAYOUT_FOLDER_PATH);
const layoutsDestPath = path.join(this.siteAssetsDestPath, LAYOUT_SITE_FOLDER_NAME);
return new Promise((resolve, reject) => {
fs.copyAsync(siteLayoutPath, layoutsDestPath)
.then(resolve)
.catch(reject);
});
};

/**
* Writes the site data to a file
*/
Expand Down
4 changes: 2 additions & 2 deletions test/test_site/expected/bugs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="stylesheet" href="..\markbind\css\bootstrap-glyphicons.min.css" >
<link rel="stylesheet" href="..\markbind\css\github.min.css">
<link rel="stylesheet" href="..\markbind\css\markbind.css">
<link rel="stylesheet" href="_markbind\layouts\default\styles.css">
<link rel="stylesheet" href="markbind\layouts\default\styles.css">

<meta name="default-head-bottom">
<link rel="icon" href="/test_site/favicon.png">
Expand Down Expand Up @@ -89,5 +89,5 @@ <h2 id="remove-extra-space-in-links">Remove extra space in links<a class="fa fa-
const baseUrl = '/test_site'
</script>
<script src="..\markbind\js\setup.js"></script>
<script src="_markbind\layouts\default\scripts.js"></script>
<script src="markbind\layouts\default\scripts.js"></script>
</html>
4 changes: 2 additions & 2 deletions test/test_site/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<link rel="stylesheet" href="markbind\css\bootstrap-glyphicons.min.css" >
<link rel="stylesheet" href="markbind\css\github.min.css">
<link rel="stylesheet" href="markbind\css\markbind.css">
<link rel="stylesheet" href="_markbind\layouts\default\styles.css">
<link rel="stylesheet" href="markbind\layouts\default\styles.css">
<link rel="stylesheet" href="markbind\css\site-nav.css">
<!-- Start of bottom level head file content insertion -->
<script src="/test_site/headFiles/customScriptBottom.js"></script>
Expand Down Expand Up @@ -296,5 +296,5 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
const baseUrl = '/test_site'
</script>
<script src="markbind\js\setup.js"></script>
<script src="_markbind\layouts\default\scripts.js"></script>
<script src="markbind\layouts\default\scripts.js"></script>
</html>
5 changes: 5 additions & 0 deletions test/test_site/expected/markbind/layouts/default/footer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<footer>
<div class="text-center">
Default footer
</div>
</footer>
4 changes: 4 additions & 0 deletions test/test_site/expected/markbind/layouts/default/head.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<head-top>
<meta name="default-head-top">
</head-top>
<meta name="default-head-bottom">
Empty file.
5 changes: 5 additions & 0 deletions test/test_site/expected/markbind/layouts/testLayout/footer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<footer>
<div class="text-center">
Layout footer
</div>
</footer>
4 changes: 4 additions & 0 deletions test/test_site/expected/markbind/layouts/testLayout/head.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<head-top>
<meta name="head-top">
</head-top>
<meta name="head-bottom">
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<navigation>
* [Layout Nav]
</navigation>
4 changes: 2 additions & 2 deletions test/test_site/expected/sub_site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="stylesheet" href="..\markbind\css\bootstrap-glyphicons.min.css" >
<link rel="stylesheet" href="..\markbind\css\github.min.css">
<link rel="stylesheet" href="..\markbind\css\markbind.css">
<link rel="stylesheet" href="_markbind\layouts\default\styles.css">
<link rel="stylesheet" href="markbind\layouts\default\styles.css">

<meta name="default-head-bottom">
<link rel="icon" href="/test_site/favicon.png">
Expand Down Expand Up @@ -47,5 +47,5 @@ <h2 id="feature-list">Feature list<a class="fa fa-anchor" href="#feature-list"><
const baseUrl = '/test_site'
</script>
<script src="..\markbind\js\setup.js"></script>
<script src="_markbind\layouts\default\scripts.js"></script>
<script src="markbind\layouts\default\scripts.js"></script>
</html>
4 changes: 2 additions & 2 deletions test/test_site/expected/testLayouts.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="stylesheet" href="markbind\css\bootstrap-glyphicons.min.css" >
<link rel="stylesheet" href="markbind\css\github.min.css">
<link rel="stylesheet" href="markbind\css\markbind.css">
<link rel="stylesheet" href="_markbind\layouts\testLayout\styles.css">
<link rel="stylesheet" href="markbind\layouts\testLayout\styles.css">

<script src="/test_site/headFiles/overwriteLayoutScript.js"></script>
<link rel="icon" href="/test_site/favicon.png">
Expand Down Expand Up @@ -54,5 +54,5 @@ <h1 id="uses-a-layout">Uses a layout<a class="fa fa-anchor" href="#uses-a-layout
const baseUrl = '/test_site'
</script>
<script src="markbind\js\setup.js"></script>
<script src="_markbind\layouts\testLayout\scripts.js"></script>
<script src="markbind\layouts\testLayout\scripts.js"></script>
</html>
4 changes: 2 additions & 2 deletions test/test_site/expected/test_md_fragment.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="stylesheet" href="markbind\css\bootstrap-glyphicons.min.css" >
<link rel="stylesheet" href="markbind\css\github.min.css">
<link rel="stylesheet" href="markbind\css\markbind.css">
<link rel="stylesheet" href="_markbind\layouts\default\styles.css">
<link rel="stylesheet" href="markbind\layouts\default\styles.css">

<meta name="default-head-bottom">
<link rel="icon" href="/test_site/favicon.png">
Expand All @@ -39,5 +39,5 @@ <h1 id="some-heading">Some heading<a class="fa fa-anchor" href="#some-heading"><
const baseUrl = '/test_site'
</script>
<script src="markbind\js\setup.js"></script>
<script src="_markbind\layouts\default\scripts.js"></script>
<script src="markbind\layouts\default\scripts.js"></script>
</html>
1 change: 1 addition & 0 deletions test/test_site/site.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}
],
"ignore": [
"_markbind/layouts/*",
"_markbind/logs/*",
"_site/*",
"site.json",
Expand Down
15 changes: 14 additions & 1 deletion test/unit/Site.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ test('Site Generate builds the correct amount of assets', async () => {
'asset/js/setup.js': '',
'asset/js/vue.min.js': '',
'asset/js/vue-strap.min.js': '',

'inner/_markbind/layouts/default/footer.md': '',
'inner/_markbind/layouts/default/head.md': '',
'inner/_markbind/layouts/default/navigation.md': '',
'inner/_markbind/layouts/default/scripts.js': '',
'inner/_markbind/layouts/default/styles.css': '',
};
fs.vol.fromJSON(json, '');
const site = new Site('inner/', 'inner/_site');
await site.generate();
const paths = Object.keys(fs.vol.toJSON());
const originalNumFiles = Object.keys(json).length;
const expectedNumBuilt = 8;
const expectedNumBuilt = 13;
expect(paths.length).toEqual(originalNumFiles + expectedNumBuilt);

// site
Expand All @@ -65,6 +71,13 @@ test('Site Generate builds the correct amount of assets', async () => {
expect(fs.existsSync(path.resolve('inner/_site/markbind/js/setup.js'))).toEqual(true);
expect(fs.existsSync(path.resolve('inner/_site/markbind/js/vue.min.js'))).toEqual(true);
expect(fs.existsSync(path.resolve('inner/_site/markbind/js/vue-strap.min.js'))).toEqual(true);

// layouts
expect(fs.existsSync(path.resolve('inner/_site/markbind/layouts/default/footer.md'))).toEqual(true);
expect(fs.existsSync(path.resolve('inner/_site/markbind/layouts/default/head.md'))).toEqual(true);
expect(fs.existsSync(path.resolve('inner/_site/markbind/layouts/default/navigation.md'))).toEqual(true);
expect(fs.existsSync(path.resolve('inner/_site/markbind/layouts/default/scripts.js'))).toEqual(true);
expect(fs.existsSync(path.resolve('inner/_site/markbind/layouts/default/styles.css'))).toEqual(true);
});

test('Site Init in existing directory generates correct assets', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/unit/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports.SITE_JSON_DEFAULT = '{\n'
+ ' "baseUrl": "",\n'
+ ' "titlePrefix": "",\n'
+ ' "ignore": [\n'
+ ' "_markbind/layouts/*",\n'
+ ' "_markbind/logs/*",\n'
+ ' "_site/*",\n'
+ ' "site.json",\n'
Expand Down

0 comments on commit b3b4329

Please sign in to comment.