-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathdocs.js
100 lines (91 loc) · 2.79 KB
/
docs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const CWD = process.cwd();
const siteConfig = require(`${CWD}/siteConfig.js`);
const {join} = require('path');
const fs = require('fs-extra');
const React = require('react');
const env = require('./env.js');
const readMetadata = require('./readMetadata.js');
const {insertTOC} = require('../core/toc.js');
const {getPath} = require('../core/utils.js');
function getFile(metadata) {
if (!metadata) {
return null;
}
let file;
if (env.versioning.enabled && 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 (env.translation.enabled && metadata.language !== 'en') {
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
} else {
file = join(CWD, '..', readMetadata.getDocsPath(), metadata.source);
}
if (!fs.existsSync(file)) {
return null;
}
return fs.readFileSync(file, 'utf8');
}
function mdToHtmlify(oldContent, mdToHtml, metadata) {
let content = oldContent;
const mdLinks = [];
// find any links to markdown files
const regex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
let match = regex.exec(content);
while (match !== null) {
mdLinks.push(match[1]);
match = regex.exec(content);
}
// replace to their website html links
new Set(mdLinks).forEach(mdLink => {
let htmlLink = mdToHtml[mdLink];
if (htmlLink) {
htmlLink = getPath(htmlLink, siteConfig.cleanUrl);
htmlLink = htmlLink.replace('/en/', `/${metadata.language}/`);
htmlLink = htmlLink.replace(
'/VERSION/',
metadata.version && metadata.version !== env.versioning.defaultVersion
? `/${metadata.version}/`
: '/'
);
content = content.replace(
new RegExp(`\\]\\((\\./)?${mdLink}`, 'g'),
`](${htmlLink}`
);
}
});
return content;
}
function getComponent(rawContent, mdToHtml, metadata) {
// generate table of contents
let content = insertTOC(rawContent);
// replace any links to markdown files to their website html links
content = mdToHtmlify(content, mdToHtml, metadata);
// replace any relative links to static assets to absolute links
content = content.replace(
/\]\(assets\//g,
`](${siteConfig.baseUrl}docs/assets/`
);
const DocsLayout = require('../core/DocsLayout.js');
return (
<DocsLayout
metadata={metadata}
language={metadata.language}
config={siteConfig}>
{content}
</DocsLayout>
);
}
module.exports = {
getComponent,
getFile,
mdToHtmlify,
};