|
| 1 | +/** |
| 2 | + * Copyright (c) 2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +var fileWalker = require('./fileWalker'); |
| 10 | +var fs = require('fs'); |
| 11 | +var path = require('path'); |
| 12 | +var writer = require('./writer'); |
| 13 | +var yaml = require('js-yaml'); |
| 14 | +import { endsWith } from './util'; |
| 15 | + |
| 16 | +exports.readSite = readSite; |
| 17 | +exports.buildSite = buildSite; |
| 18 | + |
| 19 | +async function readSite(siteRoot) { |
| 20 | + var site = { |
| 21 | + root: path.resolve(siteRoot), |
| 22 | + files: [] |
| 23 | + }; |
| 24 | + |
| 25 | + await fileWalker(site.root, (absPath, stat) => { |
| 26 | + var relPath = path.relative(site.root, absPath); |
| 27 | + return readFileData(absPath, relPath, stat).then(data => { |
| 28 | + data = normalizeData(data); |
| 29 | + var dirName = path.dirname(relPath); |
| 30 | + var dirs = dirName === '.' ? [] : dirName.split(path.sep); |
| 31 | + // TODO: throw if a dirname is only a number or is called "length" |
| 32 | + // TODO: there must be a better data structure that doesn't hack Array |
| 33 | + var files = site.files; |
| 34 | + files.push(data); |
| 35 | + for (var i = 0; i < dirs.length; i++) { |
| 36 | + files = files[dirs[i]] || (files[dirs[i]] = []); |
| 37 | + files.push(data); |
| 38 | + } |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + return site; |
| 43 | +} |
| 44 | + |
| 45 | +function buildSite(buildRoot, site) { |
| 46 | + return Promise.all(site.files.map(file => { |
| 47 | + return writer(buildRoot, file, site); |
| 48 | + })); |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +var PAGEISH = [ '.html.js', '.md', '.markdown' ]; |
| 54 | + |
| 55 | +function isPageish(filePath) { |
| 56 | + for (var i = 0; i < PAGEISH.length; i++) { |
| 57 | + if (endsWith(filePath, PAGEISH[i])) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + } |
| 61 | + return false; |
| 62 | +} |
| 63 | + |
| 64 | +var FRONT_MATTER_RX = |
| 65 | + /^(---\s*\n(?:(?:[^\n]*\n)(?!---|\.\.\.))*[^\n]*\n)(?:---|\.\.\.)?(?:\s*\n)*/; |
| 66 | + |
| 67 | +// Given some file information produce a data structure that can be used. |
| 68 | +// If a file is page-like, front-matter will be looked for. |
| 69 | +function readFileData(absPath, relPath, stat) { |
| 70 | + if (stat.size > 100000 || !isPageish(relPath)) { |
| 71 | + return Promise.resolve({ absPath, relPath, stat }); |
| 72 | + } |
| 73 | + return readFile(absPath).then(content => { |
| 74 | + var frontMatter = FRONT_MATTER_RX.exec(content); |
| 75 | + if (!frontMatter) { |
| 76 | + return { absPath, relPath, stat, content }; |
| 77 | + } |
| 78 | + return { |
| 79 | + ...yaml.load(frontMatter[1]), |
| 80 | + absPath, |
| 81 | + relPath, |
| 82 | + stat, |
| 83 | + content: content.slice(frontMatter[0].length) |
| 84 | + }; |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +// Normalize the file data |
| 89 | +function normalizeData(file) { |
| 90 | + file.isPage = file.content && isPageish(file.relPath); |
| 91 | + file.url = urlToFile(file); |
| 92 | + file.date = file.date ? |
| 93 | + Date.parse(file.date) : |
| 94 | + (file.stat.birthtime || file.stat.ctime); |
| 95 | + return file; |
| 96 | +} |
| 97 | + |
| 98 | +// The URL with a leading slash to a file. |
| 99 | +function urlToFile(file) { |
| 100 | + // Determine full url from permalink or the file path. |
| 101 | + var url; |
| 102 | + if (file.permalink) { |
| 103 | + url = file.permalink[0] === '/' ? |
| 104 | + file.permalink : |
| 105 | + '/' + path.join(path.dirname(file.relPath), file.permalink); |
| 106 | + // Ext-less permalinks should have trailing slashes |
| 107 | + if (!endsWith(url, '/') && path.extname(url) === '') { |
| 108 | + url += '/'; |
| 109 | + } |
| 110 | + } else { |
| 111 | + url = '/' + file.relPath; |
| 112 | + for (var i = 0; i < PAGEISH.length; i++) { |
| 113 | + if (endsWith(url, PAGEISH[i])) { |
| 114 | + url = url.slice(0, -PAGEISH[i].length) + '.html'; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Assume index.html stands for the parent directory |
| 120 | + if (path.basename(url) === 'index.html') { |
| 121 | + url = path.dirname(url); |
| 122 | + if (!endsWith(url, '/')) { |
| 123 | + url += '/'; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return url; |
| 128 | +} |
| 129 | + |
| 130 | +// Simple Promise wrapper around fs.readFile |
| 131 | +function readFile(filePath) { |
| 132 | + return new Promise((resolve, reject) => { |
| 133 | + fs.readFile(filePath, 'utf8', (err, content) => { |
| 134 | + if (err) { |
| 135 | + reject(err); |
| 136 | + } else { |
| 137 | + resolve(content); |
| 138 | + } |
| 139 | + }); |
| 140 | + }); |
| 141 | +} |
0 commit comments