mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
Add Prettier Formatting (#258)
* Add Prettier formatting to source files and example files, and check that Prettier formatting is maintained on PRs * Remove trailing-comma as we are using Node 6 on Circle * Use latest Node 6 LTS version in Circle * Remove unused test
This commit is contained in:
parent
0cead4b6f9
commit
65421db62e
50 changed files with 1376 additions and 1350 deletions
|
@ -7,28 +7,26 @@
|
|||
|
||||
const CWD = process.cwd();
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const glob = require("glob");
|
||||
const chalk = require("chalk");
|
||||
const siteConfig = require(CWD + "/siteConfig.js");
|
||||
const versionFallback = require("./versionFallback.js");
|
||||
const escapeStringRegexp = require("escape-string-regexp");
|
||||
|
||||
const ENABLE_VERSIONING = fs.existsSync(CWD + "/versions.json");
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const glob = require('glob');
|
||||
const chalk = require('chalk');
|
||||
const siteConfig = require(CWD + '/siteConfig.js');
|
||||
const versionFallback = require('./versionFallback.js');
|
||||
const escapeStringRegexp = require('escape-string-regexp');
|
||||
|
||||
const ENABLE_VERSIONING = fs.existsSync(CWD + '/versions.json');
|
||||
|
||||
let languages;
|
||||
if (fs.existsSync(CWD + "/languages.js")) {
|
||||
languages = require(CWD + "/languages.js");
|
||||
if (fs.existsSync(CWD + '/languages.js')) {
|
||||
languages = require(CWD + '/languages.js');
|
||||
} else {
|
||||
languages = [
|
||||
{
|
||||
enabled: true,
|
||||
name: "English",
|
||||
tag: "en"
|
||||
}
|
||||
name: 'English',
|
||||
tag: 'en',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -39,15 +37,13 @@ if (fs.existsSync(CWD + "/languages.js")) {
|
|||
// All .md docs still (currently) must be in one flat directory hierarchy.
|
||||
// e.g., docs/whereDocsReallyExist/*.md (all .md files in this dir)
|
||||
function getDocsPath() {
|
||||
return siteConfig.customDocsPath
|
||||
? siteConfig.customDocsPath
|
||||
: "docs";
|
||||
return siteConfig.customDocsPath ? siteConfig.customDocsPath : 'docs';
|
||||
}
|
||||
// returns map from id to object containing sidebar ordering info
|
||||
function readSidebar() {
|
||||
let allSidebars;
|
||||
if (fs.existsSync(CWD + "/sidebars.json")) {
|
||||
allSidebars = require(CWD + "/sidebars.json");
|
||||
if (fs.existsSync(CWD + '/sidebars.json')) {
|
||||
allSidebars = require(CWD + '/sidebars.json');
|
||||
} else {
|
||||
allSidebars = {};
|
||||
}
|
||||
|
@ -76,7 +72,7 @@ function readSidebar() {
|
|||
previous: previous,
|
||||
next: next,
|
||||
sidebar: sidebar,
|
||||
category: categoryOrder[i]
|
||||
category: categoryOrder[i],
|
||||
};
|
||||
}
|
||||
});
|
||||
|
@ -85,19 +81,19 @@ function readSidebar() {
|
|||
|
||||
// split markdown header
|
||||
function splitHeader(content) {
|
||||
const lines = content.split("\n");
|
||||
if (lines[0] !== "---") {
|
||||
const lines = content.split('\n');
|
||||
if (lines[0] !== '---') {
|
||||
return {};
|
||||
}
|
||||
let i = 1;
|
||||
for (; i < lines.length - 1; ++i) {
|
||||
if (lines[i] === "---") {
|
||||
if (lines[i] === '---') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return {
|
||||
header: lines.slice(1, i + 1).join("\n"),
|
||||
content: lines.slice(i + 1).join("\n")
|
||||
header: lines.slice(1, i + 1).join('\n'),
|
||||
content: lines.slice(i + 1).join('\n'),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -106,31 +102,33 @@ function extractMetadata(content) {
|
|||
const metadata = {};
|
||||
const both = splitHeader(content);
|
||||
if (Object.keys(both).length === 0) {
|
||||
return { metadata, rawContent: content };
|
||||
return {metadata, rawContent: content};
|
||||
}
|
||||
const lines = both.header.split("\n");
|
||||
const lines = both.header.split('\n');
|
||||
for (let i = 0; i < lines.length - 1; ++i) {
|
||||
const keyvalue = lines[i].split(":");
|
||||
const keyvalue = lines[i].split(':');
|
||||
const key = keyvalue[0].trim();
|
||||
let value = keyvalue
|
||||
.slice(1)
|
||||
.join(":")
|
||||
.join(':')
|
||||
.trim();
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} 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
|
||||
function processMetadata(file) {
|
||||
const result = extractMetadata(fs.readFileSync(file, "utf8"));
|
||||
const result = extractMetadata(fs.readFileSync(file, 'utf8'));
|
||||
|
||||
let regexSubFolder = new RegExp("/" + escapeStringRegexp(getDocsPath()) + "\/(.*)\/.*/");
|
||||
let regexSubFolder = new RegExp(
|
||||
'/' + escapeStringRegexp(getDocsPath()) + '/(.*)/.*/'
|
||||
);
|
||||
|
||||
let language = "en";
|
||||
let language = 'en';
|
||||
const match = regexSubFolder.exec(file);
|
||||
if (match) {
|
||||
language = match[1];
|
||||
|
@ -143,7 +141,7 @@ function processMetadata(file) {
|
|||
if (!metadata.id) {
|
||||
metadata.id = path.basename(file, path.extname(file));
|
||||
}
|
||||
if (metadata.id.includes("/")) {
|
||||
if (metadata.id.includes('/')) {
|
||||
throw new Error('Document id cannot include "/".');
|
||||
}
|
||||
if (!metadata.title) {
|
||||
|
@ -151,26 +149,26 @@ function processMetadata(file) {
|
|||
}
|
||||
|
||||
if (languages.length === 1 && !siteConfig.useEnglishUrl) {
|
||||
metadata.permalink = "docs/" + metadata.id + ".html";
|
||||
metadata.permalink = 'docs/' + metadata.id + '.html';
|
||||
} else {
|
||||
metadata.permalink = "docs/" + language + "/" + metadata.id + ".html";
|
||||
metadata.permalink = 'docs/' + language + '/' + metadata.id + '.html';
|
||||
}
|
||||
|
||||
if (ENABLE_VERSIONING) {
|
||||
metadata.version = "next";
|
||||
metadata.version = 'next';
|
||||
if (languages.length === 1 && !siteConfig.useEnglishUrl) {
|
||||
metadata.permalink = metadata.permalink.replace("docs/", "docs/next/");
|
||||
metadata.permalink = metadata.permalink.replace('docs/', 'docs/next/');
|
||||
} else {
|
||||
metadata.permalink = metadata.permalink.replace(
|
||||
"docs/" + language + "/",
|
||||
"docs/" + language + "/next/"
|
||||
'docs/' + language + '/',
|
||||
'docs/' + language + '/next/'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// change ids previous, next
|
||||
metadata.localized_id = metadata.id;
|
||||
metadata.id = language + "-" + metadata.id;
|
||||
metadata.id = language + '-' + metadata.id;
|
||||
metadata.language = language;
|
||||
|
||||
const order = readSidebar();
|
||||
|
@ -182,20 +180,20 @@ function processMetadata(file) {
|
|||
|
||||
if (order[id].next) {
|
||||
metadata.next_id = order[id].next;
|
||||
metadata.next = language + "-" + order[id].next;
|
||||
metadata.next = language + '-' + order[id].next;
|
||||
}
|
||||
if (order[id].previous) {
|
||||
metadata.previous_id = order[id].previous;
|
||||
metadata.previous = language + "-" + order[id].previous;
|
||||
metadata.previous = language + '-' + order[id].previous;
|
||||
}
|
||||
}
|
||||
|
||||
return { metadata, rawContent: rawContent };
|
||||
return {metadata, rawContent: rawContent};
|
||||
}
|
||||
|
||||
// process metadata for all docs and save into core/metadata.js
|
||||
function generateMetadataDocs() {
|
||||
console.log("Generating Metadata for Docs....");
|
||||
console.log('Generating Metadata for Docs....');
|
||||
|
||||
let order;
|
||||
try {
|
||||
|
@ -216,13 +214,13 @@ function generateMetadataDocs() {
|
|||
const defaultMetadatas = {};
|
||||
|
||||
// metadata for english files
|
||||
let files = glob.sync(CWD + "/../" + getDocsPath() + "/**");
|
||||
let files = glob.sync(CWD + '/../' + getDocsPath() + '/**');
|
||||
files.forEach(file => {
|
||||
let language = "en";
|
||||
let language = 'en';
|
||||
|
||||
const extension = path.extname(file);
|
||||
|
||||
if (extension === ".md" || extension === ".markdown") {
|
||||
if (extension === '.md' || extension === '.markdown') {
|
||||
const res = processMetadata(file);
|
||||
|
||||
if (!res) {
|
||||
|
@ -235,36 +233,36 @@ function generateMetadataDocs() {
|
|||
// these will get replaced if/when the localized file is downloaded from crowdin
|
||||
enabledLanguages
|
||||
.filter(currentLanguage => {
|
||||
return currentLanguage != "en";
|
||||
return currentLanguage != 'en';
|
||||
})
|
||||
.map(currentLanguage => {
|
||||
let baseMetadata = Object.assign({}, metadata);
|
||||
baseMetadata["id"] = baseMetadata["id"]
|
||||
baseMetadata['id'] = baseMetadata['id']
|
||||
.toString()
|
||||
.replace(/^en-/, currentLanguage + "-");
|
||||
if (baseMetadata["permalink"])
|
||||
baseMetadata["permalink"] = baseMetadata["permalink"]
|
||||
.replace(/^en-/, currentLanguage + '-');
|
||||
if (baseMetadata['permalink'])
|
||||
baseMetadata['permalink'] = baseMetadata['permalink']
|
||||
.toString()
|
||||
.replace(/^docs\/en\//, "docs/" + currentLanguage + "/");
|
||||
if (baseMetadata["next"])
|
||||
baseMetadata["next"] = baseMetadata["next"]
|
||||
.replace(/^docs\/en\//, 'docs/' + currentLanguage + '/');
|
||||
if (baseMetadata['next'])
|
||||
baseMetadata['next'] = baseMetadata['next']
|
||||
.toString()
|
||||
.replace(/^en-/, currentLanguage + "-");
|
||||
if (baseMetadata["previous"])
|
||||
baseMetadata["previous"] = baseMetadata["previous"]
|
||||
.replace(/^en-/, currentLanguage + '-');
|
||||
if (baseMetadata['previous'])
|
||||
baseMetadata['previous'] = baseMetadata['previous']
|
||||
.toString()
|
||||
.replace(/^en-/, currentLanguage + "-");
|
||||
baseMetadata["language"] = currentLanguage;
|
||||
defaultMetadatas[baseMetadata["id"]] = baseMetadata;
|
||||
.replace(/^en-/, currentLanguage + '-');
|
||||
baseMetadata['language'] = currentLanguage;
|
||||
defaultMetadatas[baseMetadata['id']] = baseMetadata;
|
||||
});
|
||||
Object.assign(metadatas, defaultMetadatas);
|
||||
}
|
||||
});
|
||||
|
||||
// metadata for non-english docs
|
||||
files = glob.sync(CWD + "/translated_docs/**");
|
||||
files = glob.sync(CWD + '/translated_docs/**');
|
||||
files.forEach(file => {
|
||||
let language = "en";
|
||||
let language = 'en';
|
||||
const match = regexSubFolder.exec(file);
|
||||
if (match) {
|
||||
language = match[1];
|
||||
|
@ -276,7 +274,7 @@ function generateMetadataDocs() {
|
|||
|
||||
const extension = path.extname(file);
|
||||
|
||||
if (extension === ".md" || extension === ".markdown") {
|
||||
if (extension === '.md' || extension === '.markdown') {
|
||||
const res = processMetadata(file);
|
||||
if (!res) {
|
||||
return;
|
||||
|
@ -295,17 +293,17 @@ function generateMetadataDocs() {
|
|||
metadata.category = order[id].category;
|
||||
if (order[id].next) {
|
||||
metadata.next_id = order[id].next.replace(
|
||||
"version-" + metadata.version + "-",
|
||||
""
|
||||
'version-' + metadata.version + '-',
|
||||
''
|
||||
);
|
||||
metadata.next = metadata.language + "-" + order[id].next;
|
||||
metadata.next = metadata.language + '-' + order[id].next;
|
||||
}
|
||||
if (order[id].previous) {
|
||||
metadata.previous_id = order[id].previous.replace(
|
||||
"version-" + metadata.version + "-",
|
||||
""
|
||||
'version-' + metadata.version + '-',
|
||||
''
|
||||
);
|
||||
metadata.previous = metadata.language + "-" + order[id].previous;
|
||||
metadata.previous = metadata.language + '-' + order[id].previous;
|
||||
}
|
||||
}
|
||||
metadatas[metadata.id] = metadata;
|
||||
|
@ -319,7 +317,7 @@ function generateMetadataDocs() {
|
|||
metadatas[metadata].previous_title =
|
||||
metadatas[metadatas[metadata].previous].title;
|
||||
} else {
|
||||
metadatas[metadata].previous_title = "Previous";
|
||||
metadatas[metadata].previous_title = 'Previous';
|
||||
}
|
||||
}
|
||||
if (metadatas[metadata].next) {
|
||||
|
@ -327,19 +325,19 @@ function generateMetadataDocs() {
|
|||
metadatas[metadata].next_title =
|
||||
metadatas[metadatas[metadata].next].title;
|
||||
} else {
|
||||
metadatas[metadata].next_title = "Next";
|
||||
metadatas[metadata].next_title = 'Next';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fs.writeFileSync(
|
||||
__dirname + "/../core/metadata.js",
|
||||
"/**\n" +
|
||||
" * @generated\n" +
|
||||
" */\n" +
|
||||
"module.exports = " +
|
||||
__dirname + '/../core/metadata.js',
|
||||
'/**\n' +
|
||||
' * @generated\n' +
|
||||
' */\n' +
|
||||
'module.exports = ' +
|
||||
JSON.stringify(metadatas, null, 2) +
|
||||
";"
|
||||
';'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -347,11 +345,11 @@ function generateMetadataDocs() {
|
|||
function generateMetadataBlog() {
|
||||
const metadatas = [];
|
||||
|
||||
let files = glob.sync(CWD + "/blog/**/*.*");
|
||||
let files = glob.sync(CWD + '/blog/**/*.*');
|
||||
if (!files || files.length == 0) {
|
||||
console.error(
|
||||
`${chalk.yellow(
|
||||
CWD + "/blog/ appears to be empty"
|
||||
CWD + '/blog/ appears to be empty'
|
||||
)} Make sure you've put your blog files in your Docusaurus 'website' folder.`
|
||||
);
|
||||
}
|
||||
|
@ -360,7 +358,7 @@ function generateMetadataBlog() {
|
|||
.reverse()
|
||||
.forEach(file => {
|
||||
const extension = path.extname(file);
|
||||
if (extension !== ".md" && extension !== ".markdown") {
|
||||
if (extension !== '.md' && extension !== '.markdown') {
|
||||
return;
|
||||
}
|
||||
// Transform
|
||||
|
@ -369,16 +367,14 @@ function generateMetadataBlog() {
|
|||
// 2015/08/13/blog-post-name-0-5.html
|
||||
const filePath = path
|
||||
.basename(file)
|
||||
.replace("-", "/")
|
||||
.replace("-", "/")
|
||||
.replace("-", "/")
|
||||
.replace(/\.md$/, ".html");
|
||||
const result = extractMetadata(
|
||||
fs.readFileSync(file, { encoding: "utf8" })
|
||||
);
|
||||
.replace('-', '/')
|
||||
.replace('-', '/')
|
||||
.replace('-', '/')
|
||||
.replace(/\.md$/, '.html');
|
||||
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
|
||||
);
|
||||
|
||||
|
@ -388,27 +384,27 @@ function generateMetadataBlog() {
|
|||
let filePathDateArr = path
|
||||
.basename(file)
|
||||
.toString()
|
||||
.split("-");
|
||||
.split('-');
|
||||
metadata.date = new Date(
|
||||
filePathDateArr[0] +
|
||||
"-" +
|
||||
'-' +
|
||||
filePathDateArr[1] +
|
||||
"-" +
|
||||
'-' +
|
||||
filePathDateArr[2] +
|
||||
"T06:00:00.000Z"
|
||||
'T06:00:00.000Z'
|
||||
);
|
||||
|
||||
metadatas.push(metadata);
|
||||
});
|
||||
|
||||
fs.writeFileSync(
|
||||
__dirname + "/../core/MetadataBlog.js",
|
||||
"/**\n" +
|
||||
" * @generated\n" +
|
||||
" */\n" +
|
||||
"module.exports = " +
|
||||
__dirname + '/../core/MetadataBlog.js',
|
||||
'/**\n' +
|
||||
' * @generated\n' +
|
||||
' */\n' +
|
||||
'module.exports = ' +
|
||||
JSON.stringify(metadatas, null, 2) +
|
||||
";"
|
||||
';'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -418,5 +414,5 @@ module.exports = {
|
|||
extractMetadata,
|
||||
processMetadata,
|
||||
generateMetadataDocs,
|
||||
generateMetadataBlog
|
||||
generateMetadataBlog,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue