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

View file

@ -57,6 +57,8 @@ headerLinks: [
- `prismColor` is the color used in the background of syntax highlighting for code in documentation. It is recommended to be the same color as `primaryColor` in `rgba` form with an alpha value of `0.03`. Other fields can be added
- Custom color configurations can also be added. For example, if user styles are added with colors specified as `$myColor`, then adding a `myColor` field to `colors` will allow you to easily configure this color.
`copyright` - The copyright string at footer of site and within feed
### Optional Fields
`editUrl` - url for editing docs, usage example: `editUrl + 'en/doc1.md'`. If this field is omitted, there will be no "Edit this Doc" button for each document.

View file

@ -67,3 +67,9 @@ Not this.
Or this.
```
## RSS Feed
Docusaurus provides a simple RSS feed for your blog posts. Both RSS and Atom feed formats are supported. This data is automatically to your website page's HTML <HEAD> tag.
A summary of the post's text is provided in the RSS feed up to the `<!--truncate-->`. If no `<!--truncate-->` tag is found, then all text up 250 chacters are used.

View file

@ -24,10 +24,10 @@ const siteConfig = {
baseUrl: "/test-site/" /* base url for your project */,
projectName: "test-site",
headerLinks: [
{ doc: "doc1", label: "Docs" },
{ doc: "doc4", label: "API" },
{ page: "help", label: "Help" },
{ blog: true, label: "Blog" }
{doc: "doc1", label: "Docs"},
{doc: "doc4", label: "API"},
{page: "help", label: "Help"},
{blog: true, label: "Blog"}
],
users,
/* path to images for header/footer */
@ -40,7 +40,12 @@ const siteConfig = {
secondaryColor: "#205C3B",
prismColor:
"rgba(46, 133, 85, 0.03)" /* primaryColor in rgba form, with 0.03 alpha */
}
},
// This copyright info is used in /core/Footer.js and blog rss/atom feeds.
copyright:
"Copyright © " +
new Date().getFullYear() +
" Your Name or Your Company Name"
};
module.exports = siteConfig;

View file

@ -12,37 +12,58 @@ const React = require("react");
// html head for each page
class Head extends React.Component {
render() {
let links = this.props.config.headerLinks;
let hasBlog = false;
links.map(link => {
if (link.blog) hasBlog = true;
});
return (
<head>
<meta charSet="utf-8" />
<meta httpEquiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<title>
{this.props.title}
</title>
<title>{this.props.title}</title>
<meta name="viewport" content="width=device-width" />
<meta property="og:title" content={this.props.title} />
<meta property="og:type" content="website" />
<meta property="og:url" content={this.props.url} />
<meta property="og:description" content={this.props.description} />
<meta name="robots" content="noindex" />
{this.props.config.ogImage &&
{this.props.config.ogImage && (
<meta
property="og:image"
content={this.props.config.baseUrl + this.props.config.ogImage}
/>}
/>
)}
<link
rel="shortcut icon"
href={this.props.config.baseUrl + this.props.config.favicon}
/>
{this.props.config.algolia &&
{this.props.config.algolia && (
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.css"
/>}
/>
)}
<link
rel="stylesheet"
href={this.props.config.baseUrl + "css/main.css"}
/>
{hasBlog && (
<link
rel="alternate"
type="application/atom+xml"
href={this.props.config.url + "/blog/atom.xml"}
title={this.props.config.title + " Blog ATOM Feed"}
/>
)}{" "}
{hasBlog && (
<link
rel="alternate"
type="application/rss+xml"
href={this.props.config.url + "/blog/feed.xml"}
title={this.props.config.title + " Blog RSS Feed"}
/>
)}
<script async defer src="https://buttons.github.io/buttons.js" />
</head>
);

33
lib/generate-feed.js Normal file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env node
/**
* 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.
*/
require("babel-register")({
babelrc: false,
only: [__dirname, process.cwd() + "/core"],
plugins: [require("./server/translate-plugin.js")],
presets: ["react", "latest"]
});
// initial check that required files are present
const chalk = require("chalk");
const fs = require("fs");
const CWD = process.cwd();
if (!fs.existsSync(CWD + "/siteConfig.js")) {
console.error(
chalk.red("Error: No siteConfig.js file found in website folder!")
);
process.exit(1);
}
// generate rss feed
const feed = require("./server/feed.js");
console.log(feed());

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 (

View file

@ -10,6 +10,7 @@
"commander": "^2.11.0",
"diff": "^3.3.0",
"express": "^4.15.3",
"feed": "^1.1.0",
"fs-extra": "^3.0.1",
"glob": "^7.1.2",
"prettier": "^1.5.3",
@ -25,6 +26,9 @@
"type": "git",
"url": "https://github.com/facebookexperimental/Docusaurus.git"
},
"devDependencies": {
"jest": "^21.2.0"
},
"bin": {
"docusaurus-start": "./lib/start-server.js",
"docusaurus-build": "./lib/build-files.js",
@ -32,6 +36,7 @@
"docusaurus-examples": "./lib/copy-examples.js",
"docusaurus-write-translations": "./lib/write-translations.js",
"docusaurus-version": "./lib/version.js",
"docusaurus-rename-version": "./lib/rename-version.js"
"docusaurus-rename-version": "./lib/rename-version.js",
"docusaurus-feed": "./lib/generate-feed.js"
}
}

View file

@ -0,0 +1,11 @@
---
title: Adding RSS Support
author: Eric Nakagawa
authorURL: http://twitter.com/ericnakagawa
authorFBID: ericnakagawa
---
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
This should be truncated.
<!--truncate-->
This line should never render in XML.

View file

@ -0,0 +1,12 @@
---
title: Adding RSS Support
author: Eric Nakagawa
authorURL: http://twitter.com/ericnakagawa
authorFBID: ericnakagawa
---
This is a test post.
<!--truncate-->
A whole bunch of other information.

View file

@ -11,7 +11,6 @@ const React = require("react");
class Footer extends React.Component {
render() {
const currentYear = new Date().getFullYear();
return (
<footer className="nav-footer" id="footer">
<section className="sitemap">
@ -74,7 +73,9 @@ class Footer extends React.Component {
/>
</a>
<section className="copyright">
Copyright &copy; {currentYear} Facebook Inc.
{this.props.config.copyright && (
<span>{this.props.config.copyright}</span>
)}
</section>
</footer>
);

View file

@ -9,5 +9,8 @@
"rename-version": "../lib/rename-version.js",
"crowdin-upload": "crowdin-cli --config ../crowdin.yaml upload sources --auto-update -b master",
"crowdin-download": "crowdin-cli --config ../crowdin.yaml download -b master"
},
"dependencies": {
"docusaurus": "^1.0.0-alpha.35"
}
}

View file

@ -40,10 +40,10 @@ const siteConfig = {
editUrl:
"https://github.com/facebookexperimental/docusaurus/edit/master/docs/",
headerLinks: [
{ doc: "installation", label: "Docs" },
{ page: "help", label: "Help" },
{ blog: true, label: "Blog" },
{ languages: false },
{doc: "installation", label: "Docs"},
{page: "help", label: "Help"},
{blog: true, label: "Blog"},
{languages: false},
{
href: "https://github.com/facebookexperimental/docusaurus",
label: "GitHub"
@ -53,7 +53,6 @@ const siteConfig = {
footerIcon: "img/docusaurus_monochrome.svg",
favicon: "img/docusaurus.ico",
// See https://docusaurus.io/docs/search for more information about Aloglia
// search
algolia: {
apiKey: "3eb9507824b8be89e7a199ecaa1a9d2c",
indexName: "docusaurus"
@ -62,7 +61,8 @@ const siteConfig = {
primaryColor: "#2E8555",
secondaryColor: "#205C3B",
prismColor: "rgba(46, 133, 85, 0.03)"
}
},
copyright: "Copyright © " + new Date().getFullYear() + " Facebook Inc."
};
module.exports = siteConfig;