var Marked = require("marked")
  , Cache = require("../common/cache")
  ;

var SitePage = module.exports = function (lien) {

    var path = lien.pathName.replace(/\/$/g, "") || "/";

    Cache.page(function (err, pages) {

        if (err) {
            Bloggify.log(err, "error");
            return lien.end(500);
        }

        Bloggify.pages.findOne({path: path}, function (err, page) {

            if (err) {
                Bloggify.log(err, "error");
                return lien.end(500);
            }

            if (!page) { return lien.end(404); }
            Cache.file(Bloggify.config.pages + "/" + page.slug + ".md", {markdown: true}, function (err, content) {
                if (err) { return lien.end(404); }
                lien.end(Bloggify.theme.render({
                    type: "sitePage"
                  , lien: lien
                  , config: Bloggify.config
                  , data: {
                        content: content
                      , menu: {
                            pages: pages
                          , active: page
                        }
                      , title: page.label
                    }
                }));
            });
        });
    });
};

var Marked = require("marked")
  , Cache = require("../common/cache")
  ;

var SitePage = module.exports = function (lien) {

    var path = lien.pathName.replace(/\/$/g, "") || "/";

    Cache.page(function (err, pages) {

        if (err) {
            Bloggify.log(err, "error");
            return lien.end(500);
        }

        Cache.page({path: path}, { markdown: true }, function (err, page) {

            if (err) {
                Bloggify.log(err, "error");
                return lien.end(500);
            }

            if (!page.length) { return lien.end(404); }
            page = page[0];

            lien.end(Bloggify.theme.render({
                type: "sitePage"
              , lien: lien
              , config: Bloggify.config
              , data: {
                    content: page.content
                  , menu: {
                        pages: pages
                      , active: page
                    }
                  , title: page.label
                }
            }));
        });
    });
};

Use page cache to fetch pages