RSS/ATOM Feed added, Prism changes, and global Copyright notice. (#94)

* Add Reason support to Prism.js

* Add XML/ATOM feed. Generates for both localhost and build script. Adds meta links to feeds to all html files.

* Updated /core/Footer.js to pull from siteConfig
This commit is contained in:
Eric Nakagawa 2017-09-27 12:49:09 -07:00 committed by Joel Marcey
parent 92ce92ee59
commit dc835770a0
15 changed files with 285 additions and 39 deletions

111
lib/server/feed.js Normal file
View file

@ -0,0 +1,111 @@
/**
* Copyright (c) 2017-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.
*/
const fs = require("fs-extra");
const path = require("path");
const os = require("os");
const Feed = require("feed");
const chalk = require("chalk");
const CWD = process.cwd();
const siteConfig = require(CWD + "/siteConfig.js");
const blogFolder = path.resolve("../blog/");
const blogRootURL = siteConfig.url + "/blog";
const jestImage = siteConfig.url + siteConfig.headerIcon;
/****************************************************************************/
let readMetadata;
let Metadata;
function reloadMetadata() {
removeFromCache("./readMetadata.js");
readMetadata = require("./readMetadata.js");
readMetadata.generateDocsMetadata();
removeFromCache("../core/metadata.js");
Metadata = require("../core/metadata.js");
}
/****************************************************************************/
// remove a module and child modules from require cache, so server does not have
// to be restarted
const removeFromCache = moduleName => {
let mod = require.resolve(moduleName);
if (mod && (mod = require.cache[mod])) {
(function traverse(mod) {
mod.children.forEach(child => {
traverse(child);
});
delete require.cache[mod.id];
})(mod);
}
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
if (cacheKey.indexOf(moduleName) > 0) {
delete module.constructor._pathCache[cacheKey];
}
});
};
reloadMetadata();
module.exports = function(type) {
console.log("feed.js triggered...");
type = type || "rss";
removeFromCache(CWD + "/siteConfig.js");
if (fs.existsSync(__dirname + "/../core/MetadataBlog.js")) {
removeFromCache("../core/MetadataBlog.js");
fs.removeSync(__dirname + "/../core/MetadataBlog.js");
}
readMetadata.generateBlogMetadata();
const MetadataBlog = require("../core/MetadataBlog.js");
const feed = new Feed({
title: siteConfig.title + " Blog",
description:
"The best place to stay up-to-date with the latest " +
siteConfig.title +
" news and events.",
id: blogRootURL,
link: blogRootURL,
image: jestImage,
copyright: siteConfig.copyright,
updated: new Date(MetadataBlog[0].date)
});
MetadataBlog.forEach(post => {
const url = blogRootURL + "/" + post.path;
let content = "";
let contentArr = post.content.split("<!--truncate-->");
if (contentArr.length > 0) {
content = contentArr[0];
}
content = content.trim().substring(0, 250);
feed.addItem({
title: post.title,
link: url,
author: [
{
name: post.author,
link: post.authorURL
}
],
date: new Date(post.date),
description: content
});
});
return type === "rss" ? feed.rss2() : feed.atom1();
};

View file

@ -23,6 +23,8 @@ function execute() {
const translate = require("./translate.js");
const versionFallback = require("./versionFallback.js");
const feed = require("./feed.js");
const ENABLE_TRANSLATION = fs.existsSync(CWD + "/languages.js");
const ENABLE_VERSIONING = fs.existsSync(CWD + "/versions.json");
@ -174,7 +176,7 @@ function execute() {
"](" + link
);
});
// replace any relative links to static assets to absolute links
rawContent = rawContent.replace(
/\]\(assets\//g,
@ -225,11 +227,11 @@ function execute() {
.replace(/\./g, "-")
.replace(/\-md$/, ".html");
const result = readMetadata.extractMetadata(
fs.readFileSync(file, { encoding: "utf8" })
fs.readFileSync(file, {encoding: "utf8"})
);
const rawContent = result.rawContent;
const metadata = Object.assign(
{ path: filePath, content: rawContent },
{path: filePath, content: rawContent},
result.metadata
);
metadata.id = metadata.title;
@ -255,7 +257,7 @@ function execute() {
const perPage = 10;
for (let page = 0; page < Math.ceil(MetadataBlog.length / perPage); page++) {
let language = "en";
const metadata = { page: page, perPage: perPage };
const metadata = {page: page, perPage: perPage};
const blogPageComp = (
<BlogPageLayout
metadata={metadata}
@ -274,6 +276,15 @@ function execute() {
"/index.html";
writeFileAndCreateFolder(targetFile, str);
}
// create rss files for all blog pages, if there are any blog files
if (MetadataBlog.length > 0) {
let targetFile =
CWD + "/build/" + siteConfig.projectName + "/blog/" + "feed.xml";
writeFileAndCreateFolder(targetFile, feed());
targetFile =
CWD + "/build/" + siteConfig.projectName + "/blog/" + "atom.xml";
writeFileAndCreateFolder(targetFile, feed("atom"));
}
// copy blog assets if they exist
if (fs.existsSync(CWD + "/blog/assets")) {
@ -375,7 +386,7 @@ function execute() {
// copy and compile a page for each enabled language from the English file
for (let i = 0; i < enabledLanguages.length; i++) {
let language = enabledLanguages[i];
// skip conversion from english file if a file exists for this language
// skip conversion from english file if a file exists for this language
if (
language !== "en" &&
fs.existsSync(file.replace("/en/", "/" + language + "/"))

View file

@ -92,7 +92,7 @@ function extractMetadata(content) {
const both = splitHeader(content);
// if no content returned, then that means there was no header, and both.header is the content
if (!both.content) {
return { metadata, rawContent: both.header };
return {metadata, rawContent: both.header};
}
const lines = both.header.split("\n");
for (let i = 0; i < lines.length - 1; ++i) {
@ -104,7 +104,7 @@ function extractMetadata(content) {
} catch (e) {}
metadata[key] = value;
}
return { metadata, rawContent: both.content };
return {metadata, rawContent: both.content};
}
// process the metadata for a document found in the docs folder
@ -173,7 +173,7 @@ function processMetadata(file) {
}
}
return { metadata, rawContent: rawContent };
return {metadata, rawContent: rawContent};
}
// process metadata for all docs and save into core/metadata.js
@ -295,13 +295,26 @@ function generateBlogMetadata() {
.replace("-", "/")
.replace(/\./g, "-")
.replace(/\-md$/, ".html");
const result = extractMetadata(fs.readFileSync(file, { encoding: "utf8" }));
const result = extractMetadata(fs.readFileSync(file, {encoding: "utf8"}));
const rawContent = result.rawContent;
const metadata = Object.assign(
{ path: filePath, content: rawContent },
{path: filePath, content: rawContent},
result.metadata
);
metadata.id = metadata.title;
// Extract, YYYY, MM, DD from the file name
let filePathDateArr = path.basename(file).toString().split("-");
metadata.date = new Date(
filePathDateArr[0] +
"-" +
filePathDateArr[1] +
"-" +
filePathDateArr[2] +
"T06:00:00.000Z"
);
metadatas.push(metadata);
});

View file

@ -23,6 +23,8 @@ function execute(port) {
const translate = require("./translate.js");
const versionFallback = require("./versionFallback");
const feed = require("./feed.js");
const CWD = process.cwd();
const ENABLE_TRANSLATION = fs.existsSync(CWD + "/languages.js");
const ENABLE_VERSIONING = fs.existsSync(CWD + "/versions.json");
@ -116,7 +118,7 @@ function execute(port) {
const metadata = Metadata[id];
links[metadata.permalink] = id;
});
// mdToHtml is a map from a markdown file name to its html link, used to
// change relative markdown links that work on GitHub into actual site links
const mdToHtml = {};
@ -219,6 +221,16 @@ function execute(port) {
res.send(renderToStaticMarkup(docComp));
});
app.get(/blog\/.*xml$/, (req, res) => {
res.set("Content-Type", "application/rss+xml");
let parts = req.path.toString().split("blog/");
if (parts[1].toLowerCase() == "atom.xml") {
res.send(feed("atom"));
return;
}
res.send(feed("rss"));
});
// handle all requests for blog pages and posts
app.get(/blog\/.*html$/, (req, res) => {
removeFromCache(CWD + "/siteConfig.js");
@ -242,7 +254,7 @@ function execute(port) {
page++
) {
let language = "en";
const metadata = { page: page, perPage: perPage };
const metadata = {page: page, perPage: perPage};
const blogPageComp = (
<BlogPageLayout
metadata={metadata}
@ -276,7 +288,7 @@ function execute(port) {
file = CWD + "/blog/" + file;
const result = readMetadata.extractMetadata(
fs.readFileSync(file, { encoding: "utf8" })
fs.readFileSync(file, {encoding: "utf8"})
);
let rawContent = result.rawContent;
rawContent = rawContent.replace(
@ -284,7 +296,7 @@ function execute(port) {
"](" + siteConfig.baseUrl + "blog/assets/"
);
const metadata = Object.assign(
{ path: req.path.toString().split("blog/")[1], content: rawContent },
{path: req.path.toString().split("blog/")[1], content: rawContent},
result.metadata
);
metadata.id = metadata.title;
@ -323,7 +335,7 @@ function execute(port) {
))
)
) {
res.send(fs.readFileSync(htmlFile, { encoding: "utf8" }));
res.send(fs.readFileSync(htmlFile, {encoding: "utf8"}));
return;
}
@ -350,7 +362,7 @@ function execute(port) {
englishFile = englishFile.replace("/" + language + "/", "/en/");
}
// check for: a file for the page, an english file for page with unspecified language,
// check for: a file for the page, an english file for page with unspecified language,
// english file for the page
if (
fs.existsSync(userFile) ||
@ -399,7 +411,7 @@ function execute(port) {
__dirname +
"/../static/" +
req.path.toString().replace(siteConfig.baseUrl, "/");
let cssContent = fs.readFileSync(mainCssPath, { encoding: "utf8" });
let cssContent = fs.readFileSync(mainCssPath, {encoding: "utf8"});
let files = glob.sync(CWD + "/static/**/*.css");
@ -408,7 +420,7 @@ function execute(port) {
return;
}
cssContent =
cssContent + "\n" + fs.readFileSync(file, { encoding: "utf8" });
cssContent + "\n" + fs.readFileSync(file, {encoding: "utf8"});
});
if (