mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-11 05:48:02 +02:00
Refactor + add more tests (Part 2) (#854)
* Refactor doc in server.js & generate.js * Refactor finding metadata with find() & avoid param reassign * Refactor replacing markdown link to html + add test * Minify func to getFile & add test * use ./ for md link as well * nits & better test * better mdToHtmlify * babel-register on versions & write-translations to transpile ES6 in server/env.js * better docs test & move out metadata to fixtures * Update .babelrc
This commit is contained in:
parent
03e237abda
commit
9f718a5097
13 changed files with 430 additions and 154 deletions
|
@ -9,12 +9,12 @@ async function execute() {
|
|||
require('../write-translations.js');
|
||||
|
||||
const metadataUtils = require('./metadataUtils');
|
||||
const docs = require('./docs');
|
||||
|
||||
const CWD = process.cwd();
|
||||
const fs = require('fs-extra');
|
||||
const readMetadata = require('./readMetadata.js');
|
||||
const path = require('path');
|
||||
const {insertTOC} = require('../core/toc');
|
||||
const {getPath} = require('../core/utils.js');
|
||||
const {minifyCss, isSeparateCss} = require('./utils');
|
||||
const React = require('react');
|
||||
|
@ -75,7 +75,6 @@ async function execute() {
|
|||
|
||||
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl);
|
||||
|
||||
const DocsLayout = require('../core/DocsLayout.js');
|
||||
const Redirect = require('../core/Redirect.js');
|
||||
|
||||
fs.removeSync(join(CWD, 'build'));
|
||||
|
@ -83,66 +82,13 @@ async function execute() {
|
|||
// create html files for all docs by going through all doc ids
|
||||
Object.keys(Metadata).forEach(id => {
|
||||
const metadata = Metadata[id];
|
||||
// determine what file to use according to its id
|
||||
let file;
|
||||
if (metadata.original_id) {
|
||||
if (env.translation.enabled && metadata.language !== 'en') {
|
||||
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
|
||||
} else {
|
||||
file = join(CWD, 'versioned_docs', metadata.source);
|
||||
}
|
||||
} else if (metadata.language === 'en') {
|
||||
file = join(CWD, '..', readMetadata.getDocsPath(), metadata.source);
|
||||
} else {
|
||||
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(file)) {
|
||||
const file = docs.getFile(metadata);
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
|
||||
let rawContent = metadataUtils.extractMetadata(
|
||||
fs.readFileSync(file, 'utf8')
|
||||
).rawContent;
|
||||
|
||||
const language = metadata.language;
|
||||
|
||||
// generate table of contents if appropriate
|
||||
rawContent = insertTOC(rawContent);
|
||||
|
||||
const defaultVersion = env.versioning.defaultVersion;
|
||||
|
||||
// replace any links to markdown files to their website html links
|
||||
Object.keys(mdToHtml).forEach(key => {
|
||||
let link = mdToHtml[key];
|
||||
link = getPath(link, siteConfig.cleanUrl);
|
||||
link = link.replace('/en/', `/${language}/`);
|
||||
link = link.replace(
|
||||
'/VERSION/',
|
||||
metadata.version && metadata.version !== defaultVersion
|
||||
? `/${metadata.version}/`
|
||||
: '/'
|
||||
);
|
||||
// replace relative links with & without "./"
|
||||
rawContent = rawContent.replace(
|
||||
new RegExp(`\\]\\((${key}|\\./${key})`, 'g'),
|
||||
`](${link}`
|
||||
);
|
||||
});
|
||||
|
||||
// replace any relative links to static assets to absolute links
|
||||
rawContent = rawContent.replace(
|
||||
/\]\(assets\//g,
|
||||
`](${siteConfig.baseUrl}docs/assets/`
|
||||
);
|
||||
|
||||
const docComp = (
|
||||
<DocsLayout metadata={metadata} language={language} config={siteConfig}>
|
||||
{rawContent}
|
||||
</DocsLayout>
|
||||
);
|
||||
const rawContent = metadataUtils.extractMetadata(file).rawContent;
|
||||
const docComp = docs.getComponent(rawContent, mdToHtml, metadata);
|
||||
const str = renderToStaticMarkupWithDoctype(docComp);
|
||||
|
||||
const targetFile = join(buildDir, metadata.permalink);
|
||||
writeFileAndCreateFolder(targetFile, str);
|
||||
|
||||
|
@ -155,7 +101,7 @@ async function execute() {
|
|||
const redirectComp = (
|
||||
<Redirect
|
||||
metadata={metadata}
|
||||
language={language}
|
||||
language={metadata.language}
|
||||
config={siteConfig}
|
||||
redirect={siteConfig.baseUrl + redirectlink}
|
||||
/>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue