2015-03-23 17:55:49 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2016-03-24 21:10:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-02-12 04:26:43 +00:00
|
|
|
var fs = require('fs')
|
|
|
|
var glob = require('glob');
|
|
|
|
var mkdirp = require('mkdirp');
|
|
|
|
var optimist = require('optimist');
|
|
|
|
var path = require('path');
|
2016-11-02 12:31:52 +00:00
|
|
|
var removeMd = require('remove-markdown');
|
2015-02-12 22:43:41 +00:00
|
|
|
var extractDocs = require('./extractDocs');
|
2017-02-07 02:16:52 +00:00
|
|
|
var cache = require('memory-cache');
|
2015-02-12 04:26:43 +00:00
|
|
|
var argv = optimist.argv;
|
|
|
|
|
|
|
|
function splitHeader(content) {
|
2016-01-02 08:56:32 +00:00
|
|
|
var lines = content.split(/\r?\n/);
|
2015-02-12 04:26:43 +00:00
|
|
|
for (var i = 1; i < lines.length - 1; ++i) {
|
|
|
|
if (lines[i] === '---') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
2015-02-19 00:39:28 +00:00
|
|
|
header: i < lines.length - 1 ?
|
|
|
|
lines.slice(1, i + 1).join('\n') : null,
|
2015-02-12 04:26:43 +00:00
|
|
|
content: lines.slice(i + 1).join('\n')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-24 21:10:15 +00:00
|
|
|
function rmFile(file) {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(file);
|
|
|
|
} catch(e) {
|
|
|
|
/* seriously, unlink throws when the file doesn't exist :( */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 04:26:43 +00:00
|
|
|
function backtickify(str) {
|
2015-04-08 18:06:56 +00:00
|
|
|
var escaped = '`' + str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/{/g, '\\{') + '`';
|
2015-02-12 04:26:43 +00:00
|
|
|
// Replace require( with require\( so node-haste doesn't replace example
|
|
|
|
// require calls in the docs
|
|
|
|
return escaped.replace(/require\(/g, 'require\\(');
|
|
|
|
}
|
|
|
|
|
2016-03-24 21:10:15 +00:00
|
|
|
function writeFileAndCreateFolder(file, content) {
|
|
|
|
mkdirp.sync(file.replace(new RegExp('/[^/]*$'), ''));
|
|
|
|
fs.writeFileSync(file, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract markdown metadata header
|
|
|
|
function extractMetadata(content) {
|
|
|
|
var metadata = {};
|
|
|
|
var both = splitHeader(content);
|
|
|
|
var lines = both.header.split('\n');
|
|
|
|
for (var i = 0; i < lines.length - 1; ++i) {
|
|
|
|
var keyvalue = lines[i].split(':');
|
|
|
|
var key = keyvalue[0].trim();
|
|
|
|
var value = keyvalue.slice(1).join(':').trim();
|
|
|
|
// Handle the case where you have "Community #10"
|
|
|
|
try { value = JSON.parse(value); } catch(e) { }
|
|
|
|
metadata[key] = value;
|
|
|
|
}
|
|
|
|
return {metadata: metadata, rawContent: both.content};
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildFile(layout, metadata, rawContent) {
|
|
|
|
return [
|
|
|
|
'/**',
|
|
|
|
' * @generated',
|
|
|
|
' */',
|
|
|
|
'var React = require("React");',
|
|
|
|
'var Layout = require("' + layout + '");',
|
|
|
|
rawContent && 'var content = ' + backtickify(rawContent) + ';',
|
|
|
|
'var Post = React.createClass({',
|
|
|
|
rawContent && ' statics: { content: content },',
|
|
|
|
' render: function() {',
|
|
|
|
' return (',
|
|
|
|
' <Layout metadata={' + JSON.stringify(metadata) + '}>',
|
|
|
|
rawContent && ' {content}',
|
|
|
|
' </Layout>',
|
|
|
|
' );',
|
|
|
|
' }',
|
|
|
|
'});',
|
|
|
|
'module.exports = Post;'
|
|
|
|
].filter(e => e).join('\n');
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:16:52 +00:00
|
|
|
function execute(options) {
|
2017-02-07 22:29:29 +00:00
|
|
|
if (options === undefined) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2016-03-24 21:10:15 +00:00
|
|
|
var DOCS_MD_DIR = '../docs/';
|
|
|
|
var BLOG_MD_DIR = '../blog/';
|
2015-02-12 04:26:43 +00:00
|
|
|
|
2016-03-24 21:10:15 +00:00
|
|
|
glob.sync('src/react-native/docs/*.*').forEach(rmFile);
|
|
|
|
glob.sync('src/react-native/blog/*.*').forEach(rmFile);
|
2016-03-25 17:45:24 +00:00
|
|
|
glob.sync('../blog/img/*.*').forEach(file => {
|
|
|
|
writeFileAndCreateFolder(
|
|
|
|
'src/react-native/blog/img/' + path.basename(file),
|
|
|
|
fs.readFileSync(file)
|
|
|
|
);
|
|
|
|
});
|
2015-02-12 04:26:43 +00:00
|
|
|
|
|
|
|
var metadatas = {
|
|
|
|
files: [],
|
|
|
|
};
|
|
|
|
|
2016-01-21 07:46:25 +00:00
|
|
|
function handleMarkdown(content, filename) {
|
2016-03-24 21:10:15 +00:00
|
|
|
if (content.slice(0, 3) !== '---') {
|
2015-02-19 00:39:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-24 21:10:15 +00:00
|
|
|
|
|
|
|
const res = extractMetadata(content);
|
|
|
|
const metadata = res.metadata;
|
|
|
|
const rawContent = res.rawContent;
|
2015-05-07 23:05:30 +00:00
|
|
|
|
|
|
|
if (metadata.sidebar !== false) {
|
|
|
|
metadatas.files.push(metadata);
|
|
|
|
}
|
2015-02-12 22:43:41 +00:00
|
|
|
|
|
|
|
if (metadata.permalink.match(/^https?:/)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-21 07:46:25 +00:00
|
|
|
metadata.filename = filename;
|
|
|
|
|
2015-02-12 22:43:41 +00:00
|
|
|
// Create a dummy .js version that just calls the associated layout
|
|
|
|
var layout = metadata.layout[0].toUpperCase() + metadata.layout.substr(1) + 'Layout';
|
|
|
|
|
2016-03-24 21:10:15 +00:00
|
|
|
writeFileAndCreateFolder(
|
|
|
|
'src/react-native/' + metadata.permalink.replace(/\.html$/, '.js'),
|
|
|
|
buildFile(layout, metadata, rawContent)
|
2015-02-12 04:26:43 +00:00
|
|
|
);
|
2015-02-12 22:43:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 02:16:52 +00:00
|
|
|
if (options.extractDocs) {
|
|
|
|
// Rendering docs can take up to 8 seconds. We wait until /docs/ are
|
|
|
|
// requested before doing so, then we store the results in memory to
|
|
|
|
// speed up subsequent requests.
|
|
|
|
var extractedDocs = cache.get('extractedDocs');
|
|
|
|
if (!extractedDocs) {
|
|
|
|
extractedDocs = extractDocs();
|
|
|
|
cache.put('extractedDocs', extractedDocs);
|
2015-02-12 22:43:41 +00:00
|
|
|
}
|
2017-02-07 02:16:52 +00:00
|
|
|
extractedDocs.forEach(function(content) {
|
|
|
|
handleMarkdown(content, null);
|
|
|
|
});
|
2015-02-12 22:43:41 +00:00
|
|
|
|
2017-02-07 02:16:52 +00:00
|
|
|
var files = glob.sync(DOCS_MD_DIR + '**/*.*');
|
|
|
|
files.forEach(function(file) {
|
|
|
|
var extension = path.extname(file);
|
|
|
|
if (extension === '.md' || extension === '.markdown') {
|
|
|
|
var content = fs.readFileSync(file, {encoding: 'utf8'});
|
|
|
|
handleMarkdown(content, path.basename(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extension === '.json') {
|
|
|
|
var content = fs.readFileSync(file, {encoding: 'utf8'});
|
|
|
|
metadatas[path.basename(file, '.json')] = JSON.parse(content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-02-12 22:43:41 +00:00
|
|
|
|
2016-02-11 14:16:34 +00:00
|
|
|
// we need to pass globals for the components to be configurable
|
|
|
|
// metadata is generated in this process which has access to process.env
|
|
|
|
// but the web pages are generated in a sandbox context and have only access to CommonJS module files
|
|
|
|
metadatas.config = Object.create(null);
|
|
|
|
Object
|
|
|
|
.keys(process.env)
|
|
|
|
.filter(key => key.startsWith('RN_'))
|
|
|
|
.forEach((key) => {
|
|
|
|
metadatas.config[key] = process.env[key];
|
|
|
|
});
|
|
|
|
|
2015-02-12 22:43:41 +00:00
|
|
|
fs.writeFileSync(
|
|
|
|
'core/metadata.js',
|
|
|
|
'/**\n' +
|
|
|
|
' * @generated\n' +
|
|
|
|
' * @providesModule Metadata\n' +
|
|
|
|
' */\n' +
|
|
|
|
'module.exports = ' + JSON.stringify(metadatas, null, 2) + ';'
|
|
|
|
);
|
2016-03-24 21:10:15 +00:00
|
|
|
|
|
|
|
|
2016-03-25 17:45:24 +00:00
|
|
|
files = glob.sync(BLOG_MD_DIR + '**/*.md');
|
2016-03-24 21:10:15 +00:00
|
|
|
const metadatasBlog = {
|
|
|
|
files: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
files.sort().reverse().forEach(file => {
|
|
|
|
// Transform
|
|
|
|
// 2015-08-13-blog-post-name-0.5.md
|
|
|
|
// into
|
|
|
|
// 2015/08/13/blog-post-name-0-5.html
|
|
|
|
var filePath = path.basename(file)
|
|
|
|
.replace('-', '/')
|
|
|
|
.replace('-', '/')
|
|
|
|
.replace('-', '/')
|
|
|
|
// react-middleware is broken with files that contains multiple . like react-0.14.js
|
|
|
|
.replace(/\./g, '-')
|
|
|
|
.replace(/\-md$/, '.html');
|
|
|
|
|
2016-10-28 20:27:33 +00:00
|
|
|
// Extract 2015-08-13 from 2015/08/13/blog-post-name-0-5.html
|
|
|
|
var match = filePath.match(/([0-9]+)\/([0-9]+)\/([0-9]+)/);
|
|
|
|
var year = match[1];
|
|
|
|
var month = match[2];
|
|
|
|
var day = match[3];
|
|
|
|
var publishedAt = year + '-' + month + '-' + day;
|
|
|
|
|
2016-03-24 21:10:15 +00:00
|
|
|
var res = extractMetadata(fs.readFileSync(file, {encoding: 'utf8'}));
|
|
|
|
var rawContent = res.rawContent;
|
2016-11-02 12:31:52 +00:00
|
|
|
var excerpt = removeMd(rawContent).trim().split('\n')[0];
|
|
|
|
var metadata = Object.assign({path: filePath, content: rawContent, publishedAt: publishedAt, excerpt: excerpt}, res.metadata);
|
2016-03-24 21:10:15 +00:00
|
|
|
|
|
|
|
metadatasBlog.files.push(metadata);
|
|
|
|
|
|
|
|
writeFileAndCreateFolder(
|
|
|
|
'src/react-native/blog/' + filePath.replace(/\.html$/, '.js'),
|
|
|
|
buildFile('BlogPostLayout', metadata, rawContent)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-11-02 12:31:52 +00:00
|
|
|
var perPage = 15;
|
2016-03-24 21:10:15 +00:00
|
|
|
for (var page = 0; page < Math.ceil(metadatasBlog.files.length / perPage); ++page) {
|
|
|
|
writeFileAndCreateFolder(
|
|
|
|
'src/react-native/blog' + (page > 0 ? '/page' + (page + 1) : '') + '/index.js',
|
|
|
|
buildFile('BlogPageLayout', { page: page, perPage: perPage })
|
|
|
|
);
|
|
|
|
}
|
|
|
|
fs.writeFileSync(
|
|
|
|
'core/metadata-blog.js',
|
|
|
|
'/**\n' +
|
|
|
|
' * @generated\n' +
|
|
|
|
' * @providesModule MetadataBlog\n' +
|
|
|
|
' */\n' +
|
|
|
|
'module.exports = ' + JSON.stringify(metadatasBlog, null, 2) + ';'
|
|
|
|
);
|
2016-10-28 20:27:33 +00:00
|
|
|
fs.writeFileSync(
|
|
|
|
'server/metadata-blog.json',
|
|
|
|
JSON.stringify(metadatasBlog, null, 2)
|
|
|
|
);
|
|
|
|
|
2015-02-12 04:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.convert) {
|
2015-02-12 22:43:41 +00:00
|
|
|
console.log('convert!');
|
2015-02-12 04:26:43 +00:00
|
|
|
execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = execute;
|