Skip to content

Commit

Permalink
Merge pull request #469 from Jazmon/master
Browse files Browse the repository at this point in the history
Add support for markdown and html templates
  • Loading branch information
koistya committed Feb 29, 2016
2 parents 0bb004c + 32b7bfd commit b6e088e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"isomorphic-style-loader": "0.0.10",
"jade": "1.11.0",
"jsonwebtoken": "5.7.0",
"markdown-it": "^6.0.0",
"node-fetch": "1.3.3",
"normalize.css": "3.0.3",
"passport": "0.3.2",
Expand Down
65 changes: 55 additions & 10 deletions src/data/queries/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@ import {

import ContentType from '../types/ContentType';

const md = require('markdown-it')();

// A folder with Jade/Markdown/HTML content pages
const CONTENT_DIR = join(__dirname, './content');

// Extract 'front matter' metadata and generate HTML
const parseJade = (path, jadeContent) => {
const fmContent = fm(jadeContent);
const htmlContent = jade.render(fmContent.body);
const parseContent = (path, fileContent, extension) => {
const fmContent = fm(fileContent);
let htmlContent;
switch (extension) {
case '.jade':
htmlContent = jade.render(fmContent.body);
break;
case '.md':
htmlContent = md.render(fmContent.body);
break;
case '.html':
htmlContent = fmContent.body;
break;
default:
return null;
}
return Object.assign({ path, content: htmlContent }, fmContent.attributes);
};

Expand All @@ -35,22 +50,52 @@ const fileExists = filename => new Promise(resolve => {
fs.exists(filename, resolve);
});

async function resolveExtension(path, extension) {
let fileNameBase = join(CONTENT_DIR, `${path === '/' ? '/index' : path}`);
let ext = extension;
if (!ext.startsWith('.')) {
ext = `.${extension}`;
}

let fileName = fileNameBase + ext;

if (!(await fileExists(fileName))) {
fileNameBase = join(CONTENT_DIR, `${path}/index`);
fileName = fileNameBase + ext;
}

if (!(await fileExists(fileName))) {
return { success: false };
}

return { success: true, fileName };
}

async function resolveFileName(path) {
const extensions = ['.jade', '.md', '.html'];

for (const extension of extensions) {
const maybeFileName = await resolveExtension(path, extension);
if (maybeFileName.success) {
return { success: true, fileName: maybeFileName.fileName, extension };
}
}

return { success: false, fileName: null, extension: null };
}

export default {
type: ContentType,
args: {
path: { type: new NonNull(StringType) },
},
async resolve({ request }, { path }) {
let fileName = join(CONTENT_DIR, `${path === '/' ? '/index' : path}.jade`);
if (!(await fileExists(fileName))) {
fileName = join(CONTENT_DIR, `${path}/index.jade`);
}

if (!(await fileExists(fileName))) {
const { success, fileName, extension } = await resolveFileName(path);
if (!success) {
return null;
}

const source = await readFile(fileName, { encoding: 'utf8' });
return parseJade(path, source);
return parseContent(path, source, extension);
},
};

0 comments on commit b6e088e

Please sign in to comment.